Upload
joseph-appiah-jnr
View
242
Download
0
Embed Size (px)
Citation preview
8/8/2019 L04 - Control Structures
1/33
Lecture 4
Control Structures
MIT AITI 2004
8/8/2019 L04 - Control Structures
2/33
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 patha program takes
Control structures include:
block statements
decision statements
loops
8/8/2019 L04 - Control Structures
3/33
Decision St
atem
ents
8/8/2019 L04 - Control Structures
4/33
If Statements
The if decision statement executes a statementconditionally
if (expression) {statement;
}
next_statement;
The expression must produce aboolean value, eithertrue orfalse
If expression returns true, statement is executed andthen next_statement
Ifexpression
returnsfalse
,statement
is not executedand the program continues at next_statement
8/8/2019 L04 - Control Structures
5/33
executestatement
executenext_statement
expression istrue?
if (expression)statement
next_statementyes
no
8/8/2019 L04 - Control Structures
6/33
If-Else Statements
The basic if statement can be extended by adding theelse clauseif (expression) {statement1;
}else{statement2;
}next_statement;
Again, the expression must produce aboolean value
Ifexpression returns true, statement1 is executeda
ndthen next_statement is executed.
Ifexpression returns false, statement2 is executed andthen next_statement is executed.
8/8/2019 L04 - Control Structures
7/33
expression istrue?
executestatement1
executestatement2
executenext_statement
if (expression){statement1
} else {statement2
}next_statement
noyes
8/8/2019 L04 - Control Structures
8/33
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.");
elseSystem.out.println("You got an F.");
8/8/2019 L04 - Control Structures
9/33
Switch Statements
The switch statement enables you to test severalcases generated by a given expression. The expression must produce a result of type char,byte, short or int, but not long, float, ordouble.
For example:switch (expression) {
case value1:statement1;
case value2:
statement2;default:
default_statement;}
Every statement after the true case is executed
8/8/2019 L04 - Control Structures
10/33
y
y
n
n
switch (expression){
case value1:
// Do value1 thing
case value2:
// Do value2 thing
...
default:
// Do default action
}
// Continue the program
expression
equa
lsvalue1? Do value1 thing
Do value2 thing
Do default action
expressionequals
value2?
Continue the
program
8/8/2019 L04 - Control Structures
11/33
Break Statements in Switch Statements
The break; statement tells the computer to exit theswitch statement
For example:switch (expression) {
case value1:statement1;break;
case value2:statement2;
break;
default:default_statement;break;
}
8/8/2019 L04 - Control Structures
12/33
expression
equalsvalue1?
expression
equalsvalue2?
Dodefault action
Do value1 t
h
ing
Do value2 thing
break
break
break
Continue theprogram
switch (expression){
case value1:
// Do value1 thing
break;
case value2:
// Do value2 thing
break;
...default:
// Do default actionbreak;
}
// Continue the program
y
y
n
n
8/8/2019 L04 - Control Structures
13/33
Remember the Example
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.");
8/8/2019 L04 - Control Structures
14/33
Another way to do this same example is to use the
switch statement
Complicated if-else chains can be rewritten with theswitch 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.");}
8/8/2019 L04 - Control Structures
15/33
Loops
8/8/2019 L04 - Control Structures
16/33
A loop allows you to execute a statement or block of
statements repeatedly.
There are three types of loops in Java:
1. for loops
2. while loops
3. Do-while loops (will not discuss in th
is course)
8/8/2019 L04 - Control Structures
17/33
The for Loopfor (initialization_expression;
loop_condition;increment_expression){//statement
}
The control of the for loop appear in parentheses and is made up ofthree parts.
1. The first part, the initialization_expression, sets theinitial conditions for the loop and is executed before the loop starts.
2. Loop executes so long as the loop_condition is true and exitsotherwise
3. The third part of the control information, theincrement_expression, is usually used to increment the loopcounter. This is executed at the end of each loop iteration.
8/8/2019 L04 - Control Structures
18/33
For example:int limit = 5;
int sum = 0;
for(int i = 1; i
8/8/2019 L04 - Control Structures
19/33
Another example:
for(int div = 0; div
8/8/2019 L04 - Control Structures
20/33
If there is more than one variable to set up or incrementthey are separated by a comma.
for(i=0, j=0; i*j
8/8/2019 L04 - Control Structures
21/33
The while Loop
while (expression){statement
}
This while loop executes as long as the given logical
expression between parentheses is true. Whenexpression is false, execution continues with the
statement following the loop block.
The expression is tested at the beginning of the loop, soif it is initially
false, the loop will not be executed at all.
8/8/2019 L04 - Control Structures
22/33
For example:
int limit = 7;
int sum = 0;int i = 1;
while (i < limit){
sum += i;i++;
}
Wha
t is theva
lue of sum ?21
8/8/2019 L04 - Control Structures
23/33
Test condition
is true?
Execute loop
statement(?)
Next statement
The while loop
y
n
Initialize count
Test condition
is true?
Execute loop
statement(s)
Increment
count
New statement
The for loop
y
n
8/8/2019 L04 - Control Structures
24/33
The continue Statement
The continue statement causes the loop to exit its currenttrip through the loop and start overat the first statement ofthe loop. Here are two examples:
Example 1:
int index = 0;while(index
8/8/2019 L04 - Control Structures
25/33
Example 2:
int sum = 0;for(int i=1; i
8/8/2019 L04 - Control Structures
26/33
Using the break Statement in Loops
We have seen the use of the break statement in the switchstatement.
In loops, you can use the break statement to exit the current
loop you are in. Here is an example:
int index = 0;
while (index
8/8/2019 L04 - Control Structures
27/33
Nested Loops
You can nest loops ofany kind one inside another to anydepth. Here is a example:
int totalCount = 0;
while(totalCount
8/8/2019 L04 - Control Structures
28/33
Final example:
for (int i = 1; i
8/8/2019 L04 - Control Structures
29/33
POP QUIZ
1. In the switch statement expression must producea result of what type?
2. What must be used to separate each section ofafor
statement.
3. Which statement causes a program to go back to thestatement that began a loop and then keep going fromthere.
4.W
ritea
for loop tha
t outputs 1001 in re
versesequence.
5. Write afor loop that outputs all numbers that aredivisible by 3 between 0-50.
char, byte, short, int
8/8/2019 L04 - Control Structures
30/33
POP QUIZ
1. In the switch statement expression must producea result of what type?
2. What must be used to separate each section ofafor
sta
tem
ent.
3. Which statement causes a program to go back to thestatement that began a loop and then keep going fromthere.
4. Write afor loop that outputs 1001 in reversesequence.
5. Write afor loop that outputs all numbers that aredivisible by 3 between 0-50.
char, byte, short, int
; (semi-colon)
8/8/2019 L04 - Control Structures
31/33
POP QUIZ
In the switch statement expression must produce aresult of what type?
What must be used to separate each section ofa for
statement.
Which statement causes a program to go back to thestatement that began a loop and then keep going fromthere.
W
ritea
for loop tha
t outputs 1001 in re
versesequence.
Write a for loop that outputs all numbers that aredivisible by 3 between 0-50.
char, byte, short, int
; (semi-colon)
continue
8/8/2019 L04 - Control Structures
32/33
POP QUIZ
In the switch statement expression must produce aresult of what type?
What must be used to separate each section ofa for
statement.
Which statement causes a program to go back to thestatement that began a loop and then keep going fromthere.
W
ritea
for loop tha
t outputs 1001 in re
versesequence.
Write a for loop that outputs all numbers that aredivisible by 3 between 0-50.
char, byte, short, int
for(int i=100; i>=0;i--) {
System.out.println(i);}
; (semi-colon)
continue
8/8/2019 L04 - Control Structures
33/33
POP QUIZ
In the switch statement expression must produce aresult of what type?
What must be used to separate each section ofa for
statement.
Which statement causes a program to go back to thestatement that began a loop and then keep going fromthere.
W
ritea
for loop tha
t outputs 1001 in re
versesequence.
Write a for loop that outputs all numbers that aredivisible by 3 between 0-50.
char, byte, short, int
for(int i=100; i>=0;i--) {
System.out.println(i);}
; (semi-colon)
continue
for(int i=0;i