1. While Loop Example
public class WhileLoop {public static void main(String args[]) {
int x = 10;
while( x < 15 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
Output :
value of x : 10, value of x : 11, value of x : 12, value of x : 13, value of x : 14, value of x : 152. For Loop Example
public class ForLoop {public static void main(String args[]) {
for(int x = 10; x < 15; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Output :
value of x : 10, value of x : 11, value of x : 12, value of x : 13, value of x : 14, value of x : 153. Do While Loop Example
public class DoWhileLoop {public static void main(String args[]){
int x = 10;
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 15 );
}
}
Output : value of x : 10, value of x : 11, value of x : 12, value of x : 13, value of x : 14, value of x : 15
4. For Each Loop Example
public class ForEach {public static void main(String args[]){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
Output :
10, 20, 30, 40, 50,James, Larry, Tom, Lacy,
5. Break Statement Example
public class Break {public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output :
10, 2020
6. Continue Statement Loop Example
public class Continue {public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output :
10, 20, 40, 507. IF Statement Loop Example
public class IfExample {public static void main(String args[]){
int x = 10;
if( x < 20 ){
System.out.print("This is if statement");
}
}
}
Output :
This is if statement.8. IF Else Statement Loop Example
public class IfElse {public static void main(String args[]){
int x = 30;
if( x < 20 ){
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
}
}
Output :
This is else statement.8. IF Else If Else Statement Loop Example
public class IfElseIf {public static void main(String args[]) {
int x = 30;
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else statement");
}
}
}
Output :
Value of X is 30.9. Nested IF Statement Loop Example
public class NestedIf {public static void main(String args[]){
int x = 30;
int y = 10;
if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
}
}
Output :
X = 30 and Y = 1010. Switch Statement Loop Example
public class SwitchExample {public static void main(String args[]){
char grade = 'C';
switch(grade) {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
System.out.println("Very Good.");
break;
case 'C' :
System.out.println("Well Done.");
break;
case 'D' :
System.out.println("You Passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}