Wednesday, 4 October 2023

JAVA

If else Statements in Java

Lets get started

  1. Use if to specify a block of code to be executed, if a specified condition is true
  2. Use else to specify a block of code to be executed, if the same condition is false
  3. Use else if to specify a new condition to test, if the first condition is false

Elaborate with a simple example :

            
  1. import java.util.*;
  2. public class MINtech {
  3. public static void main(String args[]){
  4. Scanner sc = new Scanner(System.in);
  5. System.out.println("Marks should range from 0 to 100 only ");
  6. int marks = sc.nextInt();
  7. if (marks==100){
  8. System.out.println("Very good + new bike");
  9. }
  10. else if(marks<100 && marks>= 90 ){
  11. System.out.println("good + new cycle ");
  12. }
  13. else if (marks<90 && marks>= 80){
  14. System.out.println("good");
  15. }
  16. else if (marks<80 && marks>= 70){
  17. System.out.println("well");
  18. }
  19. else if (marks<70 && marks>= 60){
  20. System.out.println("baad");
  21. }
  22. else if (marks<60 && marks>= 50){
  23. System.out.println("pooor");
  24. }
  25. else if (marks<50 && marks>= 0){
  26. System.out.println("Get out from my mother land ");
  27. }else{
  28. System.out.println("Invalid marks");
  29. }
  30. }
  31. }

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