Control Statement Examples
if-else programs
Program for Simple if-else conditionProgram for Simple if-else condition
class Demo
{
public static void main(String args[])
{
int user = 17;
if(user<18)
{
System.out.println("User is below 18");
}
else
{
System.out.println("User is either 18 or above 18");
}
}
}
Output
User is below 18
Program for if-else if condition
Program for if-else if condition
class Demo
{
public static void main(String args[])
{
int user = 17;
if(user<18)
{
System.out.println("User is below 18");
}
else if(user>=18 && user<=30)
{
System.out.println("User is between 18 and 30");
}
else
{
System.out.println("User is above 30");
}
}
}
Output
User is below 18
Program for Multiple if-else if condition
Program for Multiple if-else if condition
class Demo
{
public static void main(String args[])
{
int user = 17;
if(user<18)
{
System.out.println("User is below 18");
}
else if(user>18 && user<30)
{
System.out.println("User is between 19 and 29");
}
else if(user==18 || user==30)
{
System.out.println("User is either 18 or 30");
}
else
{
System.out.println("User is above 30");
}
}
}
Output
User is below 18
Program for Nested if-else-else if
Program for Nested if-else-else if
public class IfElseIfDemo
{
public static void main(String args[])
{
int temp1=3;
int temp2=3;
if(temp1==5)
{
if(temp2==3)
{
System.out.println("Hi, temp1 is 5 and temp2 is 3");
}
else
{
System.out.println
("Hi, temp1 is 5 and temp2 is some value other than 3");
}
}
else if(temp1==4)
{
System.out.println("Hi, temp1 is 4");
}
else if(temp1==3)
{
if(temp2==3)
{
System.out.println("Hi, temp1 is 3 and temp2 is 3");
}
else if(temp2==2)
{
System.out.println("Hi, temp1 is 3 and temp2 is 2");
}
}
else
{
System.out.println("Hi, temp1 is some value other than 5,4,3");
}
}
}
Output
Hi, temp1 is 3 and temp2 is 3
Program to check Even or Odd
Program to check Even or Odd
import java.io.*;
public class IfElseDemo
{
public static void main(String[] args) throws IOException
{
try
{
int n;
System.out.println("Enter a number:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(in.readLine());
if (n % 2 == 0)
{
System.out.println("Given number is Even.");
}
else
{
System.out.println("Given number is Odd.");
}
}
catch(NumberFormatException e)
{
System.out.println(e.getMessage()+ " is not a numeric value.");
System.exit(0);
}
}
}
Output
Enter a number: 23 Given number is Odd.
switch() case programs
Program for simple free flowing Switch caseProgram for simple free flowing Switch case
public class SwitchDemo
{
public static void main(String[] args)
{
int i=1;
switch(i)
{
case 0:
System.out.println("i is 0");
break;
case 1:
System.out.println("i is 1");
break;
case 2:
System.out.println("i is 2");
break;
default:
System.out.println("i is greater than 2");
}
}
}
Output
i is 1
Program for simple Switch case through loop
Program for simple Switch case through loop
public class SwitchDemo
{
public static void main(String[] args)
{
for(int i=0; i<= 3 ; i++)
{
switch(i)
{
case 0:
System.out.println("i is 0");
break;
case 1:
System.out.println("i is 1");
break;
case 2:
System.out.println("i is 2");
break;
default:
System.out.println("i is greater than 2");
}
}
}
}
Output
i is 0
i is 1
i is 2
i is greater than 2
Program for Nested Switch Statements
Program for Nested Switch Statements
public class NestedSwitchDemo
{
public static void main(String[] args)
{
int i = 0;
int j = 1;
switch(i)
{
case 0:
switch(j)
{
case 0:
System.out.println("i is 0, j is 0");
break;
case 1:
System.out.println("i is 0, j is 1");
break;
default:
System.out.println
("nested default case!!");
}
break;
default:
System.out.println("No matching case found!!");
}
}
}
Output
i is 0, j is 1
Program for multiple Nested Switch Statements
Program for multiple Nested Switch Statements
public class NestedSwitchDemo
{
public static void main(String[] args)
{
int i = 0;
int j = 1;
int k = 2;
switch(i)
{
case 0:
switch(j)
{
case 0:
System.out.println("i is 0, j is 0");
break;
case 1:
switch(k)
{
case 0:
System.out.println("i is 0, j is 0 k is 0");
break;
case 1:
System.out.println("i is 0, j is 1, k is 1");
break;
case 2:
System.out.println("i is 0, j is 1, k is 2");
break;
default:
System.out.println("nested default case!!!!");
}
break;
default:
System.out.println("nested default case!!");
}
break;
default:
System.out.println("No matching case found!!");
}
}
}
Output
i is 0, j is 1, k is 2
for() loop programs
Program to print simple values through for() loopProgram to print simple values through for() loop
class ForLoopDemo
{
public static void main(String args[])
{
int i;
for(i=0; i<10; i++)
{
System.out.println("Value of i "+i);
}
}
}
Output
Value of i 0 Value of i 1 Value of i 2 Value of i 3 Value of i 4 Value of i 5 Value of i 6 Value of i 7 Value of i 8 Value of i 9
Program to print values through for() loop in if() condition
Program to print values through for() loop in if() condition
public class Demo
{
public static void main(String args[])
{
int i;
int j=1;
if(j==1)
{
System.out.println("Value of i in if()");
for(i=0; i<10; i++)
{
System.out.println("Value of i " +i);
}
}
else
{
System.out.println("Value of i in else");
for(i=0; i<10; i++)
{
System.out.println("Value of i " +i);
}
}
}
}
Output
Value of i in if() Value of i 0 Value of i 1 Value of i 2 Value of i 3 Value of i 4 Value of i 5 Value of i 6 Value of i 7 Value of i 8 Value of i 9
Program to print Up Pyramid through for() loop
Program to print Up Pyramid through for() loop
public class Demo
{
public static void main(String[] args) {
for(int i=1; i<= 6 ;i++){
for(int j=0; j < i; j++){
System.out.print("*");
}
System.out.println("");*
}
}
}
Output
* ** *** **** ***** ******
Program to print Down Pyramid through for() loop
Program to print Down Pyramid through for() loop
public class Demo
{
public static void main(String[] args)
{
for(int i=5; i>0 ;i--)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Output
***** **** *** ** *
Program to print Up-Down Pyramid through for() loop
Program to print Up-Down Pyramid through for() loop
public class Demo
{
public static void main(String[] args)
{
for(int i=1; i<= 5 ;i++)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
System.out.println("");
}
//create second half of pyramid
for(int i=5; i>0 ;i--)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Output
* ** *** **** ***** ***** **** *** ** *
Program to print Up Pyramid of Numbers through for() loop
Program to print Up Pyramid of Numbers through for() loop
public class Demo
{
public static void main(String[] args)
{
for(int i=1; i<= 5 ;i++)
{
for(int j=0; j < i; j++)
{
System.out.print(j+1);
}
System.out.println("");
}
}
}
Output
1 12 123 1234 12345
Program to print Down Pyramid of Numbers through for() loop
Program to print Down Pyramid of Numbers through for() loop
public class Demo
{
public static void main(String[] args)
{
for(int i=5; i>0 ;i--)
{
for(int j=0; j < i; j++)
{
System.out.print(j+1);
}
System.out.println("");
}
}
}
Output
12345 1234 123 12 1
Program to print Up-Down Inside Pyramid of Numbers through for() loop
Program to print Up-Down Inside Pyramid of Numbers through for() loop
public class Demo
{
public static void main(String[] args)
{
//generate upper half of the pyramid
for(int i=5; i>0 ;i--)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
//create a new line
System.out.println("");
}
//generate bottom half of the pyramid
for(int i=1; i<= 5 ;i++)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
//create a new line
System.out.println("");
}
}
}
Output
***** **** *** ** * * ** *** **** *****
Program to print triangle of Numbers
Program to print triangle of Numbers
class Demo
{
public static void main(String[] args)
{
int N = 5;
int k=1;
for (int i = 1; i <= N; i++)
{
for (int j = 0; j < N - i; j++)
System.out.print(" ");
for (int j = 0; j < (2 * i - 1); j++)
System.out.print(k);
System.out.println();
k++;
}
}
}
Output
1 222 33333 4444444 555555555
while() loop Example
Program for simple while() loopProgram for simple while() loop
public class Demo
{
public static void main(String[] args)
{
int i = 0;
while(i < 5)
{
System.out.println("i is : " + i);
i++;
}
}
}
Output
i is : 0 i is : 1 i is : 2 i is : 3 i is : 4
Program to print tables though nested while() loop
Program to print tables though nested while() loop
public class Demo
{
public static void main(String args[])
{
int i = 1, j = 1;
System.out.println("Table");
while (i <= 2)
{
while (j <= 10) {
System.out.println(i + " * " + j + " = " + (i * j));
j++;
}
i++;
System.out.println("");
System.out.println("");
}
}
}
Output
Table 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10
do while loop Example
Program to print simple numbers through do whileProgram to print simple numbers through do while
public class Demo
{
public static void main(String args[]) {
int i = 1;
System.out.print("");
do {
System.out.println(i + " ");
i++;
} while (i <= 10);
}
}
Output
1 2 3 4 5 6 7 8 9 10
Program to print Triangle through do while
Program to print Triangle through do while
public class Demo
{
public static void main(String args[])
{
int i = 1, j = 1;
do
{
int k = 5;
do
{
System.out.print(" ");
k--;
} while (k >= i);
j = 1;
do
{
System.out.print(i + " ");
j++;
} while (j <= i);
System.out.println("");
i++;
} while (i <= 5);
}
}
Output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Enhance for() loop
Program to print simple array valuesProgram to print simple array values
public class Demo
{
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4 };
for (int i : arr)
{
System.out.println(i);
}
}
}
Output
1 2 3 4
Program to print array values with Scanner
Program to print array values with Scanner
import java.util.Scanner;
class Demo
{
public static void main(String arng[]) {
int value[] = new int[5];
Scanner data = new Scanner(System.in);
float sum = 0;
System.out.println("Enter 5 element of array");
// Enhanced for loop
for (int i : value) {
i = data.nextInt();
sum = sum + i;
}
System.out.println("Sum is " +sum);
}
}
Output
Enter 5 element of array 1 2 3 4 5 Sum is 15.0


