Module 1
Chapter 4: Control
Statements
By:
Ms. Thanu M R
Dept. of IS&E
Jain Institute of Technology
Davangere.
Control Statements:-
• Java’s program control statements can be put into the
following categories: selection, iteration, and jump.
• Selection statements allow your program to choose different
paths of execution based upon the outcome of an expression
or the state of a variable.
• Iteration statements enable program execution to repeat one
or more statements (that is, iteration statements form loops).
• Jump statements allow your program to execute in a
nonlinear fashion.
Java’s Selection Statements :-
• Java supports two selection statements: if and switch.
1. if
• The if statement executes a block of code only if the specified
expression is true.
• If the value is false, then the if block is skipped and execution
continues with the rest of the program.
• You can either have a single statement or a block of code within an
if statement.
• Note that the conditional expression must be a Boolean
expression.
• Example:
int a = 10,
b = 20;
if (a > b) {
System.out.println("a > b"); }
if (a < b) {
2. If else:-
• The if statement is Java’s conditional branch statement. It can be
used to route program execution through two different paths.
• Here is the general form of the if statement:
• Here, each statement may be a single statement or a compound
statement enclosed in curly braces (that is, a block).
• The condition is any expression that returns a boolean value. The
else clause is optional.
• The if works like this: If the condition is true, then statement1 is
executed. Otherwise, statement2 (if it exists) is executed.
3. Nested ifs:-
• A nested if is an if statement that is the target of another if or else.
• When you nest ifs, the main thing to remember is that an else statement always
refers to the nearest if statement that is within the same block as the else and that
is not already associated with an else.
• Here is an example:
if(i == 10) {
if(j < 20) {
a = b; }
if(k > 100){
c = d; // this if is
else a = c; }// associated with this else }
else {
a = d; }// this else refers to if(i == 10)
4. The if-else-if Ladder
• A common programming construct that is based upon a sequence of nested ifs is the if-
else-if ladder.
• It looks like this:
• The if statements are executed from the top down.
• As soon as one of the conditions controlling the if is true, the statement associated with that if
is executed, and the rest of the ladder is bypassed.
• If none of the conditions is true, then the final else statement will be executed.
5. Switch:-
• The switch case statement is a multi-way branch with several
choices. A switch is easier to implement than a series of
if/else statements.
• Structure of Switch:
• The switch statement begins with a keyword, followed by
an expression that equates to a no long integral value.
• Following the controlling expression is a code block that
contains zero or more labeled cases.
• Each label must equate to an integer constant and each
must be unique.
• Working of switch case:
• When the switch statement executes, it compares the value
of the controlling expression to the values of each case
label.
• The program will select the value of the case label that
• If none of the case label values match, then none of the codes within the switch
statement code block will be executed. Java includes a default label to use in cases
where there are no matches.
• We can have a nested switch within a case block of an outer switch.
Example:
int a = 10, b = 20, c = 30; int status = -1;
if (a > b && a > c)
{ status = 1; }
else if (b > c)
{ status = 2; }
else
{ status = 3; }
switch (status)
{
case 1: System.out.println("a is the greatest"); break;
case 2: System.out.println("b is the greatest"); break;
case 3: System.out.println("c is the greatest"); break;
default: System.out.println("Cannot be determined");
}
• The break statement is optional. If you omit the break, execution will continue
on into the next case.
• It is sometimes desirable to have multiple cases without break statements
between them.
• For example, consider the following program:
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5"); break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10"); break;
default:
System.out.println("i is 10 or more");
}
Nested switch Statements:-
• You can use a switch as part of the statement sequence of an outer switch. This is
called a nested switch.
• Since a switch statement defines its own block, no conflicts arise between the case
constants in the inner switch and those in the • outer switch. there are three
In summary,
• For example, the following fragment is perfectly valid:
important features of the switch
switch(count) { statement to note:
case 1: switch(target) • The switch differs from the if in
{ // nested switch that switch can only test for
case 0: equality, whereas if can evaluate
System.out.println("target is zero"); break;any type of Boolean expression.
case 1: // no conflicts with outer switch That is, the switch looks only for
System.out.println("target is one"); break;a match between the value of the
} expression and one of its case
break; constants.
case 2: // ... • No two case constants in the
same switch can have identical
values. Of course, a switch
statement and an enclosing
outer switch can have case
constants in common.
Iteration Statements:-
• Java’s iterative statements are for , while, do-while. These
statements create what we commonly call loops. As you
probably know, a loop repeatedly executes the same set of
instructions until a termination condition is met.
1.The while loop:-
• The while statement is a looping construct control statement
that executes a block of code while a condition is true.
• You can either have a single statement or a block of code
within the while loop.
• The loop will never be executed if the testing expression
evaluates to false.
• The loop condition must be a boolean expression.
2. The do-while loop:-
• The do-while loop is similar to the while loop, except that the test is performed at
the end of the loop instead of at the beginning.
• This ensures that the loop will be executed at least once.
3. The for loop :-
• The for loop is a looping construct which can execute a set of instructions a
specified number of times. It’s a counter controlled loop.
Declaring Loop Control Variables Inside the for Loop :-
• Often the variable that controls a for loop is only needed for the purposes of the
loop and is not used elsewhere.
• When this is the case, it is possible to declare the variable inside the initialization
portion of the for.
• When you declare a variable inside a for loop, there is one important point to
remember: the scope of that variable ends Using
when thethefor Comma :-
statement does
• There will be times when you will want
to include more than one statement in
the initialization and iteration portions
of the for loop.
The For-Each Version of the for Loop :-
• Beginning with JDK 5, a second form of for was defined that implements a “for-
each” style loop.
• The general form of the for-each version of the for is shown here:
• Here, type specifies the type and itr-var specifies the name of an iteration variable
that will receive the elements from a collection, one at a time, from beginning to end.
• The collection being cycled through is specified by collection.
• There are various types of collections that can be used with the for, but the only type
used in this chapter is the array.
• Working:
• With each iteration of the loop, the next element in the collection is retrieved and
stored in itr-var.
• The loop repeats until all elements in the collection have been obtained.
• Because the iteration variable receives values from the collection, type must be the
same as (or compatible with) the elements stored in the collection.
• Thus, when iterating over arrays, type must be compatible with the base type of the
array.
3. Nested Loops :-
• Like all other programming languages, Java allows loops to be nested.
• That is, one loop may be inside another. For example, here is a program that nests
for loops:
Jump Statements :-
• Java supports three jump statements: break, continue, and return. These
statements transfer control to another part of your program.
1. Break statement:-
• The break statement transfers control out of the enclosing loop (for, while, do or
switch statement).
• You use a break statement when you want to jump immediately to the statement
following the enclosing control structure.
• You can also provide a loop with a label, and then use the label in your break
statement.
• The label name is optional, and is usually only used when you wish to terminate
the outermost loop in a series of nested loops.
2. Continue statement :-
• A continue statement stops the iteration of a loop (while, do or for) and causes
execution to resume at the top of the nearest enclosing loop.
• You use a continue statement when you do not want to execute the remaining
statements in the loop, but you do not want to exit the loop itself.
• You can also provide a loop with a label and then use the label in your continue
statement.
• The label name is optional, and is usually only used when you wish to return to
the outermost loop in a series of nested loops.
Example:-
System.out.println("Odd Numbers");
for (int i = 1; i <= 10; ++i)
{
if (i % 2 == 0)
continue;
System.out.println(i + "\t");
}
3. Return statement :-
• The return statement exits from the current method, and control flow returns to
where the method was invoked.