Week 4
Week 4
o Decision Making
o If statement
The if statement determines whether a code should be executed based on the
specified condition.
If expression evaluates to true, execute statement, otherwise do nothing.
if (condition) {
Statement 1; //executed if condition is true
}
Example:
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 10) {
System.out.println("x + y is greater than 20");
}
}
}
o If-else statement
In this statement, if the condition is true, the if block is executed. Otherwise, the
else block is executed.
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Example:
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
o If-else-if statement
The if-else-if statement contains the if-statement followed by multiple else-if
statements.
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
Example:
public class Main {
public static void main(String[] args) {
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
o Nested-if statement
In nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Example:
public class Main {
public static void main(String[] args) {
int age=20;
int weight=80;
o Switch statement
The Java switch statement executes one statement from multiple conditions. The
switch statement allows us to execute a block of code among many alternatives.
In other words, the switch statement tests the equality of a variable against
multiple values.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Example:
public class SwitchExample {
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}
o Loop Statements
Looping in programming languages is a feature which facilitates the execution of a set of
instructions/functions repeatedly while some condition evaluates to true.
A loop is a programming construct that allows a code block to be repeated. Java has numerous
constructs for implementing loops. These are while, for, and do while loops.
o Do while loop
In a do while loop, the code block is executed first, then the condition is verified.
If the condition is met, the loop continues, else it ends. It is safe to assume that
the do while loop will be executed at least once.
How does a While loop executes?
1. Control falls into the do-while loop.
2. The statements inside the body of the loop get executed.
3. Updation takes place.
4. The flow jumps to Condition
5. Condition is tested.
6. If Condition yields true, goto Step 6.
7. If Condition yields false, the flow goes outside the loop
8. Flow goes back to Step 2.
// Java program to illustrate the do-while loop
class dowhileloopDemo {
public static void main(String args[])
{
// initialisation expression
int i = 1;
do {
// update expression
i++;
}
// test expression
while (i < 6);
}
}
o While loop
A while loop iterates around a block of code until the condition is met. If this
condition fails, the loop ends.
How does a While loop executes?
1. Control falls into the while loop.
2. The flow jumps to Condition
3. Condition is tested.
4. If Condition yields true, the flow goes into the Body.
5. If Condition yields false, the flow goes outside the loop
6. The statements inside the body of the loop get executed.
7. Updating takes place.
8. Control flows back to Step 2.
9. The do-while loop has ended and the flow has gone outside.
// test expression
while (i < 6) {
System.out.println("Hello World");
// update expression
i++;
}
}
}
o For loop
The for loop lets the programmer control both the condition and the loop
variable. Iterate over the loop variables and run until a condition on one of them
is true.
How does a While loop executes?
1. Control falls into the for loop. Initialization is done
2. The flow jumps to Condition
3. Condition is tested.
4. If Condition yields true, the flow goes into the Body
5. If Condition yields false, the flow goes outside the loop
6. The statements inside the body of the loop get executed.
7. The flow goes to the Updation
8. Updation takes place and the flow goes to Step 3 again
9. The for loop has ended and the flow has gone outside.
// Java program to illustrate for loop
class forLoopDemo {
public static void main(String args[])
{
// Writing a for loop
// to print Hello World 5 times
for (int i = 1; i <= 5; i++)
System.out.println("Hello World");
}
}