loops, switch statements, and if-else statements in Java

5/5 - (1 vote)

Hello everyone, in the previous topic we were talking about Operators in Java. Today we are going to talk about loops, switch statements, and if-else statements in Java.

loops, switch statements, and if-else statements in Java

loops, switch statements, and if-else statements in Java
loops, switch statements, and if-else statements in Java

Java if Statement

The Java if statement tests the condition. It executes the if block if a condition is true.

if(condition){
//code to be executed
}

Java if-else Statement

The Java if-else statement also tests the condition. It executes the if block, if the condition is true otherwise else block, is executed.

if(condition){
//code is true
}else{
//code if condition is false
}

Leap Year Example of Java if-else statement

A year is leap, if it is divisible by 4 and 400. But, not by 100.
public class test{
public static void main(String[] args) {
 int year=2020;
 if((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){ System.out.println(“LEAP YEAR”);
}
else{
System.out.println(“NOT LEAP YEAR”);
}
}
}
Leap Year Example of Java if-else statement

Short Hand If..Else (Ternary Operator)

Syntax
variable = (condition) ? expressionTrue: expressionFalse;
public class test{
public static void main(String[] args){
 int number=12,
 //Using ternary operator
String output=(number%2==0)?”even number”:”odd number”;
System.out.println(output);
}
}
Short Hand If.Else (Ternary Operator)

Switch Statement in Java

The Java switch statement executes one statement from multiple conditions. It is like if-else if ladder statement.

 In other words, the switch statement tests the equality of a variable against multiple values.

switch(expression){
case value 1:
//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;
Switch Statement in Java

Note– Basically, the expression can be byte, short, char, and int primitive data types. Beginning with JDK7, it also works with enumerated types (Enums in java), the String class and Wrapper classes default.

Example
public class test{
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”);
}
}
}

Java Switch Statement with String

class test {
public static void main(String[] args) {
String demo= “Expert”;
int level= 0;
switch(demo) {
case “Beginner”: level=1;
break;
case “Intermediate”: level=2;
break;
case “Expert”: level=3;
break;
default level=0;
}
System.out.println(“Your Level is. “+level);
}
}
Output- Your Level is: 3
Java Switch Statement with String

Some important points to remember in switch case

  • There can be one or N number of case values for a switch expression.
  • The case value must be of switch expression type only. The case value must be literal or constant. It doesn’t allow variables.
  • The case values must be unique. In case of duplicate value, it renders compile-time error.
  • The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums, and string.
  • Each case statement can have a break statement which is optional. When control reaches the break statement, it jumps the control after the switch expression. If the break statement is not found, it executes 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.

Loops in JAVA

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

  1. for loop
  2. while loop
  3. do-while loop
  4. for loop
Syntax of for loop- for(initialization; condition; incr/decr)
{
//statement or code to be executed
}
Syntax of for loop
Example of for loop
public class test{
public static void main(String[] args) {
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
while and do-while loop
 while loop executes the code until the condition is false.
Syntax of while loop-
while(condition){
//code
}
Syntax of while loop
Syntax of a do-while loop-
do
{
statements;
} while (expression);
Syntax of a do-while loop

Difference between while and do-while loop

  1. Condition is checked first then statement(s) is executed in while whereas in do-while Statement’s) is executed at least once after that condition is checked.
  2.  In while loop occur statement(s) is executed zero times, if condition is false whereas in do while at least once the statement(s) is executed
  3. No semicolon at the end of while i.e while(condition) whereas semicolon is mandatory at the end of do while i.e 

while(condition);

  1.  If there is a single statement, brackets are not required in while whereas in do-while brackets are always required.
  2. Variable in the condition is initialized before the execution of loop in while whereas in a do-while variable may be initialized before or within the loop.
  3.  While loop is entry controlled loop whereas the do-while loop is exit controlled loop.
Syntax of while-
while(condition)
{statement(s);} whereas
Syntax of do-while is-
do {statement(s);}
while(condition);
Syntax of while

Java for-each Loop

The Java for-each loop is used to traverse array or collection in java. It is easier to use than simple for a loop because we don’t need to increment value and use subscript notation. It works on the basis of an element, not an index. It returns elements one by one in the defined variable.

It is not available in C or C++ languages.

Syntax of for-each loop-
for(Type var:array){
//code to be executed
}
Syntax of the for-each loop
Example of the for-each loop
String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”);
for (String i: cars) {
System.out.println(i);  
}
Example of the for-each loop
Another example of for-each loop
int arr[]=(12,23,44,56,78);
for(int i:arr){
System.out.println(i); }
Another example of for-each loop

So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our Instagram page for your daily dose of Motivation.

Thank You,

Regards

Grooming Urban

General FAQ

What is Java if statement?

The Java if statement tests the condition. It executes the if block if a condition is true.

What is Java if-else statement?

The Java if-else statement also tests the condition. It executes the if block, if the condition is true otherwise else block, is executed.

What is a Java switch statement?

The Java switch statement executes one statement from multiple conditions. It is like if-else if ladder statement.
In other words, the switch statement tests the equality of a variable against multiple values.

What are loops in Java?

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
1. for loop
2. while loop
3. do-while loop
4. for loop

What is Java for-each loop?

The Java for-each loop is used to traverse array or collection in java. It is easier to use than simple for a loop because we don’t need to increment value and use subscript notation. It works on the basis of an element, not an index. It returns elements one by one in the defined variable.
It is not available in C or C++ languages.

Sharing Is Caring:
Kumar Shanu Sinha

An aspiring MBA student formed an obsession with Management Related Concept, Digital Marketing, Leadership, and Personality Development now helping others to improve in their studies and personality as well.

1 thought on “loops, switch statements, and if-else statements in Java”

Leave a Comment