Decision-making statements in any programming language help us in taking decisions base on certain conditions so we can execute a particular block of code and discard the remaining block of code to get our desired output. These are used to control the flow of the execution of the program. It works similarly like we take in real-life situations.
Selection statements are available in Java.
- Simple if
- if-else
- nested-if-else
- switch case
- break
- continue
- return
It might necessary to make a decision before arriving at a conclusion or to go on to the next step of processing, a selection statement is used in that case.
- Simple if: If is the most simple decision-making statement which is used to decide whether a statement or a block of code will be executed or not. If the condition is written inside if the statement is true then the block of code will be executed otherwise it will not be executed.
- if statement only accepts the boolean value true or false and if the condition is written inside round brackets is true then the code inside the curly brackets will be executed.
Here, Curly brackets are essential if there is more than one statement to execute. If you will write more than one statement inside if statement without curly brackets then only the first statement will execute when the condition is satisfied.
Flowchart of if statement:
Example of if statement:
class AlgoLesson { public static void main(String[] args) { int num = 4; if(num < 5) { int square = num * num; System.out.println(square); } } }
16
- if-else: In this statement, if the condition gets true then the code that is present in the if block will get executed, and if the condition gets false then the code that is present in the else block gets executed. It is different from simple if because here we know what to do when the condition will not satisfy.
Flowchart for if-else statement:
Example of an if-else statement:class AlgoLesson { public static void main(String[] args) { int num = 6; if(num < 5) { int sum = num + num; System.out.println(sum); } else { int square = num * num; System.out.println(square); } } }
36
- nested if-else: It is used when one if statement triggers another if statement that is present inside the if block of code. We can check multiple conditions using nested if-else statements.
Flowchart for nested if-else:
Example of nested if-else:
class AlgoLesson
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
int c = 30;
if(a > b)
{
if(a > c)
{
System.out.println("Biggest number: "+a);
}
else
{
System.out.println("Biggest number: "+b);
}
}
else if(c > b)
{
System.out.println("Biggest number: "+c);
}
else
{
System.out.println("Biggest number: "+b);
}
}
}
Biggest number: 30
- switch case: In the switch statement, we use a value of a variable or an expression to change the control flow of the program execution. Here we choose which block of code to execute base on the value we give as input and each block of code ends with a break statement.
If non of the variable or expression matches with our desired value then the default block of code get executed.
Some important points about switch case in Java:
- We can have N number of switch-case statements in Java.
- Duplicate values are not allowed or we will get a compilation error.
- Data of the value which we provide to the switch case must be the same as the data type of the expression of the switch case.
- The datatype which is allowed for switch case expression is int, char, short, byte, and long.
- Break statement in switch case is to control execution if we remove break then it will execute all the code until it reaches its end or meets new break statement.
- The default statement is optional, it gets execute when no expression matches the case. If we remove the default statement then noting will execute in that case.
Flowchart for switch case statement:
Example of switch statement:
public class AlgoLesson
{
public static void main(String[] args) {
int day = 6;
switch(day)
{
case 1:
System.out.println("Today is Monday");
break;
case 2:
System.out.println("Today is Tuesday");
break;
case 3:
System.out.println("Today is Wednesday");
break;
case 4:
System.out.println("Today is Thursday");
break;
case 5:
System.out.println("Today is Friday");
break;
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Invalid Input");
}
}
}
Today is Saturday
- break: break statement is mostly used to terminate the switch sequence or to exit a loop. But if you are using a break statement inside a nested loop then only it will only break out of the innermost loop.
public class AlgoLesson { public static void main(String[] args) { for(int i = 1; i<=10; i++) { //terminate the loop when i is 6 if(i == 6) break; System.out.println(i); } System.out.println("Loop get Terminated"); } }
1
2
3
4
5
Loop get Terminated
- continue: continue statement skips the current iteration of the loop and begin the next iteration of the loop. It is useful for the early iteration of a loop when a certain condition is met.
Flowchart for continue statement:
Example of continue statement:
//program to print even numbers public class AlgoLesson { public static void main(String[] args) { for(int i = 0; i < 10; i++) { if(i == 6) { continue; } System.out.println(i); } } }
Output:
0
1
2
3
4
5
7
8
9
- return: return statement basically uses to return from a method. It can also use to return a value and the type of value it returns depends on the method return type.
Example of return statement:
public class AlgoLesson { public static void main(String[] args) { int a = 10, b = 10; System.out.println("Before return statement"); if(a == b) return; System.out.println("This will not execute!"); } }
Before return statement
public class AlgoLesson { public static boolean returnExample(int a, int b) { if(a == b) return true; else return false; } public static void main(String[] args) { boolean result = returnExample(10, 20); System.out.println(result); } }
false
Related post:
(getButton) #text=(Switch case statement in Java) #icon=(link) #color=(#2339bd)
(getButton) #text=(Do While Loop in Java) #icon=(link) #color=(#2339bd)
(getButton) #text=(For Loop in Java) #icon=(link) #color=(#2339bd)
No comments:
Post a Comment