Hello Everyone, I am posting my second blog where i have highlighted the concepts of Java Control Statements with simple examples. Hope it will be helpful for everyone.
Control Statements in any programming language is a fundamental and essential part that allows any developers to control the order in which instructions are executed in a program.In this blog we will go through the basic control statements in java which are if-else blocks,switch statements and loops.
If-Else Statement:
The If -Else is a conditional statement which is used if one condition is true and another condition false which means that the program can choose between two conditions and execute the block of code.
Syntax of Simple If-Else Statement:
if (condition) {
//if condition is true this block of code gets executed
} else {
//if condition is false then this block of code gets executed
}
Let's see with a simple example:
Find student's eligibility to get into Elite club greater than 90%
if(average>90){
System.out.println("You are in elite club");
} else {
System.out.println("You are a part of non-elite club");
}
Nested If-Else Statement:
This conditional statement allows to check multiple conditions sequentially and executes it
Syntax of Nested if-Else Statement:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
Example of a Nested if-Else Statement:
Check the grade of a student
public class Studentgrade {
public static void main(String[] args) {
int marks=85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
if (marks>= 85) {
System.out.println("Well done!");
} else {
System.out.println("Good job!");
}
} else if (marks>= 70) {
System.out.println("Grade: C");
} else if (marks >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
Switch Statements and Loops:
Switch Statements:Switch statements in java are control statements which allows to select and execute one of the block of code depending on the expression. Switch statement is a good alternative for Nested if -Else for making the code more readable and easier to manage
Syntax of Switch Statement:
switch (expression) {
case value1:
// Code to run when expression equals value1
break; // Exits the switch block
case value2:
// Code to run when expression equals value2
break; // Exits the switch block
// ...
default:
// Code to run if no case matches
}
Example of a Switch Statement:
Write remarks of a student in progress report
public class SwitchExample {
public static void main(String[] args) {
switch (grade) {
case 'a':{
System.out.println("Well done.Congrats");
break;
}
case 'b':{
System.out.println("Good work,keep it up");
break;
}
case 'c':{
System.out.println("Perform well next time");
break;
}
case 'd':{
System.out.println("Need improvement");
break;
}
default:{
System.out.println("Need focus");
}
}
Loops in Java: Java loops are a fundamental concept in Java programming as it allows to execute a block of code multiple times.There are mainly three types of loops in Java namely for loops, While loops and do-while loops.
Java For Loop: The for loop is used when we know the exact number of times you need to iterate over a block of code.
Syntax:
for(initialization; condition; increement/decreement) {
//code to be executed after each iteration
}
Example of a For loop:
Print even numbers between 0 to 100:
int i=0;
for (i=0;i<=100;i=i+2){
System.out.println("value of i is" +i);
}
Java While Loop: The While loop loops through a block of code as long as a specified condition is true
Syntax:
while(condition) {
    // Code to execute as long as condition is true
}
Example of a While Loop:
Print even numbers from 0 to 100 using while loop
int i=0;
while(i<=100){
System.out.println("while loop of i" +i);
i=i+2;
}
Do while Loop:The do-while loop makes sure that the block of code is executed at least once before the condition is tested.
Syntax:
do{
//block of code to be executed
}
while (condition);
Example of a do-while loop:
Print numbers from 1 to 10
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 10);
}
}
Loop Control Statements in Java:
Java provides Control Statements to manage the flows within the loops-'break' to exit a loop and 'Continue' to skip the current iteration and proceed to the next.
Break Statement: The 'break' statement is necessary to exit a loop before the loop has finished fully iterating over all the step values. For example, looping over a list of numbers until you find a number that satisfies a certain condition. Or looping over a stream of characters from a file until a certain character is read.
Let's demonstrate with a simple example:
for(int i=0; i<10; i++) {
System.out.println(i);
if(i==4) {
break; //condition is true
}
}
Continue Statement:Unlike the'break statement' the Continue Statement skips over the current iteration of the loop and continues to the next iteration of the loop .This is particularly used when we want to skip a particular condition and continue to the rest of the execution.
Let's demonstrate with a simple example:
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
System.out.println(i);
i++;
}
Thank you Everyone ....
Comentarios