control statements
TRANSCRIPT
![Page 1: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/1.jpg)
Chapter 4
Control Structures
http://www.java2all.com
![Page 2: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/2.jpg)
“ There is No goto in Java ”
• Structured programming: the building blocks
• There are 3 different kinds of operations in a program:
perform a sequence of actions,
perform a selection between alternative actions, or
perform a repetition or iteration of the same action.
Sequence, Selection, Iteration
Structured Programming
http://www.java2all.com
![Page 3: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/3.jpg)
• Sequence: one thing after another
Structured Programming
task1
task2
task3
http://www.java2all.com
![Page 4: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/4.jpg)
• Selection: making choices
Structured Programming
taskA
taskB
?YES
NOStructured
programming,
only one entrance,
only one exit.
http://www.java2all.com
![Page 5: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/5.jpg)
• Repetition, Part I: doing the same thing again until there’s a reason to stop.
Structured Programming
taskATRUE
FALSE
Do while: maybe won’t ever do taskA even once.
“A while loop repeats as long as a condition is true.”
expression?
http://www.java2all.com
![Page 6: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/6.jpg)
• Repetition, Part II: doing the same thing again until there’s a reason to stop.
Structured Programming
taskA
?TRUE
FALSEDo until: will always do taskA at least once.
“A Do Until loop repeats as long as a condition is false.”http://www.java2all.com
![Page 7: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/7.jpg)
Structured Programming
Procedural Structured Programming
• Begin at the top, move to the bottom.
• Each program unit has only one entrance and only one exit.
http://www.java2all.com
![Page 8: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/8.jpg)
Selection in JavaObject Oriented Programming
• Within a method, procedural code.
• Simple ‘if’ with or without brackets.
if( expression )statement;
if( expression ){ statement;}
http://www.java2all.com
![Page 9: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/9.jpg)
Object Oriented Programming
• Simple ‘if’ with or without brackets.
if( expression )statement;
if( expression ){ statement;}
• Within brackets, a “block.”
Selection in Java
http://www.java2all.com
![Page 10: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/10.jpg)
Object Oriented Programming
• Simple ‘if’ / ‘else’ without brackets.
if( expression )statement;
elsestatement;
• Without brackets, limit of only one statement per branch.
Selection in Java
http://www.java2all.com
![Page 11: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/11.jpg)
w
Object Oriented Programming
• Simple ‘if’ / ‘else’ with brackets.
if( expression ){
statement;statement;
}else
{statement;statement;
}
Selection in Java
http://www.java2all.com
![Page 12: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/12.jpg)
• Compound ‘if’ / ‘else if’ / ‘else if’ / ‘else’.
if( expression ){
statement;}
else if( expression ){
statement;}
else{
statement;}
Selection in Java
http://www.java2all.com
![Page 13: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/13.jpg)
If Statement Syntax• Decision Making
• The if exactly mirrors C/C++, and it has three variants:1.) if( expression )
statement;
2.) if( expression )statement;
else statement;
3.) if( expression ) statement;
else if( expression ) statement;
else statement;
http://www.java2all.com
![Page 14: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/14.jpg)
if( expression )statement;
if( expression ){ statement1;
statement2;
}
If Statement Syntax
• Simple If• The “expression” must be something that uses the
comparison operators and resolves to either true or false.
• The statement is executed if the expression is true.
• Only one statement can be made conditional without brackets. If you wish to conditionally execute more than
one statement, you use brackets to create a block.http://www.java2all.com
![Page 15: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/15.jpg)
if( expression )statement;else
statement;
If Statement Syntax
• Simple if/else• If the “expression” is true, the if branch executes, if not,
the else branch executes.
http://www.java2all.com
![Page 16: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/16.jpg)
if( expression ){
statement1;statement2;
} else{
statement3;statement4; }
If Statement Syntax
• Simple if/else• If the “expression” is true, the if branch executes, if not,
the else branch executes.
if( expression ){
statement1;statement2;} //end of if
else{statement3;
statement4;
} // end of else
Don’t bother to label the closing brackets unless you have a really long if.
Still you should always lineup your brackets.
http://www.java2all.com
![Page 17: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/17.jpg)
If Statement Syntax
if( expression )statement;else if( expression )
statement;else
statement;
• Compact if/else if/ else
• To prevent your nested ‘if’s from marching across the page, you can use this nested if. You can go on nesting
them as long as you like, and the last one is just an else.
http://www.java2all.com
![Page 18: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/18.jpg)
Multiple-Selection Structure
(switch selection statement)• Once you start nesting many ‘if’s, it becomes a nuisance.
• Java—like C and C++ before it—provides the switch structure, which provides multiple selections.
• Unfortunately—in contrast to Visual Basic’s Select Case and even COBOL’s Evaluate—you cannot use any of type of argument in the switch statement other than an integer.
http://www.java2all.com
![Page 19: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/19.jpg)
int x = 0;switch( x )
{case 1:
do stuff;break;
case 2:do stuff; break;
case 55:do stuff; break;
case 102:case 299:
do stuff okay for both; break;
default:if nothing else do this stuff; break;
}
Multiple-Selection Structure
• The integer expression x isevaluated. If x contains a 1,then the case 1 branch isperformed. Notice the‘break;’ statement. This is required. Without it,every line after the matchwill be executed until it
reaches a break;
w
http://www.java2all.com
![Page 20: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/20.jpg)
Multiple-Selection Structure
w
• The expression within the switch( expression )section must evaluate to an integer.• Actually, the expression can evaluate to any of these types (all numeric but long):
byteshortint char
but they will be reduced to an integer and that value will be used in the comparison.
http://www.java2all.com
![Page 21: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/21.jpg)
Multiple-Selection Structure
• The expression after each case statement can only be a
constant integral expression
—or any combination of character constants and integer constants that evaluate to a constant integer value.
http://www.java2all.com
![Page 22: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/22.jpg)
Multiple-Selection Structure
• The default: is optional.• If you omit the default choice, then it is possible for none of your choices to find a match and that nothing will be executed.
• If you omit the break; then the code for every choice after that—except the default!—will be executed.
http://www.java2all.com
![Page 23: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/23.jpg)
Multiple-Selection Structure
• Question: if only integer values can appear in the switch( x ) statement, then how is it possible for a char to be the expression?
http://www.java2all.com
![Page 24: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/24.jpg)
• Used when you know in advance how many times you want the loop to be executed.
4 Requirements:
1. Variable to count the number of repetitions
2. Starting value of counter
3. Amount in increment the counter each loop
4. The condition that decides when to stop looping.
Counter-Controlled Repetition
http://www.java2all.com
![Page 25: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/25.jpg)
• The for Loop
• A common structure called a for loop is specially designed to manage counter-controlled looping.
Counter-Controlled Repetition
for( int x = 1; x < 10; x++ )
1.) count variable,2.) starting value
3.) Increment
4.) condition, final value
http://www.java2all.com
![Page 26: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/26.jpg)
// ForCounter.javaimport java.awt.Graphics;import javax.swing.JApplet;
public class ForCounter extends JApplet{ public void paint( Graphics g ) { 1. 2. 4. 3.
for( int counter=1 ; counter <= 10 ; counter++ ){
g.drawLine( 10, 10, 250, counter * 10 ); }
}} 1.) count variable 2.) starting value 3.) increment 4.) condition, final value
• When appropriate, the for is quick and easy.
Counter-Controlled Repetition
http://www.java2all.com
![Page 27: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/27.jpg)
Counter-Controlled Repetition
• The for loop is a do-while.
• It tests the condition before it executes the loop for the first time.
( • Note: since the variable int counter was declared within the for , it vanishes after the for is finished. )
http://www.java2all.com
![Page 28: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/28.jpg)
counter <= 10?
int counter = 1;
{ }
counter++
TRUE
FALSE
1. 2.
3. body
4.
http://www.java2all.com
![Page 29: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/29.jpg)
• All Three Sections are Optional
• Effects of Omitting Sections: condition
Counter-Controlled Repetition
for( int x = 1; x < 10; x++ )
• If you omit the condition, Java assumes the statement is true, and you have an infinite loop.
for( int x = 1;; x++ )
http://www.java2all.com
![Page 30: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/30.jpg)
• Effects of Omitting Sections: initialization
Counter-Controlled Repetition
for( int x = 1; x < 10; x++ )
• You can omit the initialization if you have initialized the control variable someplace else.
int x = 1; for(; x < 10; x++ )
http://www.java2all.com
![Page 31: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/31.jpg)
• Effects of Omitting Sections: increment
Counter-Controlled Repetition
for( int x = 1; x < 10; x++ )
• You can omit the increment of the variable if you are doing so within the body of the loop.
for( int x = 1; x < 10;){
other stuff
x++;}
http://www.java2all.com
![Page 32: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/32.jpg)
• Can Use the while Loop
• Although the while is usually used when we don’t know how many times we’re going to loop, it works just fine.
• Still must supply the 4 Requirements.
Counter-Controlled Repetition
http://www.java2all.com
![Page 33: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/33.jpg)
while—“the Do While”
• The Test is First
Repetition: while Part I
while( expression )statement;
http://www.java2all.com
![Page 34: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/34.jpg)
while—“the Do While”
• The Test is First
Repetition: while Part I
while( expression ){
statement;statement;
}
The while { “Do While” } is used when you can’t predict exactly how many times your
loop will be executed.
The while may not be executed even once. It executes the loop while the expression is
still true.
w
http://www.java2all.com
![Page 35: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/35.jpg)
// WhileTest.java// Since "c" is already false when it reaches the// test, the loop never executes.
public class DoWhile{ public static void main( String args[] ) { boolean c = false
while( c ) {
System.out.println( ”Execute DoWhile while c is true" ); }
System.exit( 0 ); }}
http://www.java2all.com
![Page 36: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/36.jpg)
while—“the Do Until”
• The Test is Last
Repetition: while Part II
do{
statement;statement;
}
while( expression );
w
http://www.java2all.com
![Page 37: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/37.jpg)
while—“the Do Until”
• The Test is Last
Repetition: while Part II
do{
statement;statement;
}
while( expression );
w
This do/while {“Do Until”} is also used when you can’t predict exactly how many times
your loop will be executed.
It executes at least once. It executes Until the expression becomes
false.
http://www.java2all.com
![Page 38: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/38.jpg)
// DoUntil.java// Even though "c" begins the loop false,// it still executes at least once.
public class DoUntil{ public static void main( String args[] ) { boolean c = false; do { System.out.println( ”Execute DoUntil at least once " ); } while( c ); System.exit( 0 ); }}
http://www.java2all.com
![Page 39: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/39.jpg)
Statements break; and continue;
• Both of these statements alter the flow of control.
• The break statement can be executed in a:
whiledo/whileforswitch
• break causes the immediate exit from the structure
http://www.java2all.com
![Page 40: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/40.jpg)
Statements break; and continue; • After a break exits the “structure”—whatever that is—execution resumes with the first statement following the structure.
• If you have nested structures—be they a while, do/while/ for or switch—the break will only exit the innermost nesting.
• break will not exit you out of all nests. To do that, you need another break*
* There is a variant of the break called a labeled break—but this is similar to a goto and is frowned upon.
http://www.java2all.com
![Page 41: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/41.jpg)
Statements break; and continue;
• The continue statement, when used in a while or do/while or a for, skips the remaining code in the structure and returns up to the condition.
• If the condition permits it, the next iteration of the loop is permitted to continue.
• So, the continue is a “temporary break.”
• The continue is only used in iterative structures, such as the while, do/while and for.
http://www.java2all.com
![Page 42: Control statements](https://reader034.vdocuments.us/reader034/viewer/2022042521/555a5fe7d8b42a47748b52a1/html5/thumbnails/42.jpg)
Statements break; and continue;
• The “Labeled” continue and break statements send execution to the label to continue execution.• Note: using the labeled break and continue is bad code. Avoid using them!
stop:
for(row = 1; row <= 10; row++){ for(col=1; col <=5; col++) { if( row == 5) {
break stop; // jump to stop block }
output += “* “; } output += “\n”;}
http://www.java2all.com