Monday, September 29, 2008

CONTROL STATEMENTS

Control Statements :
The control statement are used to controll the flow of execution of the program . This execution order depends on the supplied data values and the conditional logic.Java contains these types of control statements.
1- Selection Statements
2- Repetition Statements
3- Branching Statements .
Selection statements:
If Statement:
This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed . Syntax:
if(conditional_expression)
{
;
...;
...;
}
Example: If n%2 evaluates to 0 then the "if" block is executed. Here it evaluates to 0 so if block is executed. Hence "This is even number" is printed on the screen.
int n = 10;
if(n%2 = = 0)
{
System.out.println("this is even number");
}
output:10 is even number.
If-else Statement:
The "if-else" statement is an extension of if statement that provides another option when 'if' statement evaluates to "false" i.e. else block is executed if "if" statement is false.
Syntax:
if(conditional_expression)
{ ; ...; ...;
}
else
{
; ....; ....;
}
Example: If n%2 doesn't evaluate to 0 then else block is executed. Here n%2 evaluates to 1 that is not equal to 0 so else block is executed. So "This is not even number" is printed on the screen.
int n = 11;
if(n%2 = = 0)
{
System.out.println("This is even number");
}
else
{
System.out.println("This is not even number");
}
output:11 is not even nuber.
Switch Statement:
This is an easier implementation to the if-else statements. The keyword "switch" is followed by an expression that should evaluates to byte, short, char or int primitive data types ,only. In a switch block there can be one or more labeled cases. The expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed ,if no case matches then the default statement (if present) is executed.
Syntax: switch(control_expression)
{ case expression 1: ;
case expression 2: ; ... ...
case expression n: ;
default: ;
}//end switchExample: Here expression "day" in switch statement evaluates to 5 which matches with a case labeled "5" so code in case 5 is executed that results to output "Friday" on the screen.
int day = 5;
switch (day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thrusday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid entry");
break;
}
output:1
monday.
Repetition Statements:
while loop statements:
This is a looping or repeating statement. It executes a block of code or statements till the given condition is true. The expression must be evaluated to a boolean value. It continues testing the condition and executes the block of code. When the expression results to false control comes out of loop.
Syntax:
while(expression)
{ ; ...; ...;
}
Example: Here expression i<=10 is the condition which is checked before entering into the loop statements. When i is greater than value 10 control comes out of loop and next statement is executed. So here i contains value "1" which is less than number "10" so control goes inside of the loop and prints current value of i and increments value of i. Now again control comes back to the loop and condition is checked. This procedure continues until i becomes greater than value "10". So this loop prints values 1 to 10 on the screen.
int i = 1;
//print 1 to 10
while (i <= 10)
{
System.out.println("Num " + i);
i++;
}
output:1 2 3 4 5 6 7 8 9 10.
do-while loop statements:
This is another looping statement that tests the given condition past so you can say that the do-while looping statement is a past-test loop statement. First the do block statements are executed then the condition given in while statement is checked. So in this case, even the condition is false in the first attempt, do block of code is executed at least once.
Syntax:
do
{ ; ...; ...;
}while (expression);
Example: Here first do block of code is executed and current value "1" is printed then the condition i<=10 is checked. Here "1" is less than number "10" so the control comes back to do block. This process continues till value of i becomes greater than 10.
int i = 1;
do{
System.out.println("Num: " + i);
i++;
}while(i <= 10);
output:1 2 3 4 5 6 7 8 9 10.
for loop statements:
This is also a loop statement that provides a compact way to iterate over a range of values. From a user point of view, this is reliable because it executes the statements within this block repeatedly till the specified conditions is true .
Syntax:
for (initialization; condition; increment or decrement)
{ ; ...; ...;
}
Initialization:
The loop is started with the value specified.condition: It evaluates to either 'true' or 'false'. If it is false then the loop is terminated. increment or decrement: After each iteration, value increments or decrements. Example: Here num is initialized to value "1", condition is checked whether num<=10. If it is so then control goes into the loop and current value of num is printed. Now num is incremented and checked again whether num<=10.If it is so then again it enters into the loop. This process continues till num>10. It prints values 1 to10 on the screen.
for (int num = 1; num <= 10; num++)
{
System.out.println("Num: " + num);
}
output:1 2 3 4 5 6 7 8 9 10.

No comments: