Java Technology -2018 - Control Statements

Java If-else Statement   

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.
  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement
----------
if(condition){  
//code to be executed  
----------
if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}  
-------------
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  
}  
------------

public class IfElseExample {  
public static void main(String[] args) {  
    int number=13;  
    if(number%2==0){  
        System.out.println("even number");  
    }else{  
        System.out.println("odd number");  
    }  
}  
}  
----------------

public class IfElseIfExample {  
public static void main(String[] args) {  
    int marks=65;  
      
    if(marks<50){  
        System.out.println("fail");  
    }  
    else if(marks>=50 && marks<60){  
        System.out.println("D grade");  
    }  
    else if(marks>=60 && marks<70){  
        System.out.println("C grade");  
    }  
    else if(marks>=70 && marks<80){  
        System.out.println("B grade");  
    }  
    else if(marks>=80 && marks<90){  
        System.out.println("A grade");  
    }else if(marks>=90 && marks<100){  
        System.out.println("A+ grade");  
    }else{  
        System.out.println("Invalid!");  
    }  
}  

Java Switch Statement

switch(expression){    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    
    
default:     
 code to be executed if all cases are not matched;    
}    
-------------

public class SwitchExample {  
public static void main(String[] args) {  
    int number=20;  
    switch(number){  
    case 10: System.out.println("10");break;  
    case 20: System.out.println("20");break;  
    case 30: System.out.println("30");break;  
    default:System.out.println("Not in 10, 20 or 30");  
    }  
}  

Java For Loop

The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loop in java.
  • Simple For Loop
  • For-each or Enhanced For Loop
  • Labeled For Loop
public class ForExample {  
public static void main(String[] args) {  
    for(int i=1;i<=10;i++){  
        System.out.println(i);  
    }  
}  
}  

---------------

public class ForEachExample {  
public static void main(String[] args) {  
    int arr[]={12,23,44,56,78};  
    for(int i:arr){  
        System.out.println(i);  
    }  
}  
}  

-------------
public class LabeledForExample {  
public static void main(String[] args) {  
    aa:  
        for(int i=1;i<=3;i++){  
            bb:  
                for(int j=1;j<=3;j++){  
                    if(i==2&&j==2){  
                        break aa;  
                    }  
                    System.out.println(i+" "+j);  
                }  
        }  
}  
}  

-------------
public class ForExample {  
public static void main(String[] args) {  
    for(;;){  
        System.out.println("infinitive loop");  
    }  
}  
}
//Use ctrl+c to go out from infinitive loop

Java While Loop

public class WhileExample {  
public static void main(String[] args) {  
    int i=1;  
    while(i<=10){  
        System.out.println(i);  
    i++;  
    }  
}  
}  
------------
public class WhileExample2 {  
public static void main(String[] args) {  
    while(true){  
        System.out.println("infinitive while loop");  
    }  
}  
}  

Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.
public class DoWhileExample {  
public static void main(String[] args) {  
    int i=1;  
    do{  
        System.out.println(i);  
    i++;  
    }while(i<=10);  
}  
}  
----------------
public class DoWhileExample2 {  
public static void main(String[] args) {  
    do{  
        System.out.println("infinitive do while loop");  
    }while(true);  
}  

Java Break Statement

The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.

Java Continue Statement

The Java continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. In case of inner loop, it continues only inner loop.

public class ContinueExample {  
public static void main(String[] args) {  
    for(int i=1;i<=10;i++){  
        if(i==5){  
            continue;  
        }  
        System.out.println(i);  
    }  
}  
}  


Comments

Popular posts from this blog

Jersey JAX RS Jar

Tomcat Installation Steps for Windows & add tomcat to eclipse

REST API