Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- System.out.println();
- System.out.println("Testing if statement");
- // if statrements, both conditions are true, therefor will print
- if(9 > 2) {
- System.out.println("Testing if 9 > 2");
- }
- if (4<5){
- System.out.println("Testing if 4 < 5");
- }
- // else statement, the conditiona is false, therefor the else statement will print
- System.out.println();
- System.out.println("Testing if (1 < 3 && 5 < 4)");
- if(1 < 3 && 5 < 4){
- System.out.println("Will not print");
- } else {
- System.out.println(" 5 < 4 is false, hence the else statement is printed");
- }
- //remember as soon as one of the conditions is met (top down) Java will exit the control flow.
- // If none of the conditions are met, Java will call the else conditional
- int shoeSize = 10;
- System.out.println();
- System.out.println("Testing if-else if-else control flow");
- if (shoeSize > 12) {
- System.out.println("You have big feet");
- }else if (shoeSize >= 6) {
- System.out.println("Ok you have normal feet");
- } else {
- System.out.println("Could your feet BE any smaller?");
- }
- //Ternary conditional statement, a neater way or writing the if/else conditional
- System.out.println();
- System.out.println("Testing ternary conditional statement");
- int pointsScored = 21;
- System.out.println("Printing true response: " + ((pointsScored > 20) ? "You win": "You lose"));
- pointsScored = 15;
- System.out.println("Printing false response: " + ((pointsScored > 20) ? "You win": "You lose"));
- //Switch statement
- System.out.println();
- System.out.println("Testing the switch statement");
- //can work for any variable type, case conditions just need to match
- int myGrade = 5;
- switch (myGrade){
- case 1:
- System.out.println("Grade 1 = Failure Grade");
- break;
- case 2:
- System.out.println("Grade 2 = Failure Grade");
- break;
- case 3:
- System.out.println("Grade 3 = Failure Grade");
- break;
- case 4:
- System.out.println("Grade 4 = Passing Grade");
- break;
- case 5:
- System.out.println("Grade 5 = Satisfactory Grade");
- break;
- case 6:
- System.out.println("Grade 6 = Good Grade");
- break;
- case 7:
- System.out.println("Grade 7 = Excellent Grade");
- break;
- case 8:
- System.out.println("Grade 8 = Perfect Grade");
- break;
- default:
- System.out.println("Please enter a grade within the grade boundaries");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment