MrDoyle

Control Flows

Aug 21st, 2020
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.println();
  5.         System.out.println("Testing if statement");
  6.  
  7.         // if statrements, both conditions are true, therefor will print
  8.         if(9 > 2) {
  9.             System.out.println("Testing if 9 > 2");
  10.         }
  11.  
  12.         if (4<5){
  13.             System.out.println("Testing if 4 < 5");
  14.         }
  15.  
  16.         // else statement, the conditiona is false, therefor the else statement will print
  17.         System.out.println();
  18.         System.out.println("Testing if (1 < 3 && 5 < 4)");
  19.         if(1 < 3 && 5 < 4){
  20.             System.out.println("Will not print");
  21.         } else {
  22.             System.out.println(" 5 < 4 is false, hence the else statement is printed");
  23.         }
  24.  
  25.  
  26.         //remember as soon as one of the conditions is met (top down) Java will exit the control flow.
  27.         // If none of the conditions are met, Java will call the else conditional
  28.         int shoeSize = 10;
  29.         System.out.println();
  30.         System.out.println("Testing if-else if-else control flow");
  31.         if (shoeSize > 12) {
  32.             System.out.println("You have big feet");
  33.         }else if (shoeSize >= 6) {
  34.             System.out.println("Ok you have normal feet");
  35.         } else {
  36.             System.out.println("Could your feet BE any smaller?");
  37.         }
  38.  
  39.         //Ternary conditional statement, a neater way or writing the if/else conditional
  40.         System.out.println();
  41.         System.out.println("Testing ternary conditional statement");
  42.         int pointsScored = 21;
  43.         System.out.println("Printing true response: " + ((pointsScored > 20) ? "You win": "You lose"));
  44.         pointsScored = 15;
  45.         System.out.println("Printing false response: " + ((pointsScored > 20) ? "You win": "You lose"));
  46.  
  47.         //Switch statement
  48.         System.out.println();
  49.         System.out.println("Testing the switch statement");
  50.  
  51.         //can work for any variable type, case conditions just need to match
  52.         int myGrade = 5;
  53.         switch (myGrade){
  54.             case 1:
  55.                 System.out.println("Grade 1 = Failure Grade");
  56.                 break;
  57.             case 2:
  58.                 System.out.println("Grade 2 = Failure Grade");
  59.                 break;
  60.             case 3:
  61.                 System.out.println("Grade 3 = Failure Grade");
  62.                 break;
  63.             case 4:
  64.                 System.out.println("Grade 4 = Passing Grade");
  65.                 break;
  66.             case 5:
  67.                 System.out.println("Grade 5 = Satisfactory Grade");
  68.                 break;
  69.             case 6:
  70.                 System.out.println("Grade 6 = Good Grade");
  71.                 break;
  72.             case 7:
  73.                 System.out.println("Grade 7 = Excellent Grade");
  74.                 break;
  75.             case 8:
  76.                 System.out.println("Grade 8 = Perfect Grade");
  77.                 break;
  78.             default:
  79.                 System.out.println("Please enter a grade within the grade boundaries");
  80.  
  81.  
  82.         }
  83.  
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment