• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Control Statements In java


Control Statements:

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.Unconditional Statements



Key Points

  • What is Conditonal Statements
  • What is Looping Statements
  • What is Unconditional Statements


Image

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

Key Points

  • What is if Condition
  • What is if else Condition
  • What is else if Condition
  • What is nested if Condition
  • What is switch Expression


Image

if condition
'if' is a keyword in java language .Which is used to execute one block of statements by negleting some other statements.
Syntax:

if(Condition)
{
..
..
..
}


In the above syntax 'if' condition is true the statements of 'if' block is execute.'if' the condition is false program terminated.

Key Points

  • What is if Condition


Image
if else condition

'if else' is a keyword in java language .Which is used to execute one block of statements between two blocks
Syntax:

if(Condition)
{
..
..
}
else
{
..
..
}

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.



ODD or Even Number Program Using IF ELSE Condition

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");
}
}
}


Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with CommandLine Arguments

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");
}
}
}


Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with Scanner Class

import java.util.*;

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");
}
}
}


Output:
Even Number

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");
}
}
}


Output:
y is Max

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");
}
}
}


Output:
y is Max

import java.util.*;
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");
}
}
}


Output:
Enter x ,y values
10 20
y is Max
else if condition
'else if' is keyword in java language which is used to execute one block of statemnets amoung the multiple blocks.
Syntax:

if(Condition)
{
..
..
}
else if(Condition)
{
..
..
}
else if(Condition)
{
..
..
}
else if(Condition)
{
..
..
}
..
..
else
{
..
..
}

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");
}
}
}


Output:
You are younger


Output:
The PostIncrement value is 10

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");
}
}
}


Output:
You are younger

import java.util.*;
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");
}
}
}


Output:
Enter Your age
24
You are younger
Nested if Condition
One if condition with in another if condition is known as nested if.
Syntax:

if(condition)
{
     if(condition)
    {
     ..
    ..
     }
     else
     {
    ..
    ..
    }
}
else
{
..
..
..
}

ODD or Even Number Program Using IF ELSE Condition(Value Must Be Positive)

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");
}
}
}


Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with CommandLine Arguments (Value Must Be Positive)

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");
}
}
}


Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with Scanner Class(Value Must Be Positive)

import java.util.*;

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");
}
}
}


Output:
Even Number
Switch Expression
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.
Syntax:

switch(Choice)
{
case 1:
     Statements;
      break;
case 2:
     Statements;
      break;
case 3:
     Statements;
      break;
.
.
default:
     Statements;
}

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;
default:
     System.out.println("Invalid Choice");
}
}
}


Output:
Even Number

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;
default:
     System.out.println("Invalid Choice");
}
}
}


Output:
Even Number

import java.util.*;
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;
default:
     System.out.println("Invalid Choice");
}
}
}


Output:
Enter x Value
10
Even Number
if else Practice Program-1

Check the given number is valid or invalid(value must between 20 and 30)

class Main
{
public static void main(String args[])
{
int x=24;
if(x>=20 && x<=30)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Valid Number

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
if(x>=20 && x<=30)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Valid Number

import java.util.*;

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>=20 && x<=30)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Enter x Value
24
Valid Number
if else Practice Program-2

Check the given number is valid or invalid(value must divisible by 2 and 3)

class Main
{
public static void main(String args[])
{
int x=24;
if(x%2==0 && x%3==0)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Valid Number

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
if(x%2==0 && x%3==0)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Valid Number

import java.util.*;

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 && x%3==0)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Enter x Value
24
Valid Number
if else Practice Program-3

Check the given number is valid or invalid(value must divisible by 2 and 3 not 4)

class Main
{
public static void main(String args[])
{
int x=24;
if(x%2==0 && x%3==0 && x%4 !=0)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
InValid Number

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
if(x%2==0 && x%3==0 && x%4 !=0)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
InValid Number

import java.util.*;

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 && x%3==0 && x%4 !=0)
{
System.out.println("Valid Number");
}
else
{
System.out.println("Invalid Number");
}
}
}


Output:
Enter x Value
24
InValid Number
if else Practice Program-4

Check the given character is vowel or consonant

class Main
{
public static void main(String args[])
{
char ch='E';
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' ||ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
System.out.println("Vowel");
}
else
{
System.out.println("Consonant");
}
}
}


Output:
Vowel

class Main
{
public static void main(String args[])
{
char ch;
ch=args[0].charAt(0);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' ||ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
System.out.println("Vowel");
}
else
{
System.out.println("Consonant");
}
}
}


Output:
Vowel

import java.util.*;
class Main
{
public static void main(String args[])
{
char ch;
Scanner s=new Scanner(System.in);
System.out.println("Enter Character");
ch=s.next().charAt(0);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' ||ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
System.out.println("Vowel");
}
else
{
System.out.println("Consonant");
}
}
}


Output:
Enter Character
E
Vowel
if else Practice Program-5

Check the given character is Small Letter or Not

class Main
{
public static void main(String args[])
{
char ch='k';
if(x>='a' && x<='z')
{
System.out.println("Small Letter");
}
else
{
System.out.println("Not a Small Letter ");
}
}
}


Output:
Small Letter

class Main
{
public static void main(String args[])
{
char ch;
ch=args[0].charAt(0);
if(x>='a' && x<='z')
{
System.out.println("Small Letter");
}
else
{
System.out.println("Not a Small Letter ");
}
}
}


Output:
Small Letter

import java.util.*;
class Main
{
public static void main(String args[])
{
char ch;
Scanner s=new Scanner(System.in);
System.out.println("Enter Character");
ch=s.next().charAt(0);
if(x>='a' && x<='z')
{
System.out.println("Small Letter");
}
else
{
System.out.println("Not a Small Letter ");
}
}
}


Output:
Enter Character
k
Small Letter