control structures a control structure is simply a pattern for controlling the flow of a program...

13
Control Structures • A control structure is simply a pattern for controlling the flow of a program module. • The three fundamental control structures of a structured programming language are sequence, selection, and iteration. • Sequence control structure is what you have been doing up until now. The second two is what we are going to take a look at next.

Upload: suzan-boyd

Post on 31-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Control Structures

• A control structure is simply a pattern for controlling the flow of a program module.

• The three fundamental control structures of a structured programming language are sequence, selection, and iteration.

• Sequence control structure is what you have been doing up until now. The second two is what we are going to take a look at next.

Page 2: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Selection/Iteration Structure

• Selection and Iteration allow the flow of the program to be altered, depending on one or more conditions.

• Selection is implemented by using a if, if/else, and switch statements.

• Iteration is implemented by using the while, do/while, and for statements.

Page 3: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

The IF statement

• The if statement works much the same as a if/then statement in Visual Basic.

• It uses relational operators to test if something is true or false.

• If it is true, the program will execute the statement(s) within the if statement.

• If it is false, the program will bypass the statements and continue with the statements following the if statement.

Page 4: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Syntax of the If Statement

if (test expression)

{

statement1;

statement2;

statement;

}

//this is an example of a compound statement

Page 5: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

if/else Statement

• The if/else statement works in the same manner as the if/then/else in Visual Basic.

• It is considered a double-alternative statement.

• If the expression evaluates as true, then the statements inside the if are executed.

• If the expression evaluates as false, then the statements inside the else are executed.

Page 6: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Syntax for if/else

if (test expression){

statement1;statement2;statement;

}else{

statement1;statement2;statement;

}//example of compound statements

Page 7: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Relational Operators

Relational operators allow two quantities to be compared.

Mathematical Symbol Java Meaning

= = = Equal to

! = Not equal to

< < Less than

< = Less than or equal

> > Greater than

> = Greater than or equal

Page 8: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Logical Operators

Java

Page 9: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Logical Operators

Page 10: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Example Switch Program• import TerminalIO.KeyboardReader;• public class ExampleSwitch• {• public static void main(String [] args)• {• KeyboardReader reader = new KeyboardReader();• int number;• number = reader.readInt("Please enter a number: ");• switch (number)• {• case 1: System.out.println("Your number is 1");• case 2:• case 3:• case 4:• case 5: System.out.println("Your number is between 2 and 5");• break;• case 6:• case 7:• case 8:• case 9:• case 10:System.out.println("Your number is between 6 and 10");• break;• default:System.out.println("Your number is not between 1 and 10");• }• }• }

Page 11: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Switch Statement

• The switch statement works in the same manner as the case select statement in Visual Basic.

• A selector variable is first evaluated to produce a value.

• The selector is then compared to a series of cases.

• If the selector value matches one of the case values, the corresponding case statements are executed.

Page 12: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Syntax for a Switch Statement

switch (selector variable){

case case1value : case1statements;break;

case case2value : case2statements;break;

case casenvalue : case_N_statements;break;

default : case exception statements;

}

Page 13: Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a

Escape Sequences

\b

\t

\n

\”

\’

\\

Backspace

Tab

Newline or line feed

Double Quote

Single Quote

Backslash