Control Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Control Structures

Description:

Control Structures. There are 3 control structures for execution flow in any ... Truth table for a disjunction: OR statement. OR T F. T. F. T T. T. F ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 14
Provided by: tcnj
Learn more at: https://owd.tcnj.edu
Category:

less

Transcript and Presenter's Notes

Title: Control Structures


1
Control Structures
There are 3 control structures for execution flow
in any programming language 1. Sequential 2.
Selection 3. Repetition
2
1. 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)
3
2. 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.
4
The Boolean expression may be 1. A Boolean
variable 2. An expression containing a relational
operator
  • binary operators requiring two operands

5
3. An expression containing Boolean operators
or both relational Boolean operators
6
Example of an assertion
7
Truth table for a disjunction OR statement
OR T F T F
T T T
F
8
Truth table for a conjunction AND statement
AND T F T F
T
F F F
9
Boolean Operator (NOT)
!leapYear
leapYear false
Equivalent to
10
method Call type cast !, ,_ (unary) , /, ,
- lt, lt, gt, gt , !
Precedence of Operators
11
Using 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)
12
Java 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.
13
The 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.
Write a Comment
User Comments (0)
About PowerShow.com