Title: Control Structures
1Control Structures
There are 3 control structures for execution flow
in any programming language 1. Sequential 2.
Selection 3. Repetition
21. Sequential flow occurs when statements are in
a compound statement or block, identified by
ex. int num 0 String word
answer num num 2
System.out.println(word num)
32. A selection control structure or decision
statement is one that chooses among alternative
statements which will be executed.
The assertion must be a Boolean Expression which
the computer evaluates to true or false.
4The Boolean expression may be 1. A Boolean
variable 2. An expression containing a relational
operator
- binary operators requiring two operands
53. An expression containing Boolean operators
or both relational Boolean operators
6Example of an assertion
7Truth table for a disjunction OR statement
OR T F T F
T T T
F
8Truth table for a conjunction AND statement
AND T F T F
T
F F F
9Boolean Operator (NOT)
!leapYear
leapYear false
Equivalent to
10method Call type cast !, ,_ (unary) , /, ,
- lt, lt, gt, gt , !
Precedence of Operators
11Using Boolean assignment statements
boolean same true boolean x
false boolean y true
System.out.println("1.a) x " x)
System.out.println("1.b) y " y)
System.out.println("1.c) same " same)
same (x y) System.out.println("2.0)
same " same)
12Java uses Short Circuit Evaluation
Ex.1 x 0 y 10 z 2 (x
! 0.0) ( y / x gt 5.0) since first expression
is false, 2nd expression is not evaluated.
Ex.2 flag true flag ((y - z)
gt (y - x)) since first expression is true, 2nd
expression is not evaluated.
13The if statement is the primary selection control
statement
When condition is true, statement is executed,
otherwise control goes to next sequential
instruction.
Single alternative form if (condition)
statement
Double alternative form if (condition)
statement1 else statement2
When condition is true, statement1 is executed,
otherwise statement2 is executed.