Java If-else Statement:
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
if:
if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Flowchart:

if-else:
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Flowchart:

nested-if:
A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement. Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
if-else-if ladder:
Here, a user can decide among multiple options.The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Flowchart:

switch-case:
The switch statement is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions. It is like an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. It basically tests the equality of variables against multiple values.
Syntax:
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;
}
Some Important Rules for Switch Statements:[TBD]
- There can be any number of cases just imposing condition check but remember duplicate case/s values are not allowed.
- The value for a case must be of the same data type as the variable in the switch.
- The value for a case must be constant or literal. Variables are not allowed.
- The break statement is used inside the switch to terminate a statement sequence.
- The break statement is optional. If omitted, execution will continue on into the next case.
- The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.(if it is used default in first and middle)
Switch Statement is fall-through:[TBD]
The Java switch statement is fall-through. It means it executes all statements after the first match if a break statement is not present.
Nested Switch Statement:[TBD]
We can use switch statement inside other switch statement in Java. It is known as nested switch statement.
This involves the concept of an inner and outer Switch. We can use an inner Switch as a part of the statement of an outer Switch. This type of Switch statement is called the Nested Switch statement or Switch(Inner) inside a Switch(Outer) is known as a Nested Switch.
syntax:
switch (count){
case 1:
switch (target){
//nested switch statement
case 0:
System.out.println(“target is 0”);
break;
case 1:
System.out.println(“target is 1”);
break;
}
break;
case 2:
//…
}
Enum in Switch Statement:[TBD]
Java allows us to use enum in switch statement. Java enum is a class that represent the group of constants. (immutable such as final variables). We use the keyword enum and put the constants in curly braces separated by comma.
Wrapper in Switch Statement:[TBD]
Java allows us to use four wrapper classes: Byte, Short, Integer and Long in switch statement.
Switch Statement with String:[TBD]
Java allows us to use strings in switch expression since Java SE 7. The case statement should be string literal.
Flowchart:

jump: [TBD]
Java supports three jump statements: break, continue and return. These three statements transfer control to another part of the program.
- Break: In Java, a break is majorly used for:
- Terminate a sequence in a switch statement (discussed above).
- To exit a loop.
- Used as a “civilized” form of goto.
- Continue: Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop’s end. The continue statement performs such an action.
Flowchart:

Reference:
https://www.geeksforgeeks.org/switch-statement-in-java/
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
https://www.w3schools.com/java/java_conditions.asp
https://www.geeksforgeeks.org/java-if-statement-with-examples/
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Leave a comment