Modern Programming Tools and Techniques-I: Lecture 4: Control Structures
Modern Programming Tools and Techniques-I: Lecture 4: Control Structures
Techniques-I
Lecture 4: Control Structures
Selection Statements
• Java supports two selection statements: if and switch.
if statement
if (condition) statement1;
else statement2;
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)
The if-else-if Ladder
• A sequence of nested ifs is the if-else-if ladder.
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
• The if statements are executed from the top to down.
switch
• The switch statement is Java’s multi-way branch statement.
• provides an easy way to dispatch execution to different parts of your
code based on the value of an expression.
• provides a better alternative than a large series of if-else-if statements.
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
• The expression must be of type byte, short, int, or char.
• Each of the values specified in the case statements must be
of a type compatible with the expression.
• Each case value must be a unique literal (i.e. constant not
variable).
• Duplicate case values are not allowed.
• The value of the expression is compared with each of the
literal values in the case statements.
• If a match is found, the code sequence following that case
statement is executed.
• If none of the constants matches the value of the
expression, then the default statement is executed.
• The default statement is optional.
• If no case matches and no default is present, then no further
action is taken.
• The break statement is used inside the switch to terminate a
statement sequence.
• When a break statement is encountered, execution branches
to the first line of code that follows the entire switch
statement.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; 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:
Nested switch Statements
• When a switch is used as a part of the statement sequence of an
outer switch. This is called a nested switch.
switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2: // ...
Difference between ifs and switch
• switch can only test for equality, whereas if can evaluate any
type of Boolean expression. That is, the switch looks only for a
match between the value of the expression and one of its case
constants.
do {
// body of loop
} while (condition);
class Loop
{
public static void main(String args[])
{
for(int i=0; i<5; i++);
System.out.println (i++);
}
}
Declaring loop control variable inside loop
• We can declare the variable inside the initialization
portion of the for.
class var21
{
public static void main(String arr[]) {
int x, y;
for(x=0, y=5; x<=y; x++, y--) {
System.out.println("x= " + x);
System.out.println(“y = " + y);
}
}
}
• Initialization and iteration can be moved out from for loop.
Example 3:
class Loopchk
{
public static void main(String arr[])
{
for(int i=1, j=5; i>0 && j>2; i++, j--)
System.out.println("i is: "+ i + "and j is: "+j);
}
}
For-Each Version of the for Loop
• Example:
class Continue
{
public static void main(String args[])
{
for(int i=0; i<5; i++)
{
System.out.println(i + " ");
if (i%2 == 0) continue;
System.out.println("No Continue");
}
}
}
• Similar to break statement, Continue may specify a label to describe which enclosing
loop to continue.
Example:
class ContinueLabel
{
public static void main(String args[])
{
outer: for (int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
if(j > i)
{
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
return
• The return statement is used to explicitly return from a
method.
• It causes program control to transfer back to the caller of
the method.
if(t) statement is necessary. Without it, the Java compiler
Example: would flag an “unreachable code” error because the
class Return { compiler would know that the last println( ) statement
public static void main(String args[])
would never {
be executed.
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}