Title: Control%20Statements
1Chapter - 2
Control Statements
2This chapter includes -
- Selection Statements
- if statement
- if-else-then statement
- switch statement
- Iteration Statements
- while statement
- Do-while statement
- for statement
- Jump statements
- Break statements
- Continue statement
- Return statement
3Control Statements
- Javas control statements are nearly identical to
those in C/C. - However there are few differences - especially in
the break and continue statement
Selection Statements
- Java supports two selection statements if and
switch - The general form of Javas if statement is
- if (condition) statement1
- else statement2
- Each statement may be a single statement or a
compound statement enclosed in curly braces (that
is, a block). - The else clause is optional.
- The if works like this If the condition is
true, then statement1 is executed. Otherwise,
statement2 (if exists) excutes.
4Nested if Statements
- Nested ifs are very common in programming.
- An else statement always refers to the nearest if
statement that is within the same block. - Here is an example
- if(i 10)
- if(j lt 20) a b
- if(k gt 100) cd
- else a c // associated with the if(k gt
100) -
- else a d // this else refers to
if(i 10) -
5The if-else-if Ladder
- Let us follow the example below to understand the
if-else-if Ladder - class IfElse
- public static void main(String arg )
- int month 4 // April
- String season
- if(month 12 month 1 month
2) - season Winter
- else if(month 3 month 4 month
5) - season Spring
- else if(month 6 month 7
month 8) - season Summer
- else if(month 9 month 10 month
11) - season Authum
- else
- season Bogus Month)
- System.out.println(April is in the season
.) -
6switch Statement
- The general form is
- switch (expression)
- case value1
- // statement sequence
- break
- case value2
- // statement sequence
- break
- .
- .
- case valueN
- // statement sequence
- break
- default
- // default statement sequence
7An Example of switch Statements
- Let us follow the example below to understand the
use of switch - class SampleSwitch
- public static void main(String arg )
- for(int i 0 i lt 4 i)
- switch(i)
- case 0
- System.out.println( i is zero)
- break
- case 1
- System.out.println( i is one)
- break
- case 2
- System.out.println( i is two)
- break
- default
- System.out.println( i is greater than
2) -
8Structure of Nested switch Statements
switch(count) case 1 switch(target)
case 0 System.out.println( target
is zero) break case 1
System.out.println( target is
one) break default System.out.prin
tln( i is greater than 2)
break case 2 //
9Iteration Statements
- Javas iteration statement are for, while, and
do-while. - These statements create what we commonly call
loops.
while Statements
- It repeats a statement or block while its
controlling expression is true. - The general form of while statement is
- while (condition)
- // body of the loop
-
- The condition can be any Boolean expression. The
body of the loop will be executed as long as the
conditional expression is true. - EXAMPLE class WhileDemo
- public static void main(String arg )
- int n10
- while (ngt0)
- System.out.println(n n)
- n - -
-
-
-
10do-while statement
- You might have noticed that if the conditional
expression controlling a while loop is initially
false, then the body of the loop will not be
executed at all. - The do-while loops always executes its body
atleast once, because its conditional expression
is at the bottom of the loop. - The general for of do-while statements is as
follows - do
- // body of loop
- while (expression)
- If the expression is true the loop will repeated.
Follow the example - class DoWhileDemo
- public static void main(String arg )
- int n10
- do
- System.out.println(n n)
- n - -
- while (n gt 0)
-
-
11for statement
- The general for of do-while statements is as
follows - for(initialization condition iteration)
- // body
-
- The for loop operates as follows. When the loop
first starts, the initailization portion of the
loop is executed. - This is an expression that sets the value of the
loop control variable. - Next condition is evaluated. This must be a
Boolean expression. - If the expression value is true, then the body of
the loop is executed. - Next, the iteration portion of the loop is
executed. - Follow the example
- class ForDemo
- public static void main(String arg )
- int n
- for (n10 ngt0 n - -)
- System.out.println(n n)
-
-
-
12Using the Comma in for statement
- To allow two or more or more variables to control
a for loop, Java permits you to include multiple
statements in both the initialization and
iteration portion of the for. - Let us follow the example
- int a, b
- for(a 1, b 4 altb a, b - - )
- System.out.println(a a)
- System.out.println(b b)
-
Nested for Loops
int i, j for(i 0 ilt10 i ) for(j
0 jlt10 j ) System.out.print ( .
) System.out.println( )
13Variation in for statement
- VARIATION 1
- The condition controlling the for can be any
Boolean Expression. - Follow the example below
- boolean done false
- for(int i 1 !done i)
- // ..
- if(interrupted( )) done true
-
- VARIATION 2
- Either the initialization or the iteration
expression or both may be absent. - Int i0
- boolean done false
- for( !done )
- // ..
- i
- if(i 10) done true
-
14Jump statement
- Java supports three jump statements break,
continue and return. - These statements transfer control to another part
of your program.
Using Break
- In Java, the break statement has three uses.
- First as you have seen, it terminates a statement
sequence in a switch statement. - Second, it can be used to exit a loop.
- Third, it can be used as a civilized form of
goto. - USING BREAK TO EXIT A LOOP
- for(int i0 i lt 100 i)
- if(i 10) break // terminates loop if i
10 - System.out.println(i i)
-
- System.out.println(loop complete.)
- break statement can be used with any of javas
loops, like while loop. - When used inside a set of nested loops, the break
statement will only break out of the innermost
loop.
15Using Break As a form of GOTO
- Java defines an expanded form of the break
statement. - By using this form of break statement, you can
break out of one or more blocks of code. This
form of block works with label. - class BreakInvalidDemo
- public static void main(String arg )
- one for(int i 0 ilt3 i)
- System.out.println(Pass i )
-
- for(int j 0 jlt3 j)
- if ( j 10) break one // WRONG
- System.out.print( j )
-
-
16Using Break As a form of GOTO (Continued)
- Keep in mind that you can not break to any label
which is not defined for an enclosing block. - For example, the following program is invalid and
will not compile. - class BreakDemo
- public static void main(String arg )
- boolean t true
- first
- second
- third
- System.out.println(Before break)
- if(t) break second
- System.out.println(This wont
execute) -
- System.out.println(This wont execute)
-
- System.out.println(This is after second
block) -
-
17Continue Statement
- In while and do-while loops a continue statement
causes control to be transferred directly to the
conditional rxpression that controls the loop. - In a for loop, control goes first to the
iteration portion of the for statement and then
to the conditional expression. - Follow the example below
- class ContinueDemo1
- public static void main(String arg )
- for (int i0 ilt10i)
- System.out.print(i )
- if(i2) continue
-
- System.out.println( )
-
18Continue Statement (continued)
- As with the break statement, continue may specify
a label to describe which enclosing loop to
continue. - class ContinueDemo
- public static void main(String arg )
- outer for (int i0 ilt10i)
- for(int j0jlt10j)
- if(jgt1)
- System.out.println( )
- continue outer
-
- System.out.println((i j))
-
-
- System.out.print( )
-
19return Statement
- The return statement is used to explicitly return
from a method. - class ReturnDemo
- public static void main(String arg )
- boolean t true
- System.out.println(before return statement
) - if(t) return
- System.out.println(This wont execute)
-