27
Nov
If-Else
Conditional logic means to execute an operation depends on its prerequisite conditions. For example, if school is closed tomorrow, then we will play.
in java, if-else logic follows this pattern:
A very simple program :
if(condition){
//statement
}
else if(condition){
//statement
}
else{
//statement
}
1. Write a program to check if a number is even or odd
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Input a number:");
Scanner input=new Scanner(System.in);
int number=input.nextInt();
if(number%2==0){
System.out.println("This is even number");
}
else{
System.out.println("This is odd number");
}
}
}
2. Write a program to check if inputted year is leap-year or not
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Input year:");
Scanner input=new Scanner(System.in);
int year=input.nextInt();
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
System.out.println("Its leapyear");
}
else{
System.out.println("Its not leapyear");
}
}
}
3. Write a program to find GPA from marks
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Input marks:");
Scanner input=new Scanner(System.in);
int marks=input.nextInt();
if(marks>=80){
System.out.println("4.00");
}
else if(marks>=75){
System.out.println("3.75");
}
else if(marks>=70){
System.out.println("3.50");
}
else if(marks>=65){
System.out.println("3.25");
}
else if(marks>=60){
System.out.println("3.00");
}
else if(marks>=55){
System.out.println("2.75");
}
else if(marks>=50){
System.out.println("2.50");
}
else if(marks>=45){
System.out.println("2.25");
}
else if(marks>=40){
System.out.println("2.00");
}
else{
System.out.println("0.0");
}
}
}
Switch Case: Switch case statement is used when there are multiple conditions and needs to perform different action based on the given condition. Pattern:
switch(case_no)
{
case value1: //code to be executed;
break;
case value2: //code to be executed;
break;
default: //code to be executed if all cases are not matched;
}
1. Write a program to find day.
public class Main {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 1:
System.out.println("Today is Saturday");
break;
case 2:
System.out.println("Today is Sunday");
break;
case 3:
System.out.println("Today is Monday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
}
}
2. Write a program to select light-fan switch
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Please choose a switch:\n1\n2\n3");
Scanner input=new Scanner(System.in);
int sw=input.nextInt();
switch (sw) {
case 1:
System.out.println("You choose light-1 switch");
break;
case 2:
System.out.println("You choose Fan switch");
break;
case 3:
System.out.println("You choose light-2 switch");
break;
default:
System.out.println("Invalid switch selected");
}
}
}
Output:
Please choose a switch:
1
2
3
3
You choose light-2 switch
Leave a Reply