You are on page 1of 37

Java I--Copyright © 2000-2007 Tom Hunter

Chapter 4
Control Structures: Part I

Java I--Copyright © 2000-2007 Tom Hunter


Structured Programming
“ 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

Java I--Copyright © 2000-2007 Tom Hunter


Structured Programming
• Sequence: one thing after another

task1

task2

task3

Java I--Copyright © 2000-2007 Tom Hunter


Structured Programming
• Selection: making choices

YES
? taskA

Structured NO
programming,
only one entrance, taskB
only one exit.

Java I--Copyright © 2000-2007 Tom Hunter


Structured Programming
• Repetition, Part I: doing the same thing again until
there’s a reason to stop.

TRUE
expression taskA
?
FALSE

Do while: maybe won’t ever do taskA even once.


“A while loop repeats as long as a condition is true.”
Java I--Copyright © 2000-2007 Tom Hunter
Structured Programming
• Repetition, Part II: doing the same thing again until
there’s a reason to stop.

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

Procedural Structured Programming


• Begin at the top, move to the bottom.
• Each program unit has only one entrance and
only one exit.

Java I--Copyright © 2000-2007 Tom Hunter


Selection in Java
Object Oriented Programming
• Within a method, procedural code.
• Simple ‘if’ with or without brackets.

if( expression )
statement;

if( expression )
{
statement;
}

Java I--Copyright © 2000-2007 Tom Hunter


Selection in Java
Object Oriented Programming

• Simple ‘if’ with or without brackets.

if( expression )
statement;

if( expression )
{
statement; • Within brackets,
} a “block.”

Java I--Copyright © 2000-2007 Tom Hunter


Selection in Java
Object Oriented Programming
• Simple ‘if’ / ‘else’ without brackets.
if( expression )
statement;
else
statement;

• Without brackets, limit of only one statement per branch.

Java I--Copyright © 2000-2007 Tom Hunter


Selection in Java w

Object Oriented Programming


• Simple ‘if’ / ‘else’ with brackets.

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”)

1.) expression must


evaluate to True or False.
Do this if
2.) If expression is True,
expression
execute the command before
is False
the colon.
Do this
3.) If expression is False, if expression
execute the command after the is True
colon.
Java I--Copyright © 2000-2007 Tom Hunter
Repetition: while Part I
while—“the Do While”
• The Test is First

while( expression )
statement;

Java I--Copyright © 2000-2007 Tom Hunter


Repetition: while Partw I
while—“the Do While”
• The Test is First

The while { “Do While” } is used when you can’t predict


exactly how
while( many times your
expression ) loop will be executed.
{
statement;
The while may not be executed even once.
It statement;
executes the loop while the expression is still true.
}
Java I--Copyright © 2000-2007 Tom Hunter
// 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 );
}
}

Java I--Copyright © 2000-2007 Tom Hunter


Repetition: while Part II w

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

Java I--Copyright © 2000-2007 Tom Hunter


// 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 );
}
}

Java I--Copyright © 2000-2007 Tom Hunter


// SelectionTest.java
import javax.swing.*;
• Test Ternary Operator
public class SelectionTest
{
public static void main( String args[] )
{
int b, s;
String big, small, out;

big = JOptionPane.showInputDialog( "Big Number" );


small = JOptionPane.showInputDialog( "Small Number" );
b = Integer.parseInt( big );
s = Integer.parseInt( small );

out = ( b < s ? "Big was larger" : "Small was larger" );

JOptionPane.showMessageDialog( null, out, "Results",


JOptionPane.INFORMATION_MESSAGE);

System.exit( 0 );
}
}

Java I--Copyright © 2000-2007 Tom Hunter


// DoWhileTest.java
import javax.swing.*;
• Test DoWhile
public class DoWhileTest
{
public static void main( String args[] )
{
int b = 2, s = 1;
String big, small, out = “Big is still Bigger”;

while( b < s )
{
JOptionPane.showMessageDialog( null,
out,
"Results",
JOptionPane.INFORMATION_MESSAGE);

big = JOptionPane.showInputDialog( "Big Number" );


small = JOptionPane.showInputDialog( "Small Number" );
b = Integer.parseInt( big );
s = Integer.parseInt( small );
}

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);

big = JOptionPane.showInputDialog( "Big Number" );


small = JOptionPane.showInputDialog( "Small Number" );
b = Integer.parseInt( big );
s = Integer.parseInt( small );
}
while( b < s );

System.exit( 0 );
Java I--Copyright © 2000-2007 Tom Hunter
Repetition: while Loops

• The majority of applications use the plain while loop.

• Choose either while loop when you can’t know in


advance how many times the loop will be executed.

• The loop is repeated until it encounters a sentinel value,


that announces that the loop has finished.

Java I--Copyright © 2000-2007 Tom Hunter


Assignment Operators
• We are already familiar with this statement:
int x;
x = 15;
• This means the value 15 is placed into the variable x.
• We say, “15 is assigned to x.”
• In Java, a single equals sign is the assignment operator.

Java I--Copyright © 2000-2007 Tom Hunter


Assignment Operators

• Another common bit of code is this:


int x; Declares x as an int.

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;

After this assignment,


x contains 20.

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 10.

x -= 5;

After this assignment,


x contains 10.

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 75.

x *= 5;

After this assignment,


x contains 75.

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 3.

x /= 5;

After this assignment,


x contains 3.

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 0.

x %= 5;

After this assignment,


x contains 0.

Java I--Copyright © 2000-2007 Tom Hunter


Increment/Decrement Operators
• There is one addition statement in Java that is so
common, it gets its own operator.
• It means, simply, “Add one to the variable.”

x++ x--

Java I--Copyright © 2000-2007 Tom Hunter


Increment/Decrement Operators

• Java offers a shortcut to the statement below:


int x = 2;

x = x + 1;
After this assignment,
x contains 3.

x++;

After this assignment,


x contains 3.
x++
Java I--Copyright © 2000-2007 Tom Hunter
Increment/Decrement Operators

• Java offers a shortcut to the statement below:


int x = 2;

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

• You can do a pre-increment, or a post-increment.


Pre Post
++x; x++;

--x; x--;

• If each of these statements is on a line by itself, there is


no difference in the effect of doing a pre- or post-
increment.

Java I--Copyright © 2000-2007 Tom Hunter


Increment/Decrement Operators

• If each of these statements is on a line by itself, there is


no difference in the effect of doing a pre- or post-
increment.

• However, if the variable—which is having the pre- or


post-increment applied to it—is used within another
statement, your choice of pre- or post-increment can alter
your results.

Java I--Copyright © 2000-2007 Tom Hunter


// PrePostIncrement.java
w
public class PrePostIncrement
Baseline, x = 0
{
public static void main( String args[] )
{ Pre-increment = ++x = 1
int x=0;
After increment, x = 1
System.out.println( " Baseline, x = " + x );
2nd Baseline, x = 0
System.out.println( "\n Pre-increment = ++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.out.println( "\n Post-increment = x++ = " + x++ );

System.out.println( "\n After increment, x = " + x );

System.exit( 0 );
} Java I--Copyright © 2000-2007 Tom Hunter
Java I--Copyright © 2000-2007 Tom Hunter

You might also like