You are on page 1of 10

CHAPTER THREE

DECISION AND REPETITION


STATEMENTS
2

Outline
• If/else statement
• Switch case
• While loop
• Do while loop
• For loop
3

Decision statements
• The program can decide which statements to execute based on a condition.

• Java has several types of selection statements:

• one-way if statements

• two-way if-else statements

• nested if statements

• multi-way if-else statements

• switch case statements, and

• conditional expressions.
4

Decision statm…
one way if statement
•A one-way if statement executes an action if and only if the condition is
true.
Syntax

•If the boolean-expression evaluates to true, the statements in the


block are executed. As an example, see the following code:
5

Decision statm…
• The block braces can be omitted if they enclose a single statement. For

example, the following statements are equivalent.

• Two-Way if-else Statements

• An if-else statement decides the execution path based on whether the

condition is true or false.


• Syntax


6

Dec statem…
Example.

• Nested if and Multi-Way if-else Statements


• An if statement can be inside another if statement to form a nested if
statement.
• Example
7

Dec statmn…
• Example multi way if statement

Switch Statements
• A switch statement executes statements based on the value of a variable or
an expression.
8

Dec statem…

• Repetition (loop) statements


• A loop can be used to tell a program to execute statements repeatedly.
• Three types of loops in java: while, do while and for loop.
9

Loops in java
while Loop
•A while loop executes statements repeatedly while the condition is
true.

•Do while loop


•A do-while loop is the same as a while loop except that it executes the
loop body first and then checks the loop continuation condition.
Comparative Study of Programming Languages Joey Paquet, 2010-2014 10

End of chapter two

You might also like