In Java, if statement is used for testing the conditions. The condition matches the statement it returns true else it returns false. There are four types of If statement they are:
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
- switch statement
1. if Statement
In Java, if statement is used for testing conditions. It is used for only true condition.
Syntax:
if(condition)
{
//Block of java statements
}
Ex1:
public class IfDemo1
{
public static void main(String[] args)
{
int marks=70;
if(marks > 65)
{
System.out.print("First division");
}
}
}
output:
First division
Ex2:
class simif
{
public static void main(String args[])
{
int x = 20;
int y = 22;
if (x<y)
{
System.out.println("Variable x is less than y");
}
}
}
Output:
Variable x is less than y
2. if-else Statement
In Java, the if-else statement is used for testing conditions. It is used for true as well as for false condition.
Syntax:
if(condition)
{
//code for true
}
else
{
//code for false
}
Ex1:
class IfElse1
{
public static void main(String[] args)
{
int number = -10;
// checks if number is greater than 0
if (number > 0)
{
System.out.println("The number is positive.");
}
else
{
System.out.println("The number is not positive.");
}
}
}
Output:
The number is positive
Ex2:
import java.util.Scanner;
class Ife
{
public static void main(String[] args)
{
int marksObtained, passingMarks;
passingMarks = 40;
Scanner input = new Scanner(System.in);
System.out.println("Input marks scored by Student :");
marksObtained = input.nextInt();
if (marksObtained >= passingMarks)
{
System.out.println("Passed the Exam");
}
else
{
System.out.println("Failed the Exam");
}
}
}
Output:
C:\Users\Student\Desktop\java prg>javac Ife.java
C:\Users\Student\Desktop\java prg>java Ife
Input marks scored by Student :
55
Exam the Passed.
3. if-else-if ladder Statement
In Java, the if-else-if ladder statement is used for testing conditions. It is used for testing one condition from multiple statements.
Syntax:
if(condition1)
{
//code for if condition1 is true
}
else if(condition2)
{
//code for if condition2 is true
}
else if(condition3)
{
//code for if condition3 is true
}
...
else
{
//code for all the false conditions
}
Ex:
class Ladder
{
public static void main(String[] args)
{
int number = 0;
// checks if number is greater than 0 if (number > 0)
{
System.out.println("The number is positive.");
}
// checks if number is less than 0 else if (number < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is 0.");
}
}
}
Output:
The number is 0.
Ex:2
class Test
{
public static void main(String arg[])
{
int a=11,b=222,c=33,d=445,e=55;
if(a>b && a>c && a>d && a>e)
{
System.out.print(a);
}
else if(b>c && b>d && b>e)
{
System.out.print(b);
}
else if(c>d && c>e)
{
System.out.print(c);
}
else if(d>e)
{
System.out.print(d);
}
else
{
System.out.print(e);
}
System.out.println(" is biggest number of 5 numbers");
}
}
Output:
C:\Users\Student\Desktop\java prg>javac Test.java
C:\Users\Student\Desktop\java prg>java Test
445 is biggest number of 5 numbers
4. Nested if statement
In Java, the Nested if statement is used for testing conditions. In this, one if block is created inside another if block when the outer block is true then only the inner block is executed.
Syntax:
if(condition)
{
//statement
if(condition)
{
//statement
}
}
Ex:
class Number
{
public static void main(String[] args)
{
// declaring double type variables
double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;
// checks if n1 is greater than or equal to n2
if (n1 >= n2)
{
// if...else statement inside the if block
// checks if n1 is greater than or equal to n3
if (n1 >= n3)
{
largestNumber = n1;
}
else
{
largestNumber = n3;
}
}
else
{
// if..else statement inside else block
// checks if n2 is greater than or equal to n3
if (n2 >= n3)
{
largestNumber = n2;
}
else
{
largestNumber = n3;
}
}
System.out.println("The largest number is " + largestNumber);
}
}
Output:
The largest number is 4.5
Ex2 using command line arguments:
class IfSample
{
public static void main(String a[])
{
int percentage=Integer.parseInt(a[0]);
if(percentage>=75)
{
if(percentage>=75 && percentage<85)
{
System.out.println("grade A1");
}
else
{
System.out.println("Grade A");
}
}
else
{
if(percentage>=65 && percentage<75)
{
System.out.println("grade B");
}
else if(percentage>=40 && percentage<65)
{
System.out.println("Grade c");
}
else
{
System.out.println("Fail");
}
}
}
}
Output:
C:\Users\Student\Desktop\java prg>javac IfSample.java
C:\Users\Student\Desktop\java prg>java IfSample 66
grade B
Ex2 with out using command line arguments:
import java.util.Scanner;
class IfSample
{
public static void main(String a[])
{
int percentage;
Scanner input = new Scanner(System.in);
System.out.println("Input percentage :");
percentage = input.nextInt();
if(percentage>=75)
{
if(percentage>=75 && percentage<85)
{
System.out.println("grade A1");
}
else
{
System.out.println("Grade A");
}
}
else
{
if(percentage>=65 && percentage<75)
{
System.out.println("grade B");
}
else if(percentage>=40 && percentage<65)
{
System.out.println("Grade c");
}
else
{
System.out.println("Fail");
}
}
}
}
Output:
C:\Users\Student\Desktop\java prg>javac IfSample.java
C:\Users\Student\Desktop\java prg>java IfSample
Input percentage :
55
Grade c
Ex3:
import java.util.Scanner;
class NestedIfElse
{
public static void main(String[] args)
{
int marksObtained, passingMarks;
char grade;
passingMarks = 40;
Scanner input = new Scanner(System.in);
System.out.println("Input marks scored by you");
marksObtained = input.nextInt();
if (marksObtained >= passingMarks)
{
if (marksObtained > 90)
grade = 'A';
else if (marksObtained > 75)
grade = 'B';
else if (marksObtained > 60)
grade = 'C';
else
grade = 'D';
System.out.println("You passed the exam and your grade is " + grade);
}
else
{
grade = 'F';
System.out.println("You failed and your grade is " + grade);
}
}
}
Output:
C:\Users\Student\Desktop\java prg>javac NestedIfElse.java
C:\Users\Student\Desktop\java prg>java NestedIfElse
Input marks scored by you
67
You passed the exam and your grade is C
5. Switch Statement
In Java, the switch statement is used for executing one statement from multiple conditions. it is similar to an if-else-if ladder. In a switch statement,the expression can be of byte, short, char and int data types.
From JDK7 enum, String class and the Wrapper classes can also be used. Following are some of the rules while using the switch statement:
There can be one or N numbers of cases. The values in the case must be unique. Each statement of the case can have a break statement. It is optional.
Syntax:
switch(expression)
{
case value1:
//code for execution; break; //optional
case value2:
// code for execution break; //optional
......
......
......
......
Case value n:
// code for execution break; //optional
default:
code for execution when none of the case is true;
}
Ex1:
class swdemo1
{
public static void main(String[] args)
{
int week = 4;
String day;
// switch statement to check day
switch(week)
{
case 1:day = "Sunday"; break;
case 2:day = "Monday"; break;
case 3:day = "Tuesday"; break;
case 4:day = "Wednesday"; break;
case 5:day = "Thursday"; break;
case 6:day = "Friday"; break;
case 7:day = "Saturday"; break;
default:day = "Invalid day"; break;
}
System.out.println("The day is " + day);
}
}
Output:
C:\Users\Student\Desktop\java prg>javac swdemo1.java
C:\Users\Student\Desktop\java prg>java swdemo1
The day is Wednesday
Ex2:
import java.util.Scanner;
class swdemo2
{
public static void main(String[] args)
{
char operator;
double n1, n2, result;
// create an object of Scanner class
Scanner s=new Scanner(System.in);
// ask user to enter operator
System.out.print("Enter operator (Either +, -, * or /): ");
operator =s.next().charAt(0);
System.out.print("Enter First Number:" );
n1=s.nextDouble();
System.out.print("Enter Second Number:" );
n2=s.nextDouble();
switch (operator)
{
// performs addition between numbers
case '+':result = n1 + n2;
System.out.print(n1 + "+" + n2 + " = " + result); break;
// performs subtraction between numbers
case '-':result = n1 - n2;
System.out.print(n1 + "-" + n2 + " = " + result); break;
// performs multiplication between numbers
case '*':result = n1 * n2;
System.out.print(n1 + "*" + n2 + " = " + result); break;
// performs division between numbers
case '/':result = n1 / n2;
System.out.print(n1 + "/" + n2 + " = " + result); break;
default:System.out.println("Invalid operator!"); break;
}
}
}
Output:
C:\Users\Student\Desktop\java prg>javac swdemo2.java
C:\Users\Student\Desktop\java prg>java swdemo2
Enter operator (Either +, -, * or /): *
Enter First Number:22
Enter Second Number:3
22.0*3.0 = 66.0
Output:
H:\>java swdemo2
Enter operator (Either +, -, * or /): *
Enter First Number:12
Enter Second Number:6
12.0*6.0 = 72.0
H:\>java swdemo2
Enter operator (Either +, -, * or /): ^
Enter First Number:34
Enter Second Number:2
Invalid operator!
**Starting from java 7.0, switch supports String data type also.
Ex:
import java.util.Scanner;
class swdemo3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Country name:");
String country=sc.next();
switch(country)
{
case "Kuwait":System.out.println("KWD-->KD."); break;
case "UK": System.out.println("Pound-->£"); break;
case "USA": System.out.println("Dollar-->$"); break;
default: System.out.println("Rupees-->Rs"); break;
}
}
}
Output:
C:>java swdemo3
Enter Country name:Kuwait
KWD-->KD.
C:>java swdemo3
Enter Country name:dd
Rupees-->Rs
No comments:
Post a Comment