Control Statements

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Java Control Statements

Decision making statement

o if statements
o switch statement

Loop statements

o do while loop
o while loop
o for loop

If Statements

1. Simple if statement
2. if-else statement
3. if-else-if ladder

1) Simple if statement:

It is the most basic statement among all control flow statements in Java. It evaluates
a Boolean expression and enables the program to enter a block of code if the
expression evaluates to true.

Syntax of if statement is given below.

if(condition) {    
statement 1; //executes when condition is true   
}    

2) if-else statement

The if-else statement is an extension to the if-statement. If the condition true


statement 1 will be executed otherwise statement 2 will be executed.

Syntax:

if(condition) {    
statement 1; //executes when condition is true   
}  
else{  
statement 2; //executes when condition is false   
}  

Example:

public class Student {  
public static void main(String[] args) {  
int x = 10;  
int y = 12;  
if(x+y < 10)
 {  
System.out.println("x + y is less than      10");  

 else
 {  
System.out.println("x + y is greater than 20");  
}  
}  
}  

Output: x + y is greater than 20

3) if-else-if ladder:

The if-else-if statement contains the if-statement followed by multiple else-if


statements

Syntax of if-else-if statement is given below.

if(condition 1) {    
statement 1; //executes when condition 1 is true   
}  
else if(condition 2) {  
statement 2; //executes when condition 2 is true   
}  
else {  
statement 2; //executes when all the conditions are false   
}  

Example:

public class Student {  
public static void main(String[] args) {  
int num = 2;  
if(num == 0) 
{  
System.out.println("number is 0");  
}
else if (num == 1) 
{  
System.out.println("number is 1");  
}
else
 {  
System.out.println(num);  
}  
}  
}  

Output: 2

Switch statement:

In Java, Switch statements are similar to if-else-if statements. The switch statement contains


multiple blocks of code called cases and a single case is executed based on the variable which is
being switched. 

The syntax to use the switch statement is given below.

switch (expression){  
    case value1:  
     statement1;  
     break;  
    .  
    .  
    .  
    case valueN:  
     statementN;  
     break;  
    default:  
     default statement;  
}  

Example:

public class Student implements Cloneable {  
public static void main(String[] args) {  
int num = 2;  
switch (num){  
case 0:  
System.out.println("number is 0");  
break;  
case 1:  
System.out.println("number is 1");  
break;  
default:  
System.out.println(num);  
}  
}  
}  

Output: 2

Looping statements:

For loop:

In Java, for loop is similar to C and C++. It enables us to initialize the loop variable,


check the condition, and increment/decrement in a single line of code. We use the
for loop only when we exactly know the number of times, we want to execute the
block of code.
Syntax:

for(initialization, condition, increment/decrement) {    
//block of statements    
}    

Example:

public class Calculattion {  
public static void main(String[] args) {  
for(int j = 1; j<=5; j++)
 {  
System.out.print(j+" “);
}  
}  
}  

Output: 1 2 3 4 5

while loop
The while loop is also used to iterate over the number of statements multiple times.
However, if we don't know the number of iterations in advance, it is recommended to
use a while loop.

The syntax of the while loop is given below.

while(condition){    
//looping statements    
}    

Example:

public class Calculattion {  
public static void main(String[] args) {  
int j=1;
while( j<=5)
 {  
System.out.print(j+" “);
J++;
}  
}  
}  

Output: 1 2 3 4 5

do-while loop
The do-while loop checks the condition at the end of the loop after executing the
loop statements. When the number of iteration is not known and we have to execute
the loop at least once, we can use do-while loop.

Syntax:

do     
{    
//statements    
} while (condition);

Example:

public class Calculattion {  
public static void main(String[] args) {  
int j=1;
do {  
System.out.print(j+" “);
J++;
} while( j<=5);

}  
}  

Output: 1 2 3 4 5

You might also like