control structures. program flow java will execute the statements in your code in a specific...
TRANSCRIPT
![Page 1: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/1.jpg)
Control Structures
![Page 2: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/2.jpg)
Program Flow• Java will execute the
statements in your code in a specific sequence, or "flow".
• The "flow" of the program and can be described through a "flow diagram":
a more sophisticated program
a simple program
statement
statement
statement
statement
statement
statement
statement statement
statement
statement
![Page 3: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/3.jpg)
What are Control Structures?
• Control structures alter the flow of the program, the sequence of statements that are executed in a program.
• They act as "direction signals" to control the path a program takes.
• Two types of control structures in Java:– decision statements– loops
![Page 4: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/4.jpg)
Decision Statements
• A decision statement allows the code to execute a statement or block of statements conditionally.
• Two types of decisions statements in Java:– if statements– switch statements
![Page 5: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/5.jpg)
If Statement
if (expression) { statement;}rest_of_program;
• expression must evaluate to a boolean value, either true or false
• If expression is true, statement is executed and then rest_of_program
• If expression is false, statement is not executed and the program continues at rest_of_program
![Page 6: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/6.jpg)
If Statement Flow Diagram
The if decision statement executes a statement if an expression is true
execute statement
execute rest_of_program
Is expressiontrue?
yes
no
if (expression) { statement1;}rest_of_program
![Page 7: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/7.jpg)
If-Else Statementif (expression) { statement1;}else{ statement2;}next_statement;
• Again, expression must produce a boolean value
• If expression is true, statement1 is executed and then next_statement is executed.
• If expression is false, statement2 is executed and then next_statement is executed.
![Page 8: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/8.jpg)
If-Else Flow Diagram
if (expression){ statement1;} else { statement2;}rest_of_program
The if-else decision statement executes a statement if an expression is true and a different statement if it is not true.
is “expression”
true?
execute statement1
execute rest_of_program
noyes
execute statement2
![Page 9: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/9.jpg)
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.");
![Page 10: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/10.jpg)
Switch Statements• The switch statement enables you to test several cases
generated by a given expression.
• For example:switch (expression) {
case value1: statement1;
case value2: statement2;
default: default_statement;}
Every statement after the true case is executed
• The expression must evaluate to a char, byte, short or int, but not long, float, or double.
![Page 11: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/11.jpg)
y
y
n
n
switch (expression){ case value1:
// Do value1 thing
case value2:// Do value2 thing
... default:
// Do default action}// Continue the program
expressionequalsvalue1?
Do value1 thing
Do value2 thing
Do default action
expressionequalsvalue2?
Continue theprogram
![Page 12: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/12.jpg)
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; }
![Page 13: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/13.jpg)
switch (expression){ case value1: // Do value1 thing break;
case value2: // Do value2 thing break;
... default: // Do default action break;}// Continue the program
expressionequals
value1?
expressionequals
value2?
do default action
Do value1 thing
Do value2 thing
break
break
break
Continue the program
y
y
n
n
![Page 14: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/14.jpg)
Remember the Chained If-Else . . .
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.");
![Page 15: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/15.jpg)
• This is how it is accomplished with a switch:
• if-else chains can be sometimes be rewritten as a “switch” statement.
• switches are usually simpler and faster
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.");
}
![Page 16: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/16.jpg)
Loops
• A loop allows you to execute a statement or block of statements repeatedly.
• Three types of loops in Java: 1. while loops
2. for loops
3. do-while loops (not discussed in this course)
![Page 17: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/17.jpg)
The while Loop
while (expression){statement
}
• This while loop executes as long as the given logical expression between parentheses is true. When expression is false, execution continues with the statement following the loop block.
• The expression is tested at the beginning of the loop, so if it is initially false, the loop will not be executed at all.
![Page 18: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/18.jpg)
• For example:
int sum = 0; int i = 1;
while (i <= 10){ sum += i; i++; }
• What is the value of sum?
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
![Page 19: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/19.jpg)
The for Loop
The control of the for loop appear in parentheses and is made up of three parts:
1. The first part, the init_expression,sets the initial conditions for the loop and is executed before the loop starts.
2. Loop executes so long as the loop_condition is true and exits otherwise.
3. The third part of the control information, the increment_expr, is usually used to increment the loop counter. This is executed at the end of each loop iteration.
for (init_expr; loop_condition; increment_expr) { statement;}
![Page 20: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/20.jpg)
• For example:
int sum = 0;
for (int i = 1; i <= 10; i++) { sum += i; }
• What is the value of sum?
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
![Page 21: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/21.jpg)
• Example 3:
for(int div = 0; div < 1000; div++){ if(div % 2 == 0) { System.out.println("even: " + div); } else { System.out.println("odd: " + div); } }
• What will this for loop do?
prints out each integer from 0 to 999,correctly labeling them even or odd
![Page 22: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/22.jpg)
• If there is more than one variable to set up or increment they are separated by a comma.
for(i=0, j=0; i*j < 100; i++, j+=2) { System.out.println(i * j);
}
• You do not have to fill all three control expressions but you must still have two semicolons.
int n = 0; for(; n <= 100;) { System.out.println(++n);
}
![Page 23: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/23.jpg)
The while loop
Test conditionis true?
Execute loopstatement(?)
Next statement
y
n
Initialize count
Test conditionis true?
Execute loop statement(s)
Increment count
New statement
The for loop
y
n
![Page 24: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/24.jpg)
The continue Statement
• The continue statement causes the program to jump to the next iteration of the loop.
/** * prints out "5689" */ for(int m = 5; m < 10; m++) { if(m == 7) { continue; } System.out.print(m); }
![Page 25: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/25.jpg)
• Another continue example:
int sum = 0; for(int i = 1; i <= 10; i++){ if(i % 3 == 0) {
continue; } sum += i;
}
• What is the value of sum?
1 + 2 + 4 + 5 + 7 + 8 + 10 = 37
![Page 26: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/26.jpg)
The break Statement
• We have seen the use of the break statement in the switch statement.
• You can also use the break statement to exit the loop entirely.
// prints out numbers unless // num is ever exactly 400 while (num > 6) { if(num == 400) { break; } System.out.println(num); num -= 8; }
![Page 27: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/27.jpg)
Nested Loops• You can nest loops of any kind one inside
another to any depth. What does this print?
for(int i = 10; i > 0; i--) {
if (i > 7) {
continue;
}
while (i > 3) {
if(i == 5) {
break;
}
System.out.println(--i);
}
System.out.println(i);
}
6554321
![Page 28: Control Structures. Program Flow Java will execute the statements in your code in a specific sequence, or "flow". The "flow" of the program and can be](https://reader036.vdocuments.net/reader036/viewer/2022081414/55142bc85503466d1a8b4d79/html5/thumbnails/28.jpg)
POP QUIZ
1. In the switch statement, which types can expression evaluate to?
2. What must be used to separate each section of a for statement.
3. Which statement causes a program to skip to the next iteration of a loop.
4. Write a for loop that outputs 100-1 in reverse sequence.
5. Write a for loop that outputs all numbers that are divisible by 3 between 0-50.
char, byte, short, int
semicolons
continue