Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

CONTROL

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, if else structures


if and else are single statements. The else clause is optional. The targets of both the if and else
can be blocks of statements. The general form of the if using blocks of statements is

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

■ Tells the computer to take one of two alternative courses of action


■ Depends on whether the value of a given boolean-valued expression is true or false
If statement If this is true….

….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(x%2==0) System.out.println(x+" is an even number");


}
Comparison statement

■ Condition can contain any comparison statement


■ x > y: x is greater than y
■ x < y: x is less than y
■ x >= y: x is greater than or equal to x
■ x <= y: x is less than or equal to y
■ x == y: x equals y
( equality: ==, assignment: = )
else

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();

if(x%2==0) System.out.println(x+" is an even number");

Else System.out.println(x+" is not an even number");

}
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

• The switch expression can be of type char, byte,


short, or int. (Floating-point expressions, for
example, are not allowed.

• No two case constants in the same switch can have


identical values. The default statement sequence is
executed if no case constant matches the
expression.

• The default is optional; if it is not present, no


action takes place if all matches fail.

• When a match is found, the statements associated


with that case are executed until the break is
encountered or, in the case of default or the last
case, until the end of the switch is reached
Simple switch program
The break

■ 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;

■ For repeating a block, the general form is


for(initialization; condition; iteration)
{
statement sequence
}
For loop

■ 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;

■ where statement may be a single statement or a block of statements, and condition


defines the condition that controls the loop, and it may be any valid Boolean expression.
The loop repeats while the condition is true.
■ When the condition becomes false, program control passes to the line immediately
following the loop
■ Here is a simple example in which a while is used to print the alphabet:
do-while loop

■ How do-while loop works?


First, the statements inside loop execute and then the
condition gets evaluated, if the condition returns true
then the control gets transferred to the “do” else it
jumps to the next statement after do-while
Difference between While and Do While in Java

■ 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

You might also like