Professional Documents
Culture Documents
Lec 4 Control Structures
Lec 4 Control Structures
STRUCTURES
LECTURE 4
Control Structure
A statement that is used to control the flow of execution in a program is called control
structure. It combines instruction into logical unit. Logical unit has one entry point and one
exit point.
There are three categories of program control statements: selection statements, which
include: the if and the switch; iteration statements, which include the for, while, and do-
while loops; and jump statements, which include break, continue, and return
Selection
It selects a statement to execute on the basis of condition. Statement is executed
when the condition is true and ignored when it is false e.g if, if else, switch structures.
if(expression)
{
statement sequence
}
else
{
statement sequence
}
If the conditional expression is true, the target of the if will be executed; otherwise, if it exists,
the target of the else will be executed. At no time will both of them be executed. The
conditional expression controlling the if may be any type of valid C++ expression that produces
If statement
….then this is
if (CONDITION) executed
{
STATEMENTS
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.print("Input a number: ");
x = in.nextInt();
if (CONDITION)
{
STATEMENTS
}
else
{
STATEMENTS
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.print("Input a number: ");
x = in.nextInt();
}
Nested if
■ it's possible to have if..else statements inside a if..else statement in Java. It's called
nested if...else statement.
■ A nested if is an if statement that is the target of another if or else. Nested ifs are very
common in programming.
■ The main thing to remember about nested ifs in Java is that an else statement always
refers to the nearest if statement that is within the same block as the else and not already
associated with an else
if-else-if Ladder
■ A common programming construct that is based upon the nested if is the if-else-if
ladder. It looks like this
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else statement
public static void test(int x)
{
if (x > 5)
{
System.out.println(x + " is > 5");
}
else if (x == 5)
{
System.out.println(x + " equals 5");
}
else
{
System.out.println(x + " is < 5");
}
}
public static void main(String[] arguments)
{
test(6);
test(5);
test(4);
}
The switch Statement
■ The switch provides for a multiway branch. Thus, it enables a program to select among
several alternatives.
■ Although a series of nested if statements can perform multiway tests, for many
situations the switch is a more efficient approach. It works like this: the value of an
expression is successively tested against a list of constants.
■ When a match is found, the statement sequence associated with that match is executed
■ The general form of the switch statement is
■ Technically, the break statement is optional, although most applications of the switch
will use it.
■ When encountered within the statement sequence of a case, the break statement causes
program flow to exit from the entire switch statement and resume at the next statement
outside the switch.
■ However, if a break statement does not end the statement sequence associated with a
case, then all the statements at and following the matching case will be executed until a
break (or the end of the switch) is encountered
Nested switch Statements
■ It is possible to have a switch as part of the statement sequence of an outer switch. This
is called a nested switch. Even if the case constants of the inner and outer switch contain
common values, no conflicts will arise.
■ For example, the following code fragment is perfectly acceptable
Repetition
In this structure the statements are executed more than one time. It is also known as iteration or loop.
A loop structure is used to execute a certain set of actions for a predefined number of times or until a
particular condition is satisfied. These are some control statements available in java to implement loop
structures.
■ For loop
■ While loop
For loop
■ The general form of the for loop for repeating a single statement is
for(initialization; condition; iteration) statement;
■ The initialization is usually an assignment statement that sets the initial value of the
loop control variable, which acts as the counter that controls the loop.
■ The condition is a Boolean expression that determines whether or not the loop will
repeat
■ The iteration expression defines the amount by which the loop control variable will
change each time the loop is repeated.
Notice that these three major sections of the loop must be separated by semicolons. The for
loop will continue to execute as long as the condition tests true. Once the condition
becomes false, the loop will exit, and program execution will resume on the statement
following the for.
The following program uses a for loop to print the square roots of the numbers between 1
and 99. It also displays the rounding error present for each square root.
■ The for loop can proceed in a positive or negative fashion, and it can change the loop
control variable by any amount. For example, the following program prints the numbers
100 to−95, in decrements of 5.
The while Loop
■ Another of Javas loops is the while. The general form of the while loop is
while(condition) statement;
■ Although Do While loop and While loop in Java looks similar, they differ in the order
of execution.
■ In Java While loop, the condition is tested at the beginning of the loop, and if the
condition is True, then only statements in that loop will be executed. So, the While loop
executes the code block only if the condition is True.
■ In Java Do While loop, the condition is tested at the end of the loop. So, the Do While
executes the statements in the code block at least once even if the condition Fails.
THE END