Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

If, If..else Statement in Java With Examples

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

If, If..

else Statement in Java


with Examples
Online Java Class
• The Java if statement is used to test the condition.
• It checks boolean condition: true or false.
• There are various types of if statement in Java.
• if statement
• if-else statement
• if-else-if ladder
• nested if statement
Java if Statement
• The Java if statement tests the condition.
• It executes the if block if condition is true.

if(condition){
//code to be executed
}
Example
//Java Program to demonstate the use of if statement.
public class IfExample {
public static void main(String[] args) {
//defining an 'age' variable
int age=3;
//checking the age Condition(true)// 1
Condition(false)//0
if(age>18) Condition(true)
{
int a=4+9;// first statement
System.out.print("Age is greater than 18"); //second statement
}
}
}
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
1.if(condition)
2.{
3.//code if condition is true
4.}else{
5.//code if condition is false
6.}
Example
//A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number.
public class IfElseExample {
/
%
public static void main(String[] args) {
4/5= //defining a variable
4%3= int number=4; 4%3= number%3 =1
4/2=
//Check if the number is divisible by 2 or not
if (number%3(1)==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;
0 1 0
if( ( (year % 4 ==0) && (year % 100 !=0) ) || (year % 400==0) ){
System.out.println("LEAP YEAR");
}
else{ &&
0&&1=0
System.out.println("COMMON YEAR"); 1 && 1=1
}
||
} 1 || 0 =1
1 || 1= 1
}
Using Ternary Operator
• We can also use ternary operator (? :) to perform the task of if...else
statement.
• It is a shorthand way to check the condition.
• If the condition is true, the result of ? is returned.
• But, if the condition is false, the result of : is returned.
public class IfElseTernaryExample {
public static void main(String[] args) {
int number=20;
//Using ternary operator
String output=(number%2==0)?"even number":"odd number";
System.out.println(output);
}
}
Java if-else-if ladder 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
}
Example:
1.//Java Program to demonstrate the use of If else-if ladder.
2.//It is a program of grading system for fail, D grade, C grade,
B grade, A grade and A+.
3.public class IfElseIfExample { 1.
4.public static void main(String[] args) { 2. else if(marks>=70 && marks<80){
5. int marks=65; 3. System.out.println("B grade");
6. 4. }
7. if(marks<50){ 5. else if(marks>=80 && marks<90){
8. System.out.println("fail"); 6. System.out.println("A grade");
9. } 7. }else if(marks>=90 && marks<100){
10. else if(marks>=50 && marks<60){ 8. System.out.println("A+ grade");
11. System.out.println("D grade"); 9. }else{
12. } 10. System.out.println("Invalid!");
13. else if(marks>=60 && marks<70){ 11. }
14. System.out.println("C grade"); 12.}
15. } 13.}
16.
Program to check POSITIVE, NEGATIVE or
ZERO:
public class PositiveNegativeExample {
public static void main(String[] args) {
int number=-13;
if(number>0){
System.out.println("POSITIVE");
}else if(number<0){
System.out.println("NEGATIVE");
}else{
System.out.println("ZERO");
}
}
}
Java Nested if statement

• The nested if statement represents the if block within another if block.


• Here, the inner if block condition executes only when outer if block
condition is true.
Syntax:

