Chapter 3 Control Statment
Chapter 3 Control Statment
Control Statements
1
Control Statements
Generally, the statements inside source programs are
executed from top to bottom, in the order that they
appear.
Control flow statements, however, alter the flow of
execution and provide better control to the programmer
on the flow of execution.
In Java control statements are categorized into 3 groups:
1. Selection or Decision Making Statements : allow the
program to choose different parts of the execution based on
the outcome of an expression
2. Iteration Statements enable program execution to repeat
one or more statements
3. Jump Statements enable your program to execute in a non-
linear fashion Prepared by: Melkamu D. 2
Selection Statements
These statements allow us to control the flow of program execution based on condition.
Java supports 2 selection statements:
if statements
switch statements
If statement:
It performs a task depending on whether a condition is true or false.
It is basically a two-way decision making statement and is used in conjunction with an
expression.
The if statement may be implemented in different forms depending on the complexity of
conditions to be tested:
Test False
expression
?
switch (expression)
{
case value-1:
statement-block 1;
break;
case value-2:
statement-block 2;
break;
......
......
default:
default_statement;
break;
}
rest_of_program
Prepared by: Melkamu D. 11
Contd…
The expression must evaluate to a char, short or int, but not
long, float or double.
The values value-1, value-2, value-3, … are constants or constant
expressions (evaluable to an integral constant) and are known as
case labels.
Each of the case labels should be unique within the switch
statement.
statement-block1,statement-block2,…. Are
statement lists and may contain zero or
more statements.
Prepared by: Melkamu D. 12
Contd…
There is no need to put braces around the statement blocks of a
switch statement but it is important to note that case labels end with
a colon (:).
When the switch is executed, the value of the expression is
successfully compared against the values value-1, value-2, ….
If a case is found whose value matches with the value of the
expression, then the block of the statement(s) that follows the case
are executed; otherwise the default-statement will be executed.
The break statement at the end of each block signals the end of a
particular case and causes an exit from the switch statement.
Depending on the position of the control statement in the loop, a control structure can
be either as the entry-controlled loop or as exit-controlled loop.
In entry-controlled loop, the control conditions are tested before the start of the loop
execution.
In exit-controlled loop, the test is performed at the end of the
body of the loop and therefore the body is executed
unconditionally for the first time.
Three types of loops in java:
1. while loops
2. do …while loops
3. for loops
Example
int sum=0;
for(n=1; n<=100; n++)
{
sum=sum+n;
}
System.out.println(“Sum is:”+sum);
You can nest loops of any kind one inside another to any depth.
Example:
for(int i = 10; i > 0; i--)
{
while (i > 3)
{
if(i == 5){
break;
}
System.out.println(i); Inner Outer
i--; Loop Loop
}
System.out.println(i*2);
}
is used to alter the execution of the for, do, and while statements.
The general form of the continue statement is:
continue label;
It may be used with or without a label. When used without a label, it
causes the statement block of the innermost for, do, or while
statement to terminate and the loop’s boolean expression to be re-
evaluated to determine whether the next loop repetition should take
place.