- if
if statement is a conditional statement
Syntax:
if(condition) {
if statements
e.g.
int a = 30, b = 20;
if( a > b ) {
System.out.println(a + " is greater than " + b);
}
2. if else
Syntax:
if(condition) {
if statements
} else {
else statements
}
e.g.
package com.java.basics;
public class IfElse {
public static void main(String[] args) {
int a = 30, b = 20;
if (a > b) {
System.out.println(a + " is greater than " + b);
} else {
System.out.println(a + " is less than or equal to " + b);
}
}
}
public class IfElse {
public static void main(String[] args) {
int a = 30, b = 20;
if (a > b) {
System.out.println(a + " is greater than " + b);
} else {
System.out.println(a + " is less than or equal to " + b);
}
}
}
Syntax:
if(condition) {
if statements
} else if(condition){
else if statements
} else if(condition){
else if statements
} else if(condition){
else if statements
}
.
.
.
else {
else statements
e.g.
package com.java.basics;
public class ElseIfLadder {
public static void main(String[] args) {
int marks = 60;
if( marks >= 90 && marks <= 100 ) {
System.out.println("Excellent");
} else if( marks >= 80 && marks < 90 ) {
System.out.println("Very Good");
} else if( marks >= 70 && marks < 80 ) {
System.out.println("Good");
} else if( marks >= 60 && marks < 70 ) {
System.out.println("Average");
} else if( marks >= 50 && marks < 60 ) {
System.out.println("Satisfactory");
} else if( marks >= 40 && marks < 60 ) {
System.out.println("Poor");
} else {
System.out.println("Fail");
}
}
}
public class ElseIfLadder {
public static void main(String[] args) {
int marks = 60;
if( marks >= 90 && marks <= 100 ) {
System.out.println("Excellent");
} else if( marks >= 80 && marks < 90 ) {
System.out.println("Very Good");
} else if( marks >= 70 && marks < 80 ) {
System.out.println("Good");
} else if( marks >= 60 && marks < 70 ) {
System.out.println("Average");
} else if( marks >= 50 && marks < 60 ) {
System.out.println("Satisfactory");
} else if( marks >= 40 && marks < 60 ) {
System.out.println("Poor");
} else {
System.out.println("Fail");
}
}
}
4. Switch:
Syntax:
switch(expression){
case value1: statement 1;
break;
case value2: statement 2;
break;
case value3: statement 3;
break;
.
.
.
case valuen: statement n;
break;
default: default statement;
}
package com.java.basics;
public class Switch {
public static void main(String[] args) {
char ch = 'A';
switch (ch) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
break;
case 'C':
System.out.println("Good");
break;
case 'D':
System.out.println("Average");
break;
case 'E':
System.out.println("Satisfactory");
break;
case 'F':
System.out.println("Fail");
break;
default:
System.out.println("Wrong Grade");
break; //Optional
}
}
}
public class Switch {
public static void main(String[] args) {
char ch = 'A';
switch (ch) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
break;
case 'C':
System.out.println("Good");
break;
case 'D':
System.out.println("Average");
break;
case 'E':
System.out.println("Satisfactory");
break;
case 'F':
System.out.println("Fail");
break;
default:
System.out.println("Wrong Grade");
break; //Optional
}
}
}
Exercise:
- Write a program to find area of triangle?
- Write a program to check a number is divisible by 2 or 3?
- Write a program to check a number is even?
- Write a program to check a number is odd?
- Write a program to check a number is power of 2?
- Write a program to find smallest number among 3 numbers?
- Write a program to check if a number is even or not using Ternary operator?
No comments:
Post a Comment