Full Transcript

Chapter 3 Conditional Statements Chapter Objectives Conditional Statements Relational Operators IF Statements IF – ELSE Statements IF – ELSE IF – ELSE Statements Nested Conditional Statements Chapter Objectives (continued) Equal Function Logical Function G...

Chapter 3 Conditional Statements Chapter Objectives Conditional Statements Relational Operators IF Statements IF – ELSE Statements IF – ELSE IF – ELSE Statements Nested Conditional Statements Chapter Objectives (continued) Equal Function Logical Function Grade Average Program Conditional Statements Allows a program to take action based on the given condition. It makes our program smarter. Conditional Statements Typically, It compares two values so that our program can decide what action should be taken. Relational Operators LABEL SYMBOL SYNTAX Equals == x == y NOT Equals != x! = y Less Than < x= x >= y IF Statements Handles 1 Conditional Expression, It either does SOMETHING or NOTHING. IF Statements if (condition){ Do Everything you want here } IF Statements if (age>=18){ System.out.println(“You Have Access!”); } IF – ELSE Statements Handles 2 Conditional Expressions, It either does the first Code Block or the second Code Block. IF – ELSE Statements if (condition){ Do Everything you want here }else{ Do Everything you want here } IF – ELSE Statements if (age>=18){ System.out.println(“You have Access!”); }else{ System.out.println(“Access Denied!”); } IF – ELSE IF - ELSE Statements Handles 3 or more conditional expressions, the possibility of this statements are limitless it will run a certain code block based on the condition. IF – ELSE IF - ELSE Statements if (condition){ Do Everything you want here }else if (condition){ Do Everything you want here }else{ Do Everything you want here } IF – ELSE IF - ELSE Statements if (age>=18){ System.out.println(“You Have Access!”); }else if (age>=13){ System.out.println(“You Need Parent Consent!”); }else{ System.out.println(“Access Denied!”); } NESTED Conditional Statement A conditional statement within a conditional statement. PS: The nested conditional statement can be any type of conditional statement. NESTED Conditional Statement if (condition){ if (condition){ Do Everything you want here } } NESTED Conditional Statement if (age>=18){ if (isVerified){ System.out.println(“Qualified”); } } Equals Function To compare strings more effectively we need to make use of the equals function because the equal relation operators compares the memory address not the content. Equals Function String x = “Hello”; if(x.equals(“Hello”)){ System.out.println(“Hi”); } Logical Operators LABEL SYMBOL SYNTAX DESCRIPTION AND && condition && Both conditions need condition to be true OR || condition || Either conditions condition needs to be true NOT ! !condition Inverts current condition Grade Average Program Create a program that will let the user input 4 grades then make the program compute the average and display a message based on its value. If average: Above 100 – Invalid Grade 98 to 100 – With Highest Honors 95 to 97.99 – With High Honors 90 to 94.99 – With Honors 75 to 89.99 – Passed Below 75 - Failed Grade Average Program Sample Output English : 95 Math : 96 Science : 94 Computer : 99 Average : 96 With High Honors PS: the first four outputs was from the user, the subjects can be anything you like.

Use Quizgecko on...
Browser
Browser