If else Statements in Java
Lets get started
- Use if to specify a block of code to be executed, if a specified condition is true
- Use else to specify a block of code to be executed, if the same condition is false
- Use else if to specify a new condition to test, if the first condition is false
Elaborate with a simple example :
- import java.util.*;
- public class MINtech {
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- System.out.println("Marks should range from 0 to 100 only ");
- int marks = sc.nextInt();
- if (marks==100){
- System.out.println("Very good + new bike");
- }
- else if(marks<100 && marks>= 90 ){
- System.out.println("good + new cycle ");
- }
- else if (marks<90 && marks>= 80){
- System.out.println("good");
- }
- else if (marks<80 && marks>= 70){
- System.out.println("well");
- }
- else if (marks<70 && marks>= 60){
- System.out.println("baad");
- }
- else if (marks<60 && marks>= 50){
- System.out.println("pooor");
- }
- else if (marks<50 && marks>= 0){
- System.out.println("Get out from my mother land ");
- }else{
- System.out.println("Invalid marks");
- }
- }
- }
OUTPUT
Marks should range from 0 to 100 only 99 good + new cycle Marks should range from 0 to 100 only 50 pooor Marks should range from 0 to 100 only 10 Get out from my mother land Marks should range from 0 to 100 only 43567 Invalid marks
No comments:
Post a Comment