5 Condtional Statements
5 Condtional Statements
1.if statement
2.if-else statement
3.if-else-if ladder
4.nested if statement
Java if Statement
if(condition)
{
//code to be executed
}
Data-flow-diagram of if Block
Example
public class IfDemo1 {
int marks=70;
System.out.print("First division");
}
Java if-else Statement
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Data-flow-diagram of if else Block
Example
•The if-else-if ladder statement executes one condition from multiple statements.
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Data-flow-diagram of If Else If Block
int marks=75; System.out.println("B grade");
if(marks<50) }
System.out.println("fail"); {
} System.out.println("A grade");
System.out.println("D grade"); {
} System.out.println("A+ grade");
{ System.out.println("Invalid!");
System.out.println("C grade"); }
int weight=70;
if(age>=18)
if(weight>50)
}
Java Switch Statement
•Syntax
switch(expression)
{
case x: // code block
break;
case y: // code block
break;
default: // code block
}
• Switch expression is evaluated once.
• Value of the expression is compared with the values of each case.
• If there is a match, the associated block of code is executed.
• Break and default keywords are optional
• Case values must be unique. In case of duplicate value, it renders compile-
time error.
int number=20;
switch(number){
break;
break;
break;
switch(name){
case "Mango":
System.out.println("It is a fruit");
break;
case "Tomato":
System.out.println("It is a vegitable");
break;
case "Coke":
Syntax:
while (condition)
{
//code to be executed
Increment / decrement statement
}
Example
class Main {
public static void main(String[] args)
{
int i = 1, n = 5;
while(i <= n)
{
System.out.println(i);
i++;
}
}}
Note:
Do not forget to increase the variable used in the condition, otherwise the loop
will never end!
Java do-while Loop
• This loop will execute the code block once, before checking if the condition
is true, then it will repeat the loop as long as the condition is true.
• Syntax:
do{
//update statement
}while(condition);
Flowchart of Java do while loop
Example
class Main
{
public static void main(String args[])
{
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
}}
for Loop
4. Statement:
• The statement of the loop is executed each time until the second
condition is false.
public class ForDemo1
{
public static void main(String[] args)
{
int n, i;
n=2;
for(i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
}
}
Java Nested for Loop
int rows = 5;
for (int i = 1; i <= rows; ++i) {
int rows = 5;
}
Java break Statement
for loop
while loop
do-while loop
Example
class Test {
public static void main(String[] args) {
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2)
{
Output:
break; 11
12
} 13
21
System.out.println(i+" "+j); 31
32
} 33
}
labeled break Statement
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2)
{
break aa;
Output:
}
11
System.out.println(i+" "+j); 12
13
} 21
}
Java continue Statement
condition occurs, and continues with the next iteration in the loop.
Working of Java continue Statement
Example
class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; ++i)
{
// if value of i is between 4 and 9 continue is executed
if (i > 4 && i < 9) {
continue; Output
1
} 2
3
System.out.println(i); 4
9
} 10
Continue Statement with Inner Loop
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
Output
continue; 11
12
} 13
21
System.out.println(i+" "+j); 23
31
32
}
33
Continue Statement with Labelled For Loop
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
continue aa;
} Output
11
System.out.println(i+" "+j); 12
13
} 21
31
} 32
33