Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

CONDITIONAL

STATEMENTS

Types of Conditional Statements

 If Statement
 Switch Statement
 If Statement

Syntax of if-else statement Example:

if (condition) { public class ifsample1 {


statement_one; public static void main (String [] args) {
} int age = 60;
else { if(age >= 60) {
statement_two; System.out.println("You are entitled for pension benefits");
} }
else {
System.out.println("You are still young to retire from work.");
}
}
}
 If Statement

import java.util.Scanner;
public class sampleif { System.out.println();
public static void main (String [] args){ System.out.println("You are not qualified to vote");
Scanner scan = new Scanner(System.in); }
int true_age = 0; }
System.out.println(); }
System.out.println("\t Age Checker Program");
System.out.println();
System.out.println("Enter your age: ");
true_age = scan.nextInt();
if(true_age >= 18) {
System.out.println();
System.out.println("You are qualified to vote"); }
else {
 If Statement

public class if1 {

public static void main(String [] args){


int age = 23;

if(age < 18){


System.out.println("Your young");
}if(age == 18){
System.out.println("Your a teen");
}if(age > 18){
System.out.println("Your old");
        }
       }
}
 If Statement

public class if2 {

public static void main (String [] args){


String gender = "";
Scanner input = new Scanner(System.in);

System.out.print("What is your gender? ");


gender = input.nextLine();
if("m".equals(gender) || "M".equals(gender)){
System.out.println("Male");
} if("f".equals(gender)|| "F".equals(gender)){
            System.out.println("Female");
        }
    }    
}
 Switch Statement

Syntax of a switch statement:


default: {
Switch (expression) { default_statement;
case value_one: }
{ }
statement_one;
break;
}
case value_two:
{
statement_two;
break;
}
case value_three:
{
statement_three;
break;
}
 Switch Statement

public class sample_switch { case ‘D’ : case ‘d’:


public static void main(String[] args) { {
char marital_status=‘M’; System.out.println(“Divorce”);
switch(marital_status) { break;
case ‘S’ : case ‘s’: }
{ case ‘W’ : case ‘w’:
System.out.println(“Single”); {
break; System.out.println(“Widow”);
} break;
case ‘M’ : case ‘m’: default :
{ System.out.println(“Invalid Code! Try again”);
System.out.println(“Married”); }
break; }
} }
 Switch Statement

import java.util.Scanner; case "f":


public class switch2 {                 System.out.println("Female");
public static void main(String [] args){                 break;
Scanner input = new Scanner(System.in);             default:
System.out.println("Gender: ");                 System.out.println("Unknown input");
String gender = input.nextLine();          }
switch (gender){      }
case "m":    }
System.out.println("Male");
break;
 Switch Statement

public class switchStatement {


public static void main(String [] args){
int vegfruit = 1;
String vegfruits = "";
switch(vegfruit){
case 1:
System.out.println("1 - Fruit");
break;
case 2:
               System.out.println("2 - Vegetable");
            break;
            default:
                 System.out.println("Invalid");
        }
         System.out.println(vegfruits);
    }
}

You might also like