Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Chapter two…

Decision and Repetition Statements


Decision and Repetition Statements
 The Java keywords for controlling program flow are nearly identical to C++.

 Selection

 The Java language provides two alternative statements -if statements and

switch statements - for selecting among the given alternatives.


 The if Statement
 The Java if statement is a test of any Boolean expression. If the Boolean

expression evaluates to true, the statement following the if statement is


executed.
General syntax:
if (<condition>) {
<statement(s)> ;
}

2
Cont’d…

import java.util.Date;
Date today = new Date();
if (today.getDay == 0) then
System.out.println("It is Sunday.");
 This code uses the java.util.Date package and creates a variable named
today that will hold the current date.
 The getDay member method is then applied to today and the result
compared to 0.
if/else statement: A Java statement that executes one block of statements if
a certain condition is true, and a second block of statements if the condition
is false.
General syntax:
if (<condition>) {
<statement(s)> ;
}
else {
<statement(s)> ;
3 }
Cont’d…
import java.util.Date;
Date today = new Date();
if (today.getDay == 0) then
System.out.println("It is Sunday.");
else
System.out.println("It is NOT Sunday.");
 In this case, the same message will be displayed whenever it is Sunday, but a
different message will be displayed whenever it is not Sunday.
 By enclosing the statements within curly braces, you can execute as many lines of
code as you'd like.
import java.util.Date;
Date today = new Date();
if (today.getDay == 0) then {
System.out.println("It is Sunday.");
System.out.println("And a good day for golf.");
}
else {
System.out.println("It is NOT Sunday.");
System.out.println("But still a good day for golf.");
4 }
Nested if/else statements
 A chain of if/else that can select between many different outcomes based on
several conditions.
General syntax (shown with three different outcomes, but any number of else
if statements can be added in the middle):
if (<condition>) {
<statement(s)> ;
} else if (<condition>) {
<statement(s)> ;
} else {
<statement(s)> ;}
Example:
int grade = console.nextInt();
if (grade >= 90) {
System.out.println("Congratulations! An A!");
} else if (grade >= 80) {
System.out.println("Your grade is B. Not bad.");
} else if (grade >= 70) {
System.out.println("You got a C. Work harder!");
}

5
Cont’d…

Exercise
Write a java program that identifies the current day from the
days in a week?

6
The switch Statement
 The Java switch statement is ideal for testing a single expression against a series of possible values
and executing the code associated with the matching case statement, as shown in the following
example:
import java.util.Date;
Date today = new Date();
switch (today.getDay) {
case 0: // Sunday
System.out.println("It is Sunday.");
break;
case 1: // Monday
System.out.println("It is Monday.");
break;
case 2: // Tuesday
System.out.println("It is Tuesday.");
break;
case 3: // Wednesday
System.out.println("It is Wednesday.");
break;
case 4: // Thursday
System.out.println("It is Thursday.");
break;
case 5: // Friday
System.out.println("It is Friday.");
System.out.println("Have a nice weekend!");
break;
default: // Saturday
System.out.println("It must be Saturday.");
}
System.out.println("All done!");
7
Iteration
 Iteration is an important concept in any computer language.
 Without the ability to loop or iterate through a set of values, our ability to
solve real-world problems would be severely limited.
 Java's iteration statements are nearly identical to those found in C and C++
and include for loops, while loops, and do…while loops.
 The for Statement
 The first line of a for loop enables you to
 specify a starting value for a loop counter
 specify the test condition that will exit the loop, and
 indicate how the loop counter should be incremented (decremented)
after each pass through the loop.
 The syntax of a Java for statement is as follows:
for (initialization; testExpression; incremement)
statement
8
For example, a sample for loop may appear as follows:
int count;
for (count=0; count<100; count++)
System.out.println("count = " + count);

9
Nested for Loop
 If we have a for loop inside the another loop, it is known as nested for loop.
 The inner loop executes completely whenever outer loop executes.
Example:
NestedForExample.java
public class NestedForExample {
public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
}
}
10
 The while Statement
 Related to the for loop is the while loop.
The syntax for a while loop is as follows:
while (booleanExpression)
statement
 As you can tell from the simplicity of this, the Java while loop does not
have the built-in support for initializing and incrementing variables that its
for loop does.
 Because of this, you need to be careful to initialize loop counters prior to
the loop and increment them within the body of the while loop.
For example, the following code fragment will display a message five times:
int count = 0;
while (count < 5) {
System.out.println("Count = " + count);
count++;
}
11
The do…while Statement
 The final looping construct in Java is the do…while loop.
 The syntax for a do…while loop is as follows:
do {
statement
} while (booleanExpression);
 This is similar to a while loop except that a do…while loop is guaranteed to
execute at least once.
 It is possible that a while loop may not execute at all depending on the test
expression used in the loop.

12
Exercise

13
The End!

14

You might also like