/* * The purpose of this program is to demonstrate the various ways in which selection statements can be used in Java. * We look at the 4 basic if-else structures and the ternary operator. */ import java.util.Scanner; class Selection { public static void main(String [] args) { //Declare a Scanner for user input Scanner input = new Scanner (System.in); /* Simple if statement. This is used when we need to do something only if a certain condition is true. * For example, we have to determine if someone got overall extra credit for this course. To receive overall extra credit, * one has to have a score of over 100%. We check for that. */ double grade; System.out.print("Please enter your overall grade in the class: "); grade = input.nextDouble(); if(grade > 100) { System.out.println("You received extra credit for the class."); } /* Simple if-else statement. This is used when we have different responses to a condition. * If the condition evaluates to be true, we execute a certain set of statements, and * if it happens to be false, we excute another set of statements. * For example, here we check if the user is broke, and give advise depending on their status. */ boolean broke; System.out.print("Are you broke? Enter true or false. : "); broke = input.nextBoolean(); if(broke == true) System.out.println("You need to budget"); else System.out.println("You can buy that sweater."); /* Multiple if-else statements. Also called an if-else ladder. It is a series of if statements, * where only one of the many possibilities is executed. * Here, we calculate the letter grade for the class based on the grade the user entered earlier. */ String letterGrade = "B"; /* If we're assigning the value inside the if statements, the compiler regonizes that the variable could potentially * never be initialized. So, we give it a dummy value at declaration. We can also eliminate some redundant code by * having this value be a default value. Over here, we initialize letterGrade with the default value "B". */ if(grade >= 93) letterGrade = "A"; else if(grade >=90) //we don't need to test for the upper limit here. We know for sure, that grade is below 93. letterGrade = "A-"; else if(grade >=87) letterGrade = "B+"; // Since we set the default to "B", any grade below 87 gets the letterGrade "B". System.out.println("The letter grade is "+letterGrade); /* Nested if statements. It is possible to write if statements inside if statements. * Here, we use this method to find the largest of 3 numbers. */ System.out.println("Enter 3 numbers : "); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double greatest = 0; //set greatest to some dummy value if ( a > b ) { if ( a > c ) greatest = a; else greatest = c; } else { if( b > c ) greatest = b; else greatest = c; } System.out.println("The largest number is "+greatest); /* Ternary Operator. * A ternary operator is an operator that takes 3 operands. Java has only one ternary operator - ?: * This works like a simple if-else statement. For more information, please see slides 17,18 and 19 of * the Selection statements slides. */ //The if-else statement about being broke can be converted to the following line. System.out.println((broke==true) ? "You need to budget" : "You can buy that sweater."); //It is also possible to nest this operator. It will work as long as the true and false blocks are single expressions. // The largest of 3 numbers example can be turned into the following line greatest = ( a > b ) ? ( a > c ) ? a : c : ( b > c ) ? b : c; System.out.println("The largest number is "+greatest); } }