Decision Making

0
17

Decision Making

We use decision making when we want to skip some of the statements in our program as per the condition.
1.if statement:This statement checks for the if condition and executes the statements if it is true.
Example:

  1. public class greater
  2. {
  3. public static void main(String args[])
  4. {
  5. if(3>1)
  6. {
  7. System.out.println(3 is greater than 1);
  8. }
  9. }
  10. }

Output:
3 is greater than 1.

2.if else-It checks for the condition and executes the statements in if block if the condition is true .If the condition is false,statements in the else block are executed.
Example:

  1. public class greater
  2. {
  3. public static void main(String args[])
  4. {
  5. if(3>8)
  6. {
  7. System.out.println(False);
  8. }
  9. else
  10. {
  11. System.out.println(True);
  12. }
  13. }
  14. }

3.nested if-It checks for multiple conditions.
Example:

  1. public class Greater
  2. {
  3. public static void main(String args[])
  4. {
  5. if(3>8)
  6. {
  7. System.out.println(False);
  8. }
  9. elseif(8>1)
  10. {
  11. System.out.println(True);
  12. }
  13. else
  14. {
  15. System.out.println(“Skip”);
  16. }
  17. }
  18. }

4.Switch case:This is based on the user’s choice.
Example

  1. public class Choice
  2.  
  3. {
  4.  
  5. public static void main(String args[])
  6.  
  7. {
  8.  
  9. int n=1;
  10.  
  11. switch(n)
  12.  
  13. {
  14.  
  15. case 1:
  16.  
  17. System.out.println(Choice 1);
  18.  
  19. break;
  20.  
  21. case 2:
  22. System.out.println(Choice 2);
  23. break;
  24.  
  25. default:
  26. System.out.println(“wrong choice”);
  27. }
  28. }
  29. }

Since n=1,we will get output:
Choice 1

We can also input the value of n instead of directly assigning it value.

Facebook Comments
(Visited 21 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here