Control statements in java

Def : Control statements are used for flow of execution and give better control for the programmer on the flow of exection. Without control statements programmers can not able to write better programs.

List of Control Statements

  1. if else statement
  2. do while loop
  3. while loop
  4. for loop
  5. nested loop
  6. for-each loop
  7. switch statement
  8. break statement
  9. continue statement
  10. return statement

1) if-else statement :

package seleniumtool.com;

public class IfElseStatement {

    public static void main(String[] args) {
        
        int i=9;
        int j=5;

        if(i<j){
            System.out.println("i is greater than j");
        }
        else if(i==j){
            System.out.println("i is equal to j");
        }
        else {
            System.out.println("i is less than j");
        }
    }

}

Result :
i is less than j

 

do..while
 

package seleniumtool.com;

class  DoWhile
{
    public static void main(String args[])
    {
        int i=3;
        do
        {
            System.out.println(+i);
            i++;
        }
        while(i<9);
    }
}

Output :
3
4
5
6
7
8

While loop in Java

package seleniumtool.com;

public class  WhileDemo
{
    public static void main(String args[])
    {
        int i=4;
        while(i<10) 
        {
            System.out.println(+i);
            i++;

        }
    }
}

Output :
4
5
6
7
8
9

 

for loop in Java

package seleniumtool.com;

public class ForLoop {

    public static void main(String[] args) {
        System.out.println("***Decrement***");
        for (int i=10; i>=0; i--){
            System.out.println(i);
        }
        
        System.out.println("***Increment***");
        for (int j=0; j<=10; j++){
            System.out.println(j);
        }
    }

}

 

Output :-
***Decrement***
10
9
8
7
6
5
4
3
2
1
0
***Increment***
0
1
2
3
4
5
6
7
8
9
10

 

nested loop in Java :-

package seleniumtool.com;

public class NestedLoop {

    public static void main(String[] args) {
        int r=10;   // r represents row
        for (int i=1;i<=r;i++)   // i represents row number
        {
            for(int s=1;s<=i;s++)  // s represents number of stars
            {
                System.out.print(" * ");
            }
            System.out.println();
        }
    }

}

Output :-

 * 
 *  * 
 *  *  * 
 *  *  *  * 
 *  *  *  *  * 
 *  *  *  *  *  * 
 *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 

 

for-each loop in Java

package seleniumtool.com;

public class ForEach {

    public static void main(String[] args) {
        
        int array[]= {15,-5,7,0,-4,13,-18}; // declare an array with 7 elements
        
       for (int i : array){   // for-each to retrive elements from array
           
           System.out.println(i); // i represent each element of arry
       }
    }

}

Output :-
15
-5
7
0
-4
13
-18

 

switch statement in Java

package seleniumtool.com;

import java.util.Scanner;

public class SwitchStatement {

    public static void main(String[] args) {

        int i;
        System.out.println("Enter any number (1 to 7) :");
        Scanner s=new Scanner(System.in);
        i=s.nextInt();
        switch(i)
        {
        case  1:
            System.out.println("Today is Monday");
            break;
        case  2:
            System.out.println("Today is Tuesday");
            break;
        case  3:
            System.out.println("Today is Wednesday");
            break;
        case  4:
            System.out.println("Today is Thursday");
            break;
        case  5:
            System.out.println("Today is Friday");
            break;
        case  6:
            System.out.println("Today is Saturday");
            break;
        case  7:
            System.out.println("Today is Sunday");
        default:
            System.out.println("Only enter value 1 to 7");
        }
    }
}

Output :-
Enter any number (1 to 7) :
4
Today is Thursday

 

break statement in Java :-

package seleniumtool.com;

public class Break {

    public static void main(String[] args) {
        int [] numbers = {0, 1, 2, 3, 4, 5};

          for(int x : numbers ) {
             if( x == 4 ) {
              break;
             }
             System.out.print( x );
             System.out.print("\n");
          }
       }
    }

Output :-
0
1
2
3

continue statement in Java :-

package seleniumtool.com;

public class Continue {

    public static void main(String[] args) {
        int [] numbers = {0, 1, 2, 3, 4, 5};

          for(int x : numbers ) {
             if( x == 3 ) {
                    continue;
             }
             System.out.println( x );
             
          }

    }

}

Output :-
0
1
2
4
5

 

return statement :-

package seleniumtool.com;

public class returnStatement {

    public static void main(String[] args) {
    int a= 3;
    System.out.println("Before return");
    if (a>2) return;
    System.out.println("After return");

    }

}

Output :-
Before return