Opening Hours :7AM to 9PM
Control Statements are used to ramdom execution of the program .In java lamguage these are classified into following types.
1. Conditional Statements
2. Looping Statements
3. Un conditional Statements
Conditional statements are used check the condition whether it is true or false.
In java language these opeatrs are classified into following types.
1. if condition
2. if else condition
3. else if condition
4. nested if condition
5. switch expression
'if' is a keyword in java language .Which is used to execute one block of statements by negleting some other statements.
if(Condition) { Statement 1; Statement 2; .. .. }
In the above syntax 'if' condition is true the statements of 'if' block is execute.'if' the condition is false program terminated.
'if else' is a keyword in java language .Which is used to execute one block of statements between two blocks
if(Condition) { Statement 1; Statement 2; .. .. } else { Statement 3; Statement 4; .. .. }
In the above syntax 'if' condition is true the statements of 'if' block is execute.'if' the condition is false the statements of 'else' block is execute.
public class Main { public static void main(String args[]) { int x=10 if(x%2==0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); } } }
public class Main { public static void main(String args[]) { int x; x=Integer.parseInt(args[0]); if(x%2==0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); } } }
import java.util.*; public class Main { public static void main(String args[]) { int x; Scanner s=new Scanner(System.in); System.out.println("Enter x Value"); x=s.nextInt(); if(x%2==0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); } } }
public class Main { public static void main(String args[]) { int x=10,y=20; if(x>y) { System.out.println("x is Max"); } else { System.out.println("y is Max"); } } }
public class Main { public static void main(String args[]) { int x,y; x=Integer.parseInt(args[0]); y=Integer.parseInt(args[0]); if(x>y) { System.out.println("x is Max"); } else { System.out.println("y is Max"); } } }
import java.util.*; public class Main { public static void main(String args[]) { int x,y; Scanner s=new Scanner(System.in); System.out.println("Enter x ,y values"); x=s.nextInt(); y=s.nextInt(); if(x>y) { System.out.println("x is Max"); } else { System.out.println("y is Max"); } } }
'else if' is keyword in java language which is used to execute one block of statemnets amoung the multiple blocks.
if(Condition) { Statement 1; Statement 2; .. .. } else if(Condition) { Statement 3; Statement 4; .. .. } else if(Condition) { Statement 5; Statement 6; .. .. } else if(Condition) { Statement 7; Statement 8; .. .. } .. .. else { Statement 9; Statement 10; .. .. }
public class Main { public static void main(String args[]) { int age=24; if(age>=0 && age<=12) { System.out.println("You are a child"); } else if(age>=13 && age<=19) { System.out.println("You are a teenager"); } else if(age>=20 && age<=45) { System.out.println("You are younger"); } else if(age>=46 && age<=80) { System.out.println("You are old"); } else if(age>=81 && age<=100) { System.out.println("You are too old"); } else if(age>=101) { System.out.println("Invalid"); } else { System.out.println("Age should not be negative"); } } }
public class Main { public static void main(String args[])
{ int age;
age=Integer.parseInt(args[0]);
if(age>=0 && age<=12) { System.out.println("You are a child"); } else if(age>=13 && age<=19) { System.out.println("You are a teenager"); } else if(age>=20 && age<=45) { System.out.println("You are younger"); } else if(age>=46 && age<=80) { System.out.println("You are old"); } else if(age>=81 && age<=100) { System.out.println("You are too old"); } else if(age>=101) { System.out.println("Invalid"); } else { System.out.println("Age should not be negative"); } } }
Age Program:
0-12 child age
12-19 teen age
19-40 younger age
40-80 old age
80-100 too old age
above 100 invalid
-11 negative age
import java.util.*; public class Main { public static void main(String args[]) { int age; Scanner s=new Scanner(System.in); System.out.println("Enter Your age"); age=s.nextInt(); if(age>=0 && age<=12) { System.out.println("You are a child"); } else if(age>=13 && age<=19) { System.out.println("You are a teenager"); } else if(age>=20 && age<=45) { System.out.println("You are younger"); } else if(age>=46 && age<=80) { System.out.println("You are old"); } else if(age>=81 && age<=100) { System.out.println("You are too old"); } else if(age>=101) { System.out.println("Invalid"); } else { System.out.println("Age should not be negative"); } } }
One if condition with in another if condition is known as nested if.
if(condition) { if(condition) { Statement 1; Statement 2; .. .. } else { Statement 3; Statement 4; .. .. } } else { Statement 5; Statement 6; .. .. }
public class Main { public static void main(String args[]) { int x=10; if(x>0) { if(x%2==0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); } } else { System.out.println("Pls Enter Positive Number"); } } }
public class Main { public static void main(String args[]) { int x; x=Integer.parseInt(args[0]); if(x>0) { if(x%2==0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); } } else { System.out.println("Pls Enter Positive Number"); } } }
import java.util.*; public class Main { public static void main(String args[]) { int x; Scanner s=new Scanner(System.in); System.out.println("Enter x Value"); x=s.nextInt(); if(x>0) { if(x%2==0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); } } else { System.out.println("Pls Enter Positive Number"); } } }
Switch is a keyword is java language .Which is used to execute one block of statements amoung the multiple blocks. here switch is a choise expression.
switch(Choice) { case 1: Statement 1; Statement 2; .. .. break; case 2: Statement 3; Statement 4; .. .. break; case 3: Statement 5; Statement 6; .. .. break; .. .. default: Statement 7; Statement 8; .. .. }
public class Main { public static void main(String args[]) { int x=10,y; y=x%2; switch(y) { case 0: System.out.println("Even Number"); break; case 1: System.out.println("Odd Number"); break; case -1: System.out.println("Negative Odd Number"); break; default: System.out.println("Invalid Choice"); } } }
public class Main { public static void main(String args[]) { int x,y; x=Integer.parseInt(args[0]); y=x%2; switch(y) { case 0: System.out.println("Even Number"); break; case 1: System.out.println("Odd Number"); break; case -1: System.out.println("Negative Odd Number"); break; default: System.out.println("Invalid Choice"); } } }
import java.util.*; public class Main { public static void main(String args[]) { int x,y; System.out.println("Enter x value"); Scanner s=new Scanner(System.in); x=s.nextInt(); y=x%2; switch(y) { case 0: System.out.println("Even Number"); break; case 1: System.out.println("Odd Number"); break; case -1: System.out.println("Negative Odd Number"); break; default: System.out.println("Invalid Choice"); } } }