Professional Documents
Culture Documents
4 Control Structures Part I
4 Control Structures Part I
Chapter 4
Control Structures: Part I
task1
task2
task3
YES
? taskA
Structured NO
programming,
only one entrance, taskB
only one exit.
TRUE
expression taskA
?
FALSE
taskA
TRUE
?
FALSE
Do until: will always do taskA at least once.
“A Do Until loop repeats as long as a condition is false.”
Java I--Copyright © 2000-2007 Tom Hunter
Structured Programming
if( expression )
statement;
if( expression )
{
statement;
}
if( expression )
statement;
if( expression )
{
statement; • Within brackets,
} a “block.”
if( expression )
{
statement;
statement;
}
else
{
statement;
statement;
}
Java I--Copyright © 2000-2007 Tom Hunter
Selection in Java
• Compound ‘if’ / ‘else if’ / ‘else if’ / ‘else’.
if( expression )
{
statement;
}
else if( expression )
{
statement;
}
else
{
statement;
}
Java I--Copyright © 2000-2007 Tom Hunter
Selection in Java
• Special “Ternary” ? : Operator—shorthand ‘if’ / ‘else’
System.out.print(expression ? “True” : “False”)
while( expression )
statement;
do
{
System.out.println( ”Execute DoUntil at least once " );
}
while( c );
System.exit( 0 );
}
}
while—“the Do Until”
• The Test is Last
do
{
This do/while {“Do Until”} is also used when you can’t
statement;
predict exactly how many times your loop will be executed.
statement;
} It executes at least once.
It while(
executes Until );becomes false.
the expression
expression
while( c )
{
System.out.println( ”Execute DoWhile while c is true" );
}
System.exit( 0 );
}
}
System.exit( 0 );
}
}
while( b < s )
{
JOptionPane.showMessageDialog( null,
out,
"Results",
JOptionPane.INFORMATION_MESSAGE);
System.exit( 0 );
}
} Java I--Copyright © 2000-2007 Tom Hunter
// DoUntilTest.java
import javax.swing.*;
• Test DoUntil
public class DoUntilTest
{
public static void main( String args[] )
{
int b = 2, s = 1; // preload variables.
String big, small, out = “Big is still Bigger”;
do
{
JOptionPane.showMessageDialog( null,
out,
"Results",
JOptionPane.INFORMATION_MESSAGE);
System.exit( 0 );
Java I--Copyright © 2000-2007 Tom Hunter
Repetition: while Loops
x = 15;
After this assignment,
x contains 15.
x = x + 5;
After this assignment,
First the addition on the
x contains
right 20. the
is done. Then,
result is assigned to the the
variable on the left.
Java I--Copyright © 2000-2007 Tom Hunter
Assignment Operators
• Java offers a shortcut to the statement below:
+=
int x = 15;
x = x + 5;
After this assignment,
x contains 20.
x += 5;
x = x - 5;
After this assignment,
x contains 10.
x -= 5;
x = x * 5;
After this assignment,
x contains 75.
x *= 5;
x = x / 5;
After this assignment,
x contains 3.
x /= 5;
x = x % 5;
After this assignment,
x contains 0.
x %= 5;
x++ x--
x = x + 1;
After this assignment,
x contains 3.
x++;
x = x - 1;
After this assignment,
x contains 1.
x--;
After this assignment,
x contains 1.
x--
Java I--Copyright © 2000-2007 Tom Hunter
Increment/Decrement Operators
--x; x--;
Post-increment = x++ = 0
System.out.println( "\n After increment, x = " + x );
After increment, x = 1
x = 0;
System.out.println( "\n 2nd Baseline, x = " + x );
System.exit( 0 );
} Java I--Copyright © 2000-2007 Tom Hunter
Java I--Copyright © 2000-2007 Tom Hunter