Add



Java Control and Iterative Statements

This lesson will focus on how you can change the flow of your program execution using certain control statements.

There are 2 types of control statements in Java. One category is of conditional statements that are used for checking condition. The second category belongs to Iterative statements (loops).

1. Conditional Statements

As for as conditional statements are concerned, there are two such statements in Java i.e. if statement, and switch statement.

If Statement

This statement consists of 3 things: if keyword, a condition, and a block of statements.

    if (condition)
    {
        … (if block)
    }

When control comes to if statement then first of all condition is evaluated. Now if its output comes out true then the if-block (set of statements) would be executed. Otherwise, the control would be transferred to the immediate next statement to if-statement.
For example,

    int x =7;
    if(x>5)
    {
       System.out.println("X is greater than 5");
    }

As 7 is greater than 5 so the if-block statement System.out.println("X is greater than 5"); would be executed.

If- Else Statement

There is also if-else statement (variation of if statement). In this case if if-statement’s condition becomes true then if-block would be executed. Otherwise else-block would be executed.

    if (condition)
    {
        …(if block)
    }
    else 
    {
        …(else block)
    }

Nested If

You can put If with-in an If. This is called Nested If.
For example,

    int a = 10;
    int b = 8; 
    int c = 9;
    if(a>b)
    {
        if(a>c)
        {
          System.out.println("a is greater than b & c");
        }
    }
    else
    {
        System.out.println("a is lesser than b");
    }

Switch Statement

This statement takes the executing program to one of several paths. First of all an expression is evaluated and then it is matched with the given cases. When it found its match, it executes that case-block. There is also a default keyword which is used if no case matched.

    char ch = 'B';
    switch(ch)
    {
       case 'A':
	        System.out.println("Print A");
            break;
       case 'B':
            System.out.println("Print B");
            break;
       default:
            System.out.println("Nothing");
    }

Note that last statement of each case-block is break. This has been put here to exit the switch statement after the execution of any case-block. Otherwise all case-blocks that are following the matched case-block would also be executed.

2. Iterative Statements

There are 3 such statements in Java i.e. while statement, Do statement, for statement.

While Statement

It is used to execute a set of statements for more than one time till the condition remains true.

    while (condition)
    {
        … (while block)
    }

First of all condition is being checked and if comes out true then while-block would be executed. Now after the execution of while block statements, the while-condition is again checked and if is true then while block will be executed and so on. Now when while-condition becomes false then control is transferred to the immediate statement after while statement.
For example,

    int k = 36;
    while(k>30)
    {
	    System.out.println("k >30");
	    k = k-1;
    }

Do Statement

The Do statement (also called Do-While statement) works very similar to While statement except that termination condition is at end.

    do
    {
        … (do block)

    } while (condition);

For Statement

The for statement is used to iterate a set of statements certain number of times.

    for (initialization; condition; update)
    {
        … (for block)
    } 

for statement has three parts: initialization is used to initialize a variable, condition is to check the condition, update is used to increment/decrement the initialized variable.

    for(int count=0 ; count<78 ; count++)
    {
       System.out.println("count = "+count);
    }

Nested For

You can define for within for statement.

    for(…)
    {
	    for(…)
	    {
		    …
        }
    }

Break Statement

To break switch statement or to break loops, the break statement is used.

    for(…)
    {
	    if(x>7)
	    {
		    break;	
        }
        …
    }

Endless Loops

Notice that the loops should be finally ended at some point. Otherwise program will get stuck in the loop and never ends.
For example,

    int var = 23;
    while(var>20)
    {
        System.out.println(var);
    }