Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.

Similar presentations


Presentation on theme: "Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You."— Presentation transcript:

1 Quiz 1 Exam 1 Next Monday

2 Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You have a B!” ); else System.out.println(“We’ll give you a C.” );

3 if / else (Cascaded) Sequence of if / else Statements Example: if (myGrade > 90) System.out.println(“You have an A!” ); else if (myGrade > 80) System.out.println(“You have a B!” ); else if (myGrade > 70) System.out.println(“You have a C!” ); else System.out.println(“Oh-oh!” );

4 Boolean Type A Boolean Value Is One of Either “True” or “False” In JAVA, Type boolean Example: boolean done = false;... if (currentLetter == ‘Z’) done = true; else done = false; … if (done) System.exit(0); if / else Conditionals Must Evaluate to True or False, and Are Therefore Called Boolean Expressions

5 Logical Operators A Logical Operator Is One Used to Further Specify True or False in an Expression Connects Two or More Expressions Together && Is Logical “AND” || Is Logical ‘OR” &&: Both Operands Must Be True for Entire Expression to Be True ||: Only One (or Both) of the Operands Must Be True for Entire Expression to Be True

6 Logical Operators if (numStudents > MIN && numStudents < MAX) classRun = true; if (numStudents > MAX || numInstructors == 0) classRun = false;

7 Operator Precedence () ! (not) *, /, % +, -, >= (Relational Operators) ==, != (Equality Operators) && (Logical AND) || (Logical OR) = (ASSIGNMENT)

8 Order of Operations Precedence: Level of Importance of Operations Multiplicative Operators Have Higher Precedence than Additive Operators: *, /, % Higher +, - Lower Associativity: Order of Operation for Equal Level Precedence Most Operators Have Left-to-Right Associativity Use Parentheses to Force Differing Precedence of Operations

9 Multiple Logical Operators if ( num1 > MAX || num2 == 0 && num3 == 0) System.out.println(“ num1 is MAX or something is 0”); System.out.println(“ I think…..” );

10 Switch Statements Also Called Switch/Case Statement Just Case in Other Languages Selects Among Several Different Actions Can Only Select from Integer or Character If an Integer Value Is Matched, Statements under Control of that Case Block Are Executed

11 Switch/Case Example final double BASERATE = 105.00; double rate; int numPassengers; System.out.print(“ Enter Passengers: “); numPassengers = scan.nextInt(); switch(numPassengers) { case 2: rate = BASERATE * 0.80; break; case 4: rate = BASERATE * 0.75; break; case 5: rate = BASERATE * 0.55; break; default: rate = BASERATE; break; }

12 Switch Case Example char menuItem; String inputS; System.out.print("Enter Menu Selection: “); inputS = scan.next(); menuItem = inputS.charAt(0); switch(menuItem) { case 'O': case ‘o’: //Code to Order Something break; case 'C': case ‘c’: //Code to Checkout break; default: //Error Code break; }

13 Announcements Exam 1 Next Week in Monday Lecture Everything through Lab 4 50 minutes 10 % of Total Grade Covers Everything through Lecture this Week –Quiz 1 Topics (terminology, variables, constants, basics) –if-then-else –Compound statements –Relational operators –Logical operators –Switch/Case Statements


Download ppt "Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You."

Similar presentations


Ads by Google