1.if(condition){
2. //code to be executed
3. if(condition){
4. //code to be executed
5. }
6.}
Example:
1.//Java Program to demonstrate the use of Nested If Statement.
2.public class JavaNestedIfExample {
3.public static void main(String[] args) {
4. //Creating two variables for age and weight
5. int age=20;
6. int weight=30;
7. //applying condition on age and weight
8. if(age>=18){
9. if(weight>50){
10. System.out.println("You are eligible to donate blood");
11. }
12. }
13.}}
1.public class JavaNestedIfExample2 {
2.public static void main(String[] args) {

Example 2: 3.
4.
//Creating two variables for age and weight
int age=25;
5. int weight=48;
6. //applying condition on age and weight
7. if(age>=18){
8. if(weight>50){
9. System.out.println("You are eligible to donate blood");
10. } else{
11. System.out.println("You are not eligible to donate blood");
}
12. } else{
13. System.out.println("Age must be greater than 18");
14. }
15.} }
Java Switch Statement
• Use the switch statement to select one of many code blocks to be
executed.
Syntax

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example
case 5:
public class Main { System.out.println("Friday"); break;
public static void main(String[] args) { case 6:
int day = 4; System.out.println("Saturday"); break;
switch (day) { case 7:
case 1: System.out.println("Sunday"); break;
System.out.println("Monday"); break; }
case 2: }
System.out.println("Tuesday"); break; }
case 3:
System.out.println("Wednesday"); break;
case 4:
System.out.println("Thursday"); break;
public class SwitchExample {
public static void main(String[] args) {
int number=20;
switch(number)
{
case 10: System.out.println("10");break;
case 20: System.out.println("20");break;
case 30: System.out.println("30");break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}
1.//Java Program to demonstrate the example of Switch statement
2.//where we are printing month name for the given number
1.
3.public class SwitchMonthExample {
2. case 7: monthString="7 - July";
4.public static void main(String[] args) {
3. break;
5. //Specifying month number
4. case 8: monthString="8 - August";
6. int month=7;
5. break;
7. String monthString=""; //String monthString=“ ”;
6. case 9: monthString="9 - September";
8. //Switch statement
7. break;
9. switch(month){
8. case 10: monthString="10 - October";
10. //case statements within the switch block
9. break;
11. case 1: monthString="1 - January";
10. case 11: monthString="11 - November";
12. break;
11. break;
13. case 2: monthString="2 - February";
12. case 12: monthString="12 - December";
14. break;
13. break;
15. case 3: monthString="3 - March";
14. default:System.out.println("Invalid Month!");
16. break;
15. }
17. case 4: monthString="4 - April";
16. //Printing month of the given number
18. break;
17. System.out.println(monthString);
19. case 5: monthString="5 - May";
18.}
20. break;
19.}
21. case 6: monthString="6 - June"; break;
Program to check Vowel or Consonant:
If the character is A, E, I, O, or U, it is vowel otherwise consonant. It is not case-sensitive.
1. break;
1.public class SwitchVowelExample { 2. case 'A':
2.public static void main(String[] args) { 3. System.out.println("Vowel"); break;
3. char ch='O'; 4. case 'E':
4. switch(ch) 5. System.out.println("Vowel"); break;
5. { 6. case 'I':
6. case 'a': 7. System.out.println("Vowel"); break;
7. System.out.println("Vowel"); break; 8. case 'O':
8. case 'e': 9. System.out.println("Vowel"); break;
9. System.out.println("Vowel"); break; 10. case 'U':
10. case 'i': 11. System.out.println("Vowel"); break;
11. System.out.println("Vowel"); break; 12. default:
12. case 'o': 13. System.out.println("Consonant");
13. System.out.println("Vowel"); break; 14. }
14. case 'u': 15.}
15. System.out.println("Vowel"); break; 16.}
16.
Java Switch Statement is fall-through

• The Java switch statement is fall-through.


• It means it executes all statements after the first match if a break
statement is not present.
1.//Java Switch Example where we are omitting the
Example 2.//break statement
3.public class SwitchExample2 {
4.public static void main(String[] args) {
5. int number=20;
6. //switch expression with int value
7. switch(number){
8. //switch cases without break statements
9. case 10: System.out.println("10");
10. case 20: System.out.println("20");
11. case 30: System.out.println("30");
12. default:System.out.println("Not in 10, 20 or 30");
13. }
14.}
15.}
Java Switch Statement with String
1. //Using String in Switch expression
2. switch(levelString){
1.//Java Program to demonstrate the use of Java Switch
3. //Using String Literal in Switch case
2.//statement with String
4. case "Beginner": level=1;
3.public class SwitchStringExample {
5. break;
4.public static void main(String[] args) {
6. case "Intermediate": level=2;
5. //Declaring String variable
7. break;
6. String levelString="Expert";
8. case "Expert": level=3;
7. int level=0;
9. break;
8. //Using String in Switch expression
10. default: level=0;
9.
11. break;
12. }
13. System.out.println("Your Level is: "+level);
14.} }
Java Nested Switch Statement
• We can use switch statement inside other switch statement in Java.
• It is known as nested switch statement.
//Java Program to demonstrate the use of Java Nested Switch
public class NestedSwitchExample {
public static void main(String args[])
{
char branch = 'C';
int collegeYear = 3;
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("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;
}
}
}
Examples For If… else
public class Main {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
public class Main {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
public class Main {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
public class Main {
public static void main(String[] args) {
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
public class Main {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
public class Main {
public static void main(String[] args) {
int time = 20;
String result;
result = (time < 18) ? "Good day." : "Good
evening.";
System.out.println(result);
}
}
Thank you for your effort!!

You might also like