8/12/2019 java chapter 2 ( If else while for )
1/35
1
Chapter II
Control Statements
Java ProgrammingCOSC 450
Dr. Mohamad EL ABED
8/12/2019 java chapter 2 ( If else while for )
2/35
Chapter II - Objectives
2
To be able to use the if and ifelse selection
To be able to use the while repetition structure to execute
statements in a program repeatedly
To be able to use the increment, decrement and assignment
operators
To understand multiple selection using the switch selection
structure
To be able to use the logical operators
8/12/2019 java chapter 2 ( If else while for )
3/35
Chapter II - Outline
3
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
4/35
The if Selection Statement
4
The ifselection statement either performsan action if a condition
is true or skipsthe action if the condition is false
Code if(condition)
Actions
If selection flowchart
8/12/2019 java chapter 2 ( If else while for )
5/35
The if Selection Statement
5
If selection flowchart Java language
Example Students grade
8/12/2019 java chapter 2 ( If else while for )
6/35
Chapter II - Outline
6
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
7/35
The ifelse Selection Statement
7
The ifelseselection statement performsan action if a condition
is true and performsa different action if the condition is false
Code if(condition)
Actions I
else
Actions II
Ifelse selection flowchart
8/12/2019 java chapter 2 ( If else while for )
8/35
The ifelse Selection Statement
8
Ifelse selection flowchart Java language
Example Students grade
8/12/2019 java chapter 2 ( If else while for )
9/35
Chapter II - Outline
9
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
10/35
switch Multiple-Selection Statement
10
o So far, we have only seen binaryselection
o Sometimes it is necessary to branch in more thantwo directions
o
We do this via multiple selectiono Javaprovides the switchmultiple-selection statement to handle
such decision making
8/12/2019 java chapter 2 ( If else while for )
11/35
switch Multiple-Selection Statement
11
o The switchstatement consists of a series of case labels, an
optionaldefault caseand statementsto execute for each case
switch ( expression){case value1:
statement-list1
case value2:
statement-list2
case
default:
statement-list
}
8/12/2019 java chapter 2 ( If else while for )
12/35
switch Multiple-Selection Statement
12
Problem Statement
Using the switch statement, write a program that outputs the
integer given by a user in the following way:
o 1 -> "One"
o 2 -> "Two"
o 3 -> "Three"
o Other numbers -> output a message "Bigger than three"
8/12/2019 java chapter 2 ( If else while for )
13/35
8/12/2019 java chapter 2 ( If else while for )
14/35
8/12/2019 java chapter 2 ( If else while for )
15/35
switch vs if Statement
15
8/12/2019 java chapter 2 ( If else while for )
16/35
switch vs if Statement
16
The switchstatement is more efficient than an ifstatement in
terms of performance (i.e., running time)
8/12/2019 java chapter 2 ( If else while for )
17/35
Chapter II - Outline
17
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
18/35
switch vs ifelse Statement
18
8/12/2019 java chapter 2 ( If else while for )
19/35
switch vs ifelse Statement
19
o A nested ifelsestructure is just as efficient as a switch
statement
o
However, a switchstatement may be easierto read
o Also, it is easier to add new cases to a switchstatement than to
a nested ifelsestructure.
o A nested ifelsestructure is then more efficient than an if
statement in terms of performance (i.e., running time)
8/12/2019 java chapter 2 ( If else while for )
20/35
Chapter II - Outline
20
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators
Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
21/35
The for Repetition Statement
21
The forrepetition statement handlesall the details of counter-
controlled repetition
Code for ( ; ; )
Actions
Initialization of the control variable
Loop-continuation condition
Increment of control variable
for repetition flowchart
8/12/2019 java chapter 2 ( If else while for )
22/35
The for Repetition Statement
22
Example Write a program that prints the numbers from 1 to 10
8/12/2019 java chapter 2 ( If else while for )
23/35
The for Repetition Statement
23
Example Write a program that prints the numbers from 1 to 10
8/12/2019 java chapter 2 ( If else while for )
24/35
Chapter II - Outline
24
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators
Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
25/35
The while Repetition Statement
25
A repetition statementallows you to specify that an action is to
be repeated while some condition remains true
Code while(condition)
Actions
while repetition flowchart
8/12/2019 java chapter 2 ( If else while for )
26/35
The while Repetition Statement
26
Problem Statement
A program designed to find the first power of 2 larger than 1000
while repetition flowchart Java language
8/12/2019 java chapter 2 ( If else while for )
27/35
Chapter II - Outline
27
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators
Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
28/35
Assignment Operators
28
8/12/2019 java chapter 2 ( If else while for )
29/35
Chapter II - Outline
29
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators
Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
30/35
Increment and Decrement Operators
30
8/12/2019 java chapter 2 ( If else while for )
31/35
Chapter II - Outline
31
The if Selection Statement
The ifelse Selection Statement
switch Multiple-Selection Statement
Switch vs if Statement
Switch vs ifelse Statement
The for Repetition Statement
The while Repetition Statement
Assignment Operators
Increment and Decrement Operators
Logical Operators
8/12/2019 java chapter 2 ( If else while for )
32/35
Logical Operators
32
o So far, we have studied only simple conditions, such as
o Sometimes it is necessary to test multiple conditionsin the
process of making a decision
8/12/2019 java chapter 2 ( If else while for )
33/35
Logical Operators
33
o We do this via logical operators
o Truth table for the logical ANDoperator (&&)
Logical Operator Name
&& Logical AND
|| Logical OR
! Logical NOT
8/12/2019 java chapter 2 ( If else while for )
34/35
Logical Operators
34
o Truth table for the logical ORoperator (||)
o Truth table for the logical NOT(!)
8/12/2019 java chapter 2 ( If else while for )
35/35
Top Related