ICSE Class 9 Computer Applications
7. Conditional Constructs in java
Conditional construct are specific statements that allow us to check a condition and execute certain parts of code depending on whether the condition is true or false.
Conditional statements in Java are : if, if-else, if–else if –else and switch case.
​
01 if statement
Syntax
if (expression) {
// statements
}
// Program to display the result - pass / fail(passing marks - 50 /100)
class ifEg
{
public static void main(String args[])
{
int comp=30;
if(comp>=50)
System.out.println("PASS");
}
}
​
​
02 if else
​
Syntax
if (expression)
{
// statements
}
else
{
// statements
}
​
// Program to display the result - pass / fail(passing marks - 50 /100)
class ifEg
{
public static void main(String args[])
{
int comp=30;
if(comp>=50)
System.out.println("PASS");
else
System.out.println("FAIL");
}
}
​
03 if else if
Syntax
if (expression1) {
// statements
}
else if(expression2) {
// statements
}
else {
// statements
}
​
/* Program to display the result based on average marks
AvgMarks Result
========================================
avg>=75 Greater than 75 Distinction
75>avg>=60 Betweem 60 and 74 I
60>avg>=45 Betweem 45 and 59 II
45>avg>=35 Betweem 35 and 44 Pass
avg<35 less than 35 Fails
*/
class ifEg
{
public static void main(String args[])
{
int avg=20;
if(avg>=75)
System.out.println("Distinction");
else if(avg>=60)
System.out.println("I");
else if(avg>=45)
System.out.println("II");
else if(avg>=35)
System.out.println("Pass");
else
System.out.println("Fail");
}
}
​
04 Nested if
​
​ A nested if is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. This is generally used when a condition needs to be satisfied inside another condition.
05 switch case
​
​ A switch statement is a type of selection control mechanism used to allow the value of a variable
or expression to change the control flow of program execution via a multiway branch.
​
Write a menu-driven program to input number of week’s day(1-7)
and translate to its equivalent name of the day of the week.
(eg. 1 to Sunday, 2 to Monday … 7 to Saturday)
​
import java.util.*;
public class switchEg
{
public static void main(String args[])
{
int dow;
Scanner kb=new Scanner(System.in);
System.out.println("Enter the week no.:(1-7):1->Sun 2->Mon 3->Tue 4->Wed 5->Thu 6->Fri 7->Sat");
dow=kb.nextInt();
switch(dow)
{
case 1: System.out.println("Sunday");
break;
case 2: System.out.println("Monday");
break;
case 3: System.out.println("Tuesday");
break;
case 4: System.out.println("Wednesday");
break;
case 5: System.out.println("Thursday");
break;
case 6: System.out.println("Friday");
break;
case 7: System.out.println("Saturday");
break;
default: System.out.println("Invalid choice");
}
}
}
​
​
06 System.exit(0)
​
​
Sometimes your program can encounter a situation that makes continuing with the program pointless.In these cases, you can end your program with a call to the System.exit(0) method .
​
/* Program for understanding System.exit(0); method */
class SystemExitEx
{
public static void main(String args[])
{
int answer=0,total=20;
int number=0;
if (number==0)
{
System.out.println("Error: Division by zero not allowed");
System.exit(0); //to end the program
}
else
{
answer = total / number;
System.out.println("The answer is " + answer);
}
System.out.println("Bye");
}
}
​​