• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Control Statements In java

Java Control Statements

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. Un conditional Statements



MSK TechnologiesTrainings


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)
{
    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.

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)
{
    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.



ODD or Even Number Program Using IF ELSE Condition
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");
        }
    }
}

                                

Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with CommandLine Arguments
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");
        }
    }
}

                                

Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with Scanner Class
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");
        }
    }
}

                                

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

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

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

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)
{
    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;
    ..
    ..
}
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

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

                                

Output:
You are younger
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

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

Output:
You are younger

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

                                

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)
    {
        Statement 1;
        Statement 2;
        ..
        .. 
       
    }
    else
    {
        Statement 3;
        Statement 4;
        ..
        ..
    }
}
else
{
    Statement 5;
    Statement 6;
    ..
    ..
}
ODD or Even Number Program Using IF ELSE Condition(Value Must Be Positive)
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");
        }
    }
}

                                

Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with CommandLine Arguments (Value Must Be Positive)
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");
    }
}
}

                                

Output:
Even Number
ODD or Even Number Program Using IF ELSE Condition with Scanner Class(Value Must Be Positive)
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");
        }
    
    }
}

                                

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

                                

Output:
Even Number

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

                                

Output:
Even Number
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");
        }
    }       
}

                                

Output:
Enter x Value
10
Even Number