control structures a control structure is simply a pattern for controlling the flow of a program...
TRANSCRIPT
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.
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.
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.
Syntax of the If Statement
if (test expression)
{
statement1;
statement2;
statement;
}
//this is an example of a compound statement
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.
Syntax for if/else
if (test expression){
statement1;statement2;statement;
}else{
statement1;statement2;statement;
}//example of compound statements
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
Logical Operators
Java
Logical Operators
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");• }• }• }
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.
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;
}
Escape Sequences
\b
\t
\n
\”
\’
\\
Backspace
Tab
Newline or line feed
Double Quote
Single Quote
Backslash