L04 - Control Structures
L04 - Control Structures
Control Structures
MIT – AITI 2004
What are Control Structures?
• Java will execute your code in a specific sequence
• Control structures are a way to alter the natural
sequence of execution in a Java program
• In other words, they act as “direction signals” to control
the path a program takes
• Control structures include:
• block statements
• decision statements
• loops
Decision Statements
If Statements
• The “if” decision statement executes a statement
conditionally
if (expression) {
statement;
}
next_statement;
if (expression)
statement yes
next_statement
execute
statement
execute
next_statement
If-Else Statements
• The basic “if” statement can be extended by adding the
“else” clause
if (expression) {
statement1;
}
else{
statement2;
}
next_statement;
if (expression){
statement1
} else {
statement2 execute execute
} statement1 statement2
next_statement
execute
next_statement
• Here is an example of chained if-else statements:
if (grade == 'A')
System.out.println("You got an A.");
else if (grade == 'B')
System.out.println("You got a B.");
else if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");
Switch Statements
• The switch statement enables you to test several
cases generated by a given expression.
• The expression must produce a result of type char,
byte, short or int, but not long, float, or
double.
• For example:
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed
expression y
equals Do value1 thing
value1?
switch (expression){
case value1:
// Do value1 thing n
case value2:
// Do value2 thing
expression y
equals Do value2 thing
...
default: value2?
// Do default action
}
// Continue the program n
Do default action
Continue the
program
Break Statements in Switch Statements
• The break; statement tells the computer to exit the
switch statement
• For example:
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
default:
default_statement;
break;
}
expression y
equals Do value1 thing break
value1?
switch (expression){
case value1:
// Do value1 thing
break; n
case value2:
// Do value2 thing
break; expression y
equals Do value2 thing break
...
value2?
default:
// Do default action
break;
}
n
// Continue the program
Do default action
if (grade == 'A')
System.out.println("You got an A.");
else if (grade == 'B')
System.out.println("You got a B.");
else if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");
• Another way to do this same example is to use the
switch statement
• Complicated if-else chains can be rewritten with the
switch statement
switch (grade) {
case 'A':
System.out.println("You got an A.");
break;
case 'B':
System.out.println("You got a B.");
break;
case 'C':
System.out.println("You got a C.");
break;
default:
System.out.println("You got an F.");
}
Loops
• A loop allows you to execute a statement or block of
statements repeatedly.
/* initialization_expression
loop_condition
increment_expression */
sum += 2;
}
if(div % 12 == 0){
System.out.println(div+"is divisible
by 12");
}
}
System.out.println(i+"*"+j+"="+i*j);
}
• You do not have to fill every part of the control of the for
loop but you must still have two semi-colons.
int limit = 7;
int sum = 0;
int i = 1;
Test condition n
is true?
n
Test condition
is true?
y
Execute loop
y statement(?)
Execute loop
statement(s)
Next statement
Increment
count
New statement
The continue Statement
• The continue statement causes the loop to exit its current
“trip” through the loop and start over at the first statement of
the loop. Here are two examples:
Example 1:
int index = 0; The index is 1
int sum = 0;
for(int i=1; i<=6; i++){
if(i%3==0){
continue;
}
sum += i;
}
index++;
if(index==3)
break;
System.out.println("The index is"+index);
}
Nested Loops
• You can nest loops of any kind one inside another to any
depth. Here is a example:
int totalCount = 0;
while(totalCount<20){
for(int i = 0; i < 2; i++){
totalCount += i;
}
}
System.out.println(totalCount);
20
• Final example: