0% found this document useful (0 votes)
51 views

007 - Java Selection Statement

The document discusses various selection statements in Java including if, if-else, if-else-if ladder, nested if, and switch statements. Code examples are provided to demonstrate the syntax and usage of each statement type for tasks like checking eligibility, grading systems, months, and vowels.

Uploaded by

Wai Linn Khant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

007 - Java Selection Statement

The document discusses various selection statements in Java including if, if-else, if-else-if ladder, nested if, and switch statements. Code examples are provided to demonstrate the syntax and usage of each statement type for tasks like checking eligibility, grading systems, months, and vowels.

Uploaded by

Wai Linn Khant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Java Selection Statement

Java if statement
The Java if statement tests the condition.
It executes the if block if condition is true.
Syntax:
if(condition){
//code to be executed
}
Java Program to demonstrate the use of if statement.

public class IfExample


{
public static void main(String[] args)
{
int age=20;
if(age>18)
{
System.out.print("Age is greater than 18");
}
}
}
Java if-else Statement
The Java if-else statement also tests the condition.
It executes the if block if condition is true otherwise else block is executed.
Syntax:
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Java if-else Statement
Java Program to demonstrate the use of if-else statement.
public class IfElseExample {
public static void main(String[] args)
{
int number=13;
if(number%2==0)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
}
}
Leap Year Example
A year is leap, if it is divisible by 4 and 400. But, not by 100.
public class LeapYearExample {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0))
{
System.out.println("LEAP YEAR");
}
else
{
System.out.println("COMMON YEAR");
}
}
}
Java if-else-if-else Statement
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
else{
//code to be executed if all the conditions are false
}
Java Program to demonstrate the use of If else-if ladder
It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.
public class IfElseIfExample {
public static void main(String[] args) {
int marks=65;
if(marks<50)
{
System.out.println("fail");
}
else if(marks>=50 && marks<60)
{
System.out.println("D grade");
}
else if(marks>=60 && marks<70)
{
System.out.println("C grade");
}
else if(marks>=70 && marks<80)
{
System.out.println("B grade");
}
else if(marks>=80 && marks<90)
{
System.out.println("A grade");
}
else if(marks>=90 && marks<100)
{
System.out.println("A+ grade");
}
else{
System.out.println("Invalid!");
}
}
}
import java.util.Scanner;
public class PassFail
{
public static void main(String[] args)
{
int mark;
Scanner input = new Scanner(System.in);
System.out.print("Enter your Mark: ");
mark = input.nextInt();
if (mark >= 40)
System.out.print("You Pass the Exam!");
if (mark < 40)
System.out.print("You Fail the Exam!");
}
}
Java Nested if statement
The nested if statement represents the if block within another if block.
The inner if block condition executes only when outer if block condition is true.
Syntax:
if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}
Program to demonstrate the use of Nested If Statement

public class JavaNestedIfExample {


public static void main(String[] args) {
int age=20;
int weight=80;
if(age>=18)
{
if(weight>50)
{
System.out.println("You are eligible to donate blood");
}
}
}
}
Program to demonstrate the use of Nested If Statement
public class JavaNestedIfExample2 {
public static void main(String[] args) {
int age=25;
int weight=48;
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
} else{
System.out.println("You are not eligible to donate blood");
}
} else{
System.out.println("Age must be greater than 18");
}
}
}
Java Switch Statement
Syntax:
switch(expression){
case value1: //code to be executed;
break; //optional
case value2: //code to be executed;
break; //optional
default:
code to be executed if all cases are not matched;
}
Java Program to demonstrate the example of Switch statement
public class SwitchMonthExample {
public static void main(String[] args) {
int month=7;
String monthString="";
switch(month){
case 1: monthString="1 - January";
break;
case 2: monthString="2 - February";
break;
case 3: monthString="3 - March";
break;
case 4: monthString="4 - April";
break;
case 5: monthString="5 - May";
break;
case 6: monthString="6 - June";
break;
case 7: monthString="7 - July";
break;
case 8: monthString="8 - August";
break;
case 9: monthString="9 - September";
break;
case 10: monthString="10 - October";
break;
case 11: monthString="11 - November";
break;
case 12: monthString="12 - December";
break;
default: System.out.println("Invalid Month!");
}
System.out.println(monthString);
}
}
public class VowelTest
{
public static void main(String[] args) throws Exception
{
char ch;
System.out.print("Enter a Character: ");
ch = (char)System.in.read();
/*if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
System.out.print("Vowel");
else
System.out.print("Consonent");*/
switch(ch)
{
case 'a': System.out.print("Vowel"); break;
case 'e': System.out.print("Vowel"); break;
case 'i': System.out.print("Vowel"); break;
case 'o': System.out.print("Vowel"); break;
case 'u': System.out.print("Vowel"); break;
default: System.out.print("Consonent");
}
}
}
Java Nested Switch Statement
o We can use switch statement inside other switch statement in Java.
o It is known as nested switch statement.

public class NestedSwitchExample {


public static void main(String args[])
{
char branch = 'C';
int collegeYear = 4;
switch( collegeYear )
{
case 1: System.out.println("English, Maths, Science");
break;
case 2:
switch( branch )
{
case 'C':
System.out.println("Operating System, Java, Data Structure");
break;
case 'E':
System.out.println("Micro processors, Logic switching theory");
break;
case 'M':
System.out.println("Drawing, Manufacturing Machines");
break;
}
break;
case 3:
switch( branch )
{
case 'C':
System.out.println("Computer Organization, MultiMedia");
break;
case 'E':
System.out.println("Fundamentals of Logic Design, Microelectronics");
break;
case 'M':
System.out.println("Internal Combustion Engines, Mechanical Vibration");
break;
}
break;
case 4:
switch( branch )
{
case 'C':
System.out.println("Data Communication and Networks, MultiMedia");
break;
case 'E':
System.out.println("Embedded System, Image Processing");
break;
case 'M':
System.out.println("Production Technology, Thermal Engineering");
break;
}
break;
}
}
}

You might also like