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.
The Java if statement tests the condition. It executes the if block if condition is true.
if(condition){ //code to be executed }
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
if(condition){ //code if condition is true }else{ //code if condition is false }
We can also use ternary operator (? :) to perform the task of if...else statement. It is a shorthand way to check the condition. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.
int number=13; //Using ternary operator String output=(number%2==0)?"even number":"odd number";
The if-else-if ladder statement executes one condition from multiple statements.
if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false }
The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.
if(condition){ //code to be executed if(condition){ //code to be executed } }
The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.
In other words, the switch statement tests the equality of a variable against multiple values.
Points to Rememberswitch(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; }
The Java switch statement is fall-through. It means it executes all statements after the first match if a break statement is not present.
public class SwitchExample2 { public static void main(String[] args) { int number=20; //switch expression with int value switch(number){ //switch cases without break statements case 10: System.out.println("10"); case 20: System.out.println("20"); case 30: System.out.println("30"); default:System.out.println("Not in 10, 20 or 30"); } } }Output:
20 30 Not in 10, 20 or 30
Java allows us to use strings in switch expression since Java SE 7. The case statement should be string literal
public class SwitchStringExample { public static void main(String[] args) { //Declaring String variable String levelString="Expert"; int level=0; //Using String in Switch expression switch(levelString){ //Using String Literal in Switch case case "Beginner": level=1; break; case "Intermediate": level=2; break; case "Expert": level=3; break; default: level=0; break; } System.out.println("Your Level is: "+level); } }
We can use switch statement inside other switch statement in Java. It is known as nested switch statement.
public class NestedSwitchExample { public static void main(String args[]) { //C - CSE, E - ECE, M - Mechanical char branch = 'C'; int collegeYear = 4; switch( collegeYear ) { case 1: System.out.println("English, Maths, Science"); break; case 2: switch( branch ) { case 'C': System.out.println("Operating System, Java, Data Structure"); break; case 'E': System.out.println("Micro processors, Logic switching theory"); break; case 'M': System.out.println("Drawing, Manufacturing Machines"); break; } break; case 3: switch( branch ) { case 'C': System.out.println("Computer Organization, MultiMedia"); break; case 'E': System.out.println("Fundamentals of Logic Design, Microelectronics"); break; case 'M': System.out.println("Internal Combustion Engines, Mechanical Vibration"); break; } break; case 4: switch( branch ) { case 'C': System.out.println("Data Communication and Networks, MultiMedia"); break; case 'E': System.out.println("Embedded System, Image Processing"); break; case 'M': System.out.println("Production Technology, Thermal Engineering"); break; } break; } } }Output:
Data Communication and Networks, MultiMedia
Java allows us to use enum in switch statement.
public class JavaSwitchEnumExample { public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat } public static void main(String args[]) { Day[] DayNow = Day.values(); for (Day Now : DayNow) { switch (Now) { case Sun: System.out.println("Sunday"); break; case Mon: System.out.println("Monday"); break; case Tue: System.out.println("Tuesday"); break; case Wed: System.out.println("Wednesday"); break; case Thu: System.out.println("Thursday"); break; case Fri: System.out.println("Friday"); break; case Sat: System.out.println("Saturday"); break; } } } }Output:
Sunday Monday Twesday Wednesday Thursday Friday Saturday
Java allows us to use four wrapper classes: Byte, Short, Integer and Long in switch statement.
public class WrapperInSwitchCaseExample { public static void main(String args[]) { Integer age = 18; switch (age) { case (16): System.out.println("You are under 18."); break; case (18): System.out.println("You are eligible for vote."); break; case (65): System.out.println("You are senior citizen."); break; default: System.out.println("Please give the valid age."); break; } } }Output:
You are eligible for vote.
In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in java.
Comparison | for loop | while loop | do while loop |
---|---|---|---|
Introduction | The Java for loop is a control flow statement that iterates a part of the programs multiple times. | The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition. | The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition. |
When to use | If the number of iteration is fixed, it is recommended to use for loop. | If the number of iteration is not fixed, it is recommended to use while loop. | If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop. |
Syntax | for(init;condition;incr/decr){ // code to be executed } | while(condition){ //code to be executed } | do{ //code to be executed }while(condition); |
Example | //for loop for(int i=1;i<=10;i++){ System.out.println(i); } | //while loop int i=1; while(i<=10){ System.out.println(i); i++; } | //do-while loop int i=1; do{ System.out.println(i); i++; }while(i<=10); |
Syntax for infinitive loop | for(;;){ //code to be executed } | while(true){ //code to be executed } | do{ //code to be executed }while(true); |
A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:
for(initialization;condition;incr/decr){ //statement or code to be executed } public class ForExample { public static void main(String[] args) { //Code of Java for loop for(int i=1;i<=10;i++){ System.out.println(i); } } }Java for-each Loop
The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.
for(Type var:array){ //code to be executed } public class ForEachExample { public static void main(String[] args) { //Declaring an array int arr[]={12,23,44,56,78}; //Printing array using for-each loop for(int i:arr){ System.out.println(i); } } }Java Labeled For Loop
We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
Usually, break and continue keywords breaks/continues the innermost for loop only.
for(initialization;condition;incr/decr){ //code to be executed } public class LabeledForExample { public static void main(String[] args) { //Using Label for outer and for loop aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ break aa; } System.out.println(i+" "+j); } } } }Output
1 1 1 2 1 3 2 1
The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.
while(condition){ //code to be executed } public class WhileExample { public static void main(String[] args) { int i=1; while(i<=10){ System.out.println(i); i++; } } }Output
1 2 3 4 5 6 7 8 9 10
The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.
The Java do-while loop is executed at least once because condition is checked after loop body.
do{ //code to be executed }while(condition); public class DoWhileExample { public static void main(String[] args) { int i=1; do{ System.out.println(i); i++; }while(i<=10); } }Output
1 2 3 4 5 6 7 8 9 10
do{ //code to be executed }while(true); public class DoWhileExample2 { public static void main(String[] args) { do{ System.out.println("infinitive do while loop"); }while(true); } }Output
infinitive do while loop infinitive do while loop infinitive do while loop ctrl+c
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.
We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.
Example Java Break Statement with Looppublic class BreakExample { public static void main(String[] args) { //using for loop for(int i=1;i<=10;i++){ if(i==5){ //breaking the loop break; } System.out.println(i); } } }Java Break Statement with Inner Loop
public class BreakExample2 { public static void main(String[] args) { //outer loop for(int i=1;i<=3;i++){ //inner loop for(int j=1;j<=3;j++){ if(i==2&&j==2){ //using break statement inside the inner loop break; } System.out.println(i+" "+j); } } } }Java Break Statement with Labeled For Loop
public class BreakExample3 { public static void main(String[] args) { aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ //using break statement with label break aa; } System.out.println(i+" "+j); } } } }Java Break Statement in while loop
public class BreakWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; } } }Java Break Statement in do-while loop
public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } }
The continue statement is used in loop control structure when you need to jump to the next iteration of the loop immediately. It can be used with for loop or while loop.
The Java continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition. In case of an inner loop, it continues the inner loop only
We can use Java continue statement in all types of loops such as for loop, while loop and do-while loop.
Example Java Continue Statement Examplepublic class ContinueExample { public static void main(String[] args) { //for loop for(int i=1;i<=10;i++){ if(i==5){ //using continue statement continue;//it will skip the rest statement } System.out.println(i); } } }Java Continue Statement with Inner Loop
public class ContinueExample2 { public static void main(String[] args) { //outer loop for(int i=1;i<=3;i++){ //inner loop for(int j=1;j<=3;j++){ if(i==2&&j==2){ //using continue statement inside inner loop continue; } System.out.println(i+" "+j); } } } }Java Continue Statement with Labeled For Loop
public class ContinueExample3 { public static void main(String[] args) { aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ //using continue statement with label continue aa; } System.out.println(i+" "+j); } } } }Output:
1 1 1 2 1 3 2 1 3 1 3 2 3 3Java Continue Statement in while loop
public class ContinueWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i==5){ //using continue statement i++; continue;//it will skip the rest statement } System.out.println(i); i++; } } }Java Continue Statement in do-while loop
public class ContinueDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using continue statement i++; continue;//it will skip the rest statement } System.out.println(i); i++; }while(i<=10); } }Output:
2 3 4 6 7 8 9 10
//This is single line commentJava Multi Line Comment
/* This is multi line comment */Java Documentation Comment
/** This is documentation comment */
Total : 26654
Today :3
Today Visit Country :