Lecture-5. Control Statements
Lecture-5. Control Statements
• If statement
• Switch statement
If Statement
• Use to evaluate a condition
• Gives a Boolean value, either true or false
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
Simple if statement
if(condition) {
statement 1; //executes when condition is true
}
Simple if statement
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
If-else statement
• An extension to the if-statement, which uses another block of
code.
• The else block is executed if the condition of the if-block is
evaluated as false.
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
If-else statement
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");
}
}
}
If-else-if ladder
• Chain of if-else statements that create a decision tree where the
program may enter in the block of code where the condition is true.
• The case variables can be int, short, byte, char. String type is also supported
since version 7 of Java
• Cases cannot be duplicate
• Default statement is executed when any of the case doesn't match the value
of expression. It is optional.
• Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
• While using switch statements, we must notice that the case expression will
be of the same type as the variable. However, it will also be a constant value.
Switch Statement
switch (expression){
case value1:
statement1;
break;
.
.
case valueN:
statementN;
break;
default:
default statement;
}
Switch Statement
public class Student implements Cloneable {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
} } }
Loop Statements
• Execute the block of code repeatedly while some condition
evaluates to true.
• Loop statements are used to execute the set of instructions in
a repeated order
1. For loop
2. While loop
3. Do-while loop
For loop
Flowchart:
Example:
for(initialization, condition, increment/decrement) {
//block of statements
}
For loop
public class Calculation {
public static void main(String[] args) {
int sum = 0;
for(int j = 1; j<=10; j++) {
sum = sum + j;
}
System.out.println("The sum of first 10 natural numbers is " + sum);
} }
While loop
Flowchart:
Example:
while(condition){
//looping statements
}
While loop
public class Calculation {
public static void main(String[] args) {
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
while(i<=10) {
System.out.println(i);
i = i + 2;
} } }
Do-while loop
Flowchart:
Example:
do
{
//statements
} while (condition);
Do-while loop
public class Calculation {
public static void main(String[] args) {
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
do {
System.out.println(i);
i = i + 2;
}
while(i<=10);
} }
Jump Statements
• Jump statements are used to transfer the control of the program
to the specific statements.
• Transfer the execution control to the other part of the program.
1. Break
2. Continue
Break Statement
Example: Output:
public class BreakExample {
0
1
public static void main(String[] args) { 2
3
for(int i = 0; i<= 10; i++) { 4
System.out.println(i); 5
if(i==6) {
break;
} } }
}
Continue Statement
Example: Output:
public class ContinueExample {
0
public static void main(String[] args) { 1
for(int i = 0; i<= 2; i++) { 2
3
for (int j = i; j<=5; j++) { 5
1
if(j == 4) { 2
continue; 3
} 5
System.out.println(j); 2
} } } 3
} 5
Thank You