If and Else
Today we are going to discuss about if else and else if statement with some examples and have a better understanding of it.
if :-
This is a conditional statement which is use when you want some operation only be excute if your given condition is true. Let us see its syntax and try to understand it.
if (condition)
{
// Set of instructions
}
Now see if condition given turns out to be true only then we will enter the if block and execute the set of instructions.
Question 1: Write a program in Java to print even numbers present between 1 to 20 using if statement.
class Even
{
public static void main( String args [ ] )
{
int i ;
for(i = 1 ; i< 20; i++)
if ( i%2 == 0 )
{
System.out.println(i);
}
}
}
Dry Run
First i value will 1 then it will enter for loop and compiler will check the condition and as it is false it will not execute the if block.
Now i value will be 2 which is a even no and satisfying the condition of if block so it will print 2.
Process will repeat till value of i becomes 20.
Output
2
4
6
8
10
12
14
16
18
if else:-
As the the name suggest else is alternate statement of 'if' block i.e. if given condition in true block is not true then this block will execute. Let us see the syntax.
if(condition)
{
// Set of instructions 1
}
else
{
// Set of instructions 2
}
Now see if condition given in 'if' block turns out to false it will not execute the 'Set of instructions 1' then it will enter the else block without any checking of conditions and execute it i.e. 'Set of instructions 2' will be executed in the end.
Let us see some questions to understand if else better and see what we can do with the help of it.
Question 2 : Write a program in Java to take input from user their age and check if he or she is eligible to vote or not.If age is less than 18 then he or she is not eligible.
import java.util.Scanner;
class Age
{
public static void main( String args [ ] )
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter your age");
int age = obj.nextInt();
if (age < 18)
{
System.out.println("Not eligible to vote");
}
else
{
System.out println("Eligible to vote");
}
}
}
Dry Run and Output
First of all if you are thinking what is scanner than just know it's use to take input from the user.
Now let us analyse the program first we will ask user to input their age.
Suppose if user enters 16 then as the given condition in if block is satisfy it will print 'Not eligible to vote'. and then it will not enter the else block.
Now suppose if user enters 20 then given condition in if block will not be satisfy then compiler will look for the else block and it will print 'Eligible to vote'.
if and else if:-
First see its syntax
if(condition1)
{
// Set of instructions 1
}
else if (condition 2)
{
// Set of instructions 2
}
else
{
// Set of instructions 3
}
If condition 1 will be satisfy then 'if' block will be execute but if not then compiler will check condition given in 'else if' block and if its true it will execute that block but if no condition will be true 'else' block will execute.
Now let us see a question and understand it better.
Question 3: Write a program in Java to take input from user his or her salary and if salary is greater than 100000 increase salary by 15000 and if salary is greater than 50000 increase salary by 10000 and other than increase salary by 5000
import java.util.Scanner;
class Salary
{
public static void main( String args [ ] )
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter your salary");
int age = obj.nextInt();
if (salary > 100000)
{
salary = salary + 15000;
}
else if ( salary > 50000 && salary < 100000)
{
salary = salary + 10000;
}
else
{
salary = salary + 5000;
}
System.out.println(salary);
}
}
Dry Run and Output
First we have taken input from user by asking them to 'Enter your salary' via scanner.
Now if user enters 110000 then it will check the if block and as condition will be satify compiler will execute the if block and will increase the salary by 15000 and then it check no more and will print the salary. (125000 in this case)
Now suppose if user enter salary 60000 compiler will check first if block but as it is satisfying the condition it will come to else if block but in this case condition is true, so it will increase salary by 10000 and will check no more and will print 70000.
Now suppose if user enters salary 20000 then compiler will check first both if and else if block but as none of them are true it will execute else block and will print salary 25000.
nice one
ReplyDelete