IF-Else statement-15 Programs in java
Introduction: The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false.
Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.ction:If-else statement.
SYNTAX:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Working of if-else statements
Control falls into the if block.
The flow jumps to Condition.
Condition is tested.
If Condition yields true, goto Step 4.
If Condition yields false, goto Step 5.
The if-block or the body inside the if is executed.
The else block or the body inside the else is executed.
Flow exits the if-else block.
Diagramatic Representaion:
PROGRAM 1:
CODE:
class IfDemo {
public static void main(String args[])
{
int i = 20;
if (i > 15)
System.out.println(“i is smaller than 15”);
}
}
OUTPUT:
i is smaller than 15
Note:Here condition is true that’s why it has executed.other wise it will not print anything.
Program :2//if statement example
// Java program to illustrate If statement
class If {
public static void main(String args[])
{
String str = “GeeksforGeeks”;
int i = 4;
// if block
if (i == 4) {
i++;
System.out.println(str);
}
// Executed by default
System.out.println(“i = “ + i);
}
}
OUTPUT:
GeeksforGeeks
i = 5
PROGRAM 3://if statement not printting anything if condition false
CODE:
class IfElseDemo {
public static void main(String args[])
{
int i = 20;
if (i < 15)
System.out.println(“i is smaller than 15”);
}
}
OUTPUT:
*Note:Condition is false that’s why they didn’t print anything.thats why concept if else come let see if-else example.
PROGRAM 4://simple example of if-else
Here we are checking is 20 value grater than 15 then if statement will execute.
class IfElseDemo {
public static void main(String args[])
{
int i = 20;
if (i > 15)
System.out.println(“i is smaller than 15”);
else
System.out.println(“i is greater than 15”);
System.out.println(“Outside if-else block”);
}
}
OUTPUT:
i is smaller than 15
Outside if-else block
PROGRAM 5://if else statement porgram for checking strings
// Java program to illustrate if-else statement
class IfElseStringDemo {
public static void main(String args[])
{
String str = “geeksforgeeks”;
if (str == “geeks”)
System.out.println(“Hello geek”);
else
System.out.println(“Welcome to GeeksforGeeks”);
}
}
OUTPUT:
Welcome to GeeksforGeeks
PROGRAM 6:
CODE:
//A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number.
public class GFG {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
System.out.println(“even number”);
}else{
System.out.println(“odd number”);
}
}
}
OUTPUT:
odd number
PROGRAM 7://Check given year is leap year or not usinf if else statement
CODE:
public class LeapYearExample {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){ //leap year condition
System.out.println(“LEAP YEAR”);
}
else{
System.out.println(“COMMON YEAR”);
}
}
}
OUTPUT:
LEAP YEAR
PROGRAM 8:// Using Ternary Operator
Ans:This program is checking odd or even number using ternary operator.Basically ternary operator works like if else statement.
If condition true then 1st statement will execute otherewise second.
CODE:
public class IfElseTernaryExample {
public static void main(String[] args) {
int number=13;
//Using ternary operator
String output=(number%2==0)?”even number”:”odd number”;
System.out.println(output);
}
}
OUTPUT:
odd number
PROGRAM 9:
CODE:
import java.util.Scanner;
import java.io.*;
public class IfElseTernaryExample {
public static void main(String[] args) {
int a, b, c;
int n;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the three number”);
//reading the number of elements from the that we want to enter
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a>b && a>c)
{
System.out.println(a);
}
if(b>a && b > c)
{
System.out.print(b);
}
if(c>a && c>b)
{
System.out.println(c);
}
if(a == b && a == c)
{
System.out.println(“All are equal”);
}
}
}
OUTPUT:
Enter the three number: 5 5 5
All are equal
TYPE 3:If:Else-If:else statement
SYNTAX:
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
PROGRAM 10://Checking integer is equal or not
COD // Java program to illustrate if-else-if ladder
Here I am comparing one by one value first in if it matches then it will print .Otherwise it will go to else if statement if not matches this integer also then else statement
import java.io.*;
class ifelseifelseDemo{
public static void main(String[] args)
{
// initializing expression
int i = 20;
// condition 1
if (i == 10)
System.out.println(“i is 10\n”);
// condition 2
else if (i == 15)
System.out.println(“i is 15\n”);
// condition 3
else if (i == 20)
System.out.println(“i is 20\n”);
else
System.out.println(“i is not present\n”);
System.out.println(“Outside if-else-if”);
}
}
OUTPUT:
i is 20
Outside if-else-if
Note*:
1.Program starts.
2. i is initialized to 20.
3. condition 1 is checked. 20 == 10, yields false.
4. condition 2 is checked. 20 == 15, yields false.
5. condition 3 is checked. 20 == 20, yields true.
5.a) “i is 20” gets printed.
6. “Outside if-else-if” gets printed.
7. Program ends.
PROGRAM 11://If_ele-if_else statement usig less(>) than and greater than operator(<).
CODE:
// Java program to illustrate if-else-if ladder
import java.io.*;
class GFG {
public static void main(String[] args)
{
// initializing expression
int i = 20;
// condition 1
if (i < 10)
System.out.println(“i is less than 10\n”);
// condition 2
else if (i < 15)
System.out.println(“i is less than 15\n”);
// condition 3
else if (i < 20)
System.out.println(“i is less than 20\n”);
else
System.out.println(“i is greater than “
+ “or equal to 20\n”);
System.out.println(“Outside if-else-if”);
}
}
OUTPUT:
i is greater than or equal to 20
Outside if-else-if
PROGRAM 12://
CODE://Multiple if-else statement
In this example we used inside first if statemnt another if-else statement .In program you will clear.
import java.util.Scanner;
import java.io.*;
public class IfElseTernaryExample {
public static void main(String[] args) {
int num=1;
if(num<10)
{
if(num==1)
{
System.out.printf(“The value is:%d\n”,num);
}
else
{
System.out.println(“The value is greater than 1”);
}
}
else
{
System.out.println(“The value is greater than 10”);
}
}
}
OUTPUT: The value is:1
PROGRAM 13://Inside for loop if else statement
Ans:Hre we are taking numbers from 1 to 10 and then printing text if values is less than 5 other wise it will execute else statement.
CODE:
import java.util.Scanner;
import java.io.*;
public class IfElseTernaryExample {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
if(i < 5) {
System.out.println(“I am smaller than five”);
}
else /*Or perhaps if an else if, ‘else if(i > 7){System.out.println(‘I am bigger than seven’);}’*/{
System.out.println(“I am bigger than five”);
}
}
}
}
OUTPUT:
I am smaller than five
I am smaller than five
I am smaller than five
I am smaller than five
I am smaller than five
I am bigger than five
I am bigger than five
I am bigger than five
I am bigger than five
I am bigger than five
PROGRAM 14://In while loop if else statement
This program same as previous one only heare we have use while loop.same program only while loop hava used for understanding to you.
CODE:
import java.util.Scanner;
import java.io.*;
public class IfElseTernaryExample {
public static void main(String[] args) {
int i=1;
while(i<10) {
if(i < 5) {
System.out.println(“I am smaller than five”);
}
else /*Or perhaps if an else if, ‘else if(i > 7){System.out.println(‘I am bigger than seven’);}’*/{
System.out.println(“I am bigger than five”);
}
i++;
}
}
}
OUTPUT:
I am smaller than five
I am smaller than five
I am smaller than five
I am smaller than five
I am bigger than five
I am bigger than five
I am bigger than five
I am bigger than five
I am bigger than five
PROGRAM 15:// Java Grade Program
Given below is a java program to find grade of a student using if else ladder according to the percentage of marks obtained.
Marks
Grade
Marks>= 90
A — Excellent
90>Marks>=80
B — Very Good
80>Marks>=70
C — Good
70>Marks>=60
D — Satisfactory
60>Marks>=50
E — Work Hard
50>Marks>=40
F — Just Passed
Marks<40
Failed
CODE:
import java.util.Scanner;
public class Percentage_status{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(“Enter percentage marks”);
double percentage = scan.nextDouble();
if(percentage >= 90){
System.out.println(“Excellent: Grade A”);
}else if(percentage < 90 && percentage >= 80){
System.out.println(“Very Good: Grade B”);
}else if(percentage < 80 && percentage >= 70){
System.out.println(“Good: Grade C”);
}else if(percentage < 70 && percentage >= 60){
System.out.println(“Satisfactory: Grade D”);
}else if(percentage < 60 && percentage >= 50){
System.out.println(“Work Hard: Grade E”);
}else if(percentage < 50 && percentage >= 40){
System.out.println(“Just Passed: Grade F”);
}else {
System.out.println(“Failed!”);
}
}
}
OUTPUT:
Enter percentage marks
92
Excellent: Grade A
THANK YOU