37
1 1

1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

Embed Size (px)

Citation preview

Page 1: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 1

Page 2: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 2

Chapter 4

Control Structures: Part I

Page 3: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 3

“ 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

Page 4: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 4

• Sequence: one thing after another

Structured Programming

task1

task2

task3

Page 5: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 5

• Selection: making choices

Structured Programming

taskA

taskB

?YES

NOStructured

programming,

only one entrance,

only one exit.

Page 6: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 6

• 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?

Page 7: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 7

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

Structured Programming

taskA

?TRUE

FALSE

Do until: will always do taskA at least once.“A Do Until loop repeats as long as a condition is false.”

Page 8: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 8

Structured Programming

Procedural Structured Programming

• Begin at the top, move to the bottom.

• Each program unit has only one entrance and only one exit.

Page 9: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 9

Selection in JavaObject Oriented Programming

• Within a method, procedural code.

• Simple ‘if’ with or without brackets.

if( expression )statement;

if( expression ){ statement;}

Page 10: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 10

Object Oriented Programming

• Simple ‘if’ with or without brackets.

if( expression )statement;

if( expression ){ statement;}

• Within brackets, a “block.”

Selection in Java

Page 11: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 11

Object Oriented Programming

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

if( expression )statement;

elsestatement;

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

Selection in Java

Page 12: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 12

w

Object Oriented Programming

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

if( expression ){

statement;statement;

}else

{statement;statement;

}

Selection in Java

Page 13: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 13

• Compound ‘if’ / ‘else if’ / ‘else if’ / ‘else’.

if( expression ){

statement;}

else if( expression ){

statement;}

else{

statement;}

Selection in Java

Page 14: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 14

• Special “Ternary” ? : Operator—shorthand ‘if’ / ‘else’

1.) expression must evaluate to True or False.

2.) If expression is True, execute the command before the colon.

3.) If expression is False, execute the command after the colon.

Do thisif expression

is True

Do this if expression

is False

Selection in Java

System.out.print(expression ? “True” : “False”)

Page 15: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 15

while—“the Do While”

• The Test is First

Repetition: while Part I

while( expression )statement;

Page 16: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 16

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

Page 17: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 17

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

Page 18: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 18

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.

Page 19: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 19

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

Page 20: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 20

// SelectionTest.javaimport javax.swing.*;

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

}

• Test Trinary Operator

Page 21: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 21

// DoWhileTest.javaimport javax.swing.*;

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

}

• Test DoWhile

Page 22: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 22

// DoUntilTest.javaimport javax.swing.*;

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

}

• Test DoUntil

Page 23: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 23

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.

Page 24: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 24

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.

Page 25: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 25

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,x contains 20.

First the addition on the right is done. Then, the result is assigned to the the variable on the left.

Page 26: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 26

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.

+=

Page 27: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 27

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.

-=

Page 28: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 28

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.

*=

Page 29: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 29

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.

/=

Page 30: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 30

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.

%=

Page 31: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 31

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

Page 32: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 32

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++

Page 33: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 33

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

Page 34: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 34

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.

Page 35: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 35

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.

Page 36: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 36

// PrePostIncrement.java

public class PrePostIncrement{

public static void main( String args[] ){ int x=0;

System.out.println( " Baseline, x = " + x );

System.out.println( "\n Pre-increment = ++x = " + ++x );

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

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

}

Baseline, x = 0

Pre-increment = ++x = 1

After increment, x = 1

2nd Baseline, x = 0

Post-increment = x++ = 0

After increment, x = 1

w

Page 37: 1 1. 1 2 Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds

1 37