Chapter 4 Control Statements



Chapter 4  Control Statements
Java's program control statements can be put into the following categories :
selection, iteration and jump.
1.Java's Selection statements :
Java supports two selection statements : if and switch.
if :
The if statement is a powerful decision making statement and is used to control the flow of execution of statement. It is basically a two-way decision statement and is used in conjunction with an expression.
It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is 'true' or 'false', then  transfer the control to a particular statement.
Syntax :
if (condition)
{
statement-block;//true
}
else
{
statement-block;//false
}

Flow chart:


Example :
if (flag==1)
{
System.out.println("prime");//true
}
else
{
System.out.println("Not prime");//false
}
Switch :
The switch statement is java's multiway branch statement. The Switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statement associated with case is executed.
Syntax :
switch(expression)
{
case value-1 :
block-1;
break;
case value-2 :block-2;
break;
.....
.....
default :default-block;
break;
}
statement-X;

The expression is an integer or character. Value-1, Value-2 ... are constant or constant expression. Default is an optional case.
Example :
...
int no=1;
switch(no)
{
case 1 :
System.out.println("One");
break;
case 2 :
System.out.println("Two");
break;
.......
.......
case 9 :
System.out.println("Nine");
break;
default :
System.out.println("Error");
break;
}
1.Java's Iteration statements :
Java's iteration statements are for, while, and do-while. These statements create what we commonly call loops.
while :
The while loop is java's most fundamental looping statement. it repeats a statements or block while its controlling expression is true. While is an entry-controlled loop statement. The basic structure of while is shown below.
Syntax :
while(test condition)
{
body of the loop
}
Flow chart :


The test-condition is evaluated and if the condition is true, then the body of loop is executed. After execution of the body, the test-condition is once again evaluated and if it is true. The body is executed once again. This process of repeated execution of the body continues until the test-condition. Finally becomes false and the control is transferred out of the loop.
Example :
...
n=10;.
while(n>0)
{
System.out.println("no : "+n);
n--;
}

do-while :
When you want to test termination expression at the end of the loop rather than at the beginning. The do-while loops always executes its body at least once, because its conditional expression is at the bottom of the loop. The do-while loop is exit-controlled loop. Its general form is shown below.
Syntax :
do(test condition)
{
body of the loop
} while (test-condition);
Flow chart :

On reaching the do statement, the program proceeds to evaluate the body of the loop first the end of the loop, the test-condition in the while statement is evaluated. If the condition is true, the program continues to evaluates the body of the loop once again. This process continues as long as the condition is true.
Example :
...
n=100;
do
{
n--;
System.out.println("n: "+n);
} while (n<0);
For Statement :
The for loop is another entry-controlled loop that provides a more concise loop control structure. The general form of for loop is :
for(initialization; test-condition; increment)
{
body of the loop
}
Flow chart:


Initialization of the control variable is done first, Ex. i=1 or i=0. The value of control variable is testing using the test-condition. Ex. i<10. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop ex. i++,j++.
Example :
for(i=1;i<=10;i++)
{
System.out.println("i "+i);
}