Selection Statements
- The if and if-else statements
- The if-else statements
- The if-else-if statements
- The switch statements
The if statements
The if-else statements
- package com.deepak.main;
- import java.util.Scanner;
- public class IfElseDemo
- {
- public static void main( String[] args )
- {
- int age;
- Scanner inputDevice = new Scanner( System.in );
- System.out.print( "Please enter Age: " );
- age = inputDevice.nextInt();
- if ( age >= 18 )
- System.out.println( "above 18 " );
- else
- System.out.println( "below 18" );
- }
- }
The if-else-if statements
- package com.deepak.main;
- import java.util.Scanner;
- public class IfElseIfDemo
- {
- public static void main( String[] args )
- {
- int age;
- Scanner inputDevice = new Scanner( System.in );
- System.out.print( "Please enter Age: " );
- age = inputDevice.nextInt();
- if ( age >= 18 && age <=35 )
- System.out.println( "between 18-35 " );
- else if(age >35 && age <=60)
- System.out.println("between 36-60");
- else
- System.out.println( "not matched" );
- }
- }
The Switch Statements
- package com.deepak.main;
- import java.util.Scanner;
- public class SwitchDemo
- {
- public static void main( String[] args )
- {
- int age;
- Scanner inputDevice = new Scanner( System.in );
- System.out.print( "Please enter Age: " );
- age = inputDevice.nextInt();
- switch ( age )
- {
- case 18:
- System.out.println( "age 18" );
- break;
- case 19:
- System.out.println( "age 19" );
- break;
- default:
- System.out.println( "not matched" );
- break;
- }
- }
- }
0 comments:
Post a Comment