Java Online Compiler

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 condition

'if' is a keyword in java language .Which is used to execute one block of statements by negleting someother 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.

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
Conditional

import java.util.*;                               
class Main
{
 public static void main(String args[])
 {
  int x;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x values");
  x=s.nextInt();
 	if(x%2==0)
    {
    System.out.println("Even number"+x);
    }
    else
    {
    System.out.println("Odd number"+x);
    }
    
 }
}

Max Number Program Using IF ELSE Condition

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

Vowel & Consonant Program Using IF ELSE Condition

import java.util.*;                               
class Main
{
 public static void main(String args[])
 {
  char x;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter Character");
  x=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");
    }
    
 }
}

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
{
..
..
..
}
ElseIF Condition

import java.util.*;
class Elseif
{
public static void main(String args[])
{
 int age;
 char gender;
 String name, location;
 Scanner values=new Scanner(System.in);
 System.out.println("Enter name:");
 name=values.next();
 System.out.println("Enter location");
 location=values.next();
 System.out.println("Enter gender:");
 gender=values.next().charAt(0);
 System.out.println("Enter age:");
 age=values.nextInt();
 System.out.println("Your name is "+name);
 System.out.println("Your location is "+location);
 System.out.println("Your gender is "+gender);
 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");
 }
}
} 

                            
                            

Nested if condition

One if condition with in another if condition is known as nested if.
syntax:
if(condition)
{
    if(condition)
    {
    ..
    ..
    }
    else
    {
    ..
    ..
    }
}
else
{
..
..
..
}
Nested IF Condition

import java.util.*;
class Elseif
{
public static void main(String args[])
{
 int age;
 char gender;
 String name, location;
 Scanner values=new Scanner(System.in);
 System.out.println("Enter name:");
 name=values.next();
 System.out.println("Enter location");
 location=values.next();
 System.out.println("Enter gender:");
 gender=values.next().charAt(0);
 System.out.println("Enter age:");
 age=values.nextInt();
 System.out.println("Your name is "+name);
 System.out.println("Your location is "+location);
 System.out.println("Your gender is "+gender);
 if(age>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 
 {
  System.out.println("Invalid");
 }
 }
 else
 {
  System.out.println("Age should not be negative");
 }
 
 
}
} 

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;
}
ODD or EVEN number Using Switch

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

Vowel Consonant Program Using Switch

import java.util.*;
class OddEven
{
 public static void main(String args[])
 {
 char ch;
 System.out.println("Enter x value");
 Scanner s=new Scanner(System.in);
 ch=s.next().charAt(0);
 
 switch(ch)
 {
 case 'a':
 case 'e':
 case 'i':
 case 'o':
 case 'u':
 case 'A':
 case 'E':
 case 'I':
 case 'O':
 case 'U':
        System.out.println("Vowel");
        break;
 default:
    System.out.println("Consonant");
 }
  
 }
}

SImple Calculator Application Using Switch

import java.util.*;
class Switchchoice
{
public static void main(String args[])
{
int x,y,choice;
System.out.println("\t_______\n\tcalci\n\t_______");
System.out.println("1.addition");
System.out.println("2.subtraction");
System.out.println("3.multiplication");
System.out.println("4.division");
System.out.println("5.modulus");
Scanner s=new Scanner(System.in);
System.out.println("enter your choice");
choice=s.nextInt();
switch(choice)
{
case 1:
    System.out.println("enter x,y values");
    x=s.nextInt();
    y=s.nextInt();
    System.out.println("addition of two numbers is"+(x+y));
    break;
case 2:
    System.out.println("enter x,y values");
    x=s.nextInt();
    y=s.nextInt();
    System.out.println("subtraction of two numbers is"+(x-y));
    break;
case 3:
    System.out.println("enter x,y values");
    x=s.nextInt();
    y=s.nextInt();
    System.out.println("multiplication of two numbers is"+(x*y));
    break;
case 4:
    System.out.println("enter x,y values");
    x=s.nextInt();
    y=s.nextInt();
    System.out.println("division of two numbers is"+(x/y));
    break;
case 5:
    System.out.println("enter x,y values");
    x=s.nextInt();
    y=s.nextInt();
    System.out.println("modulus of two numbers is"+(x%y));
    break;
default:
    System.out.println("invalid");
}
}
}


Display Employee Details(You Must Read The Values From Keyboard Using Scanner Class).

import java.util.*;
class PersonDetail
{
public static void main(String args[])
{
String name;
int age;
char gender;
float salary;
Scanner y=new Scanner(System.in);
System.out.println(" enter name of the person");
name=y.next();
System.out.println("enter age of the person");
age=y.nextInt();
System.out.println("enter gender of the person");
gender=y.next().charAt(0);
System.out.println("enter salary of the person");
salary=y.nextFloat();
System.out.println("name of the person is"+name);
System.out.println("age of the person is"+age);
System.out.println("gender of the person is"+gender);
System.out.println("salary of the person is"+salary);
}
}

Display Employee Details(You Must Read The Values Using CommandLine Arguments).

class PersonDetails
{
 public static void main(String args[])
 {
  int age;
  float salary;
  String name;
  char gender;
  name=args[0];
  age=Integer.parseInt(args[1]);
  gender=args[2].charAt(0);
  marks=Float.parseFloat(args[3]);
  System.out.println("Name of the person is "+name);
  System.out.println("Age of the person is "+age);
  System.out.println("Gender of the person is "+gender);
  System.out.println("Salary of the person is "+salary);
 }
}