Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2011 Introduction to Programming I Selections (I)

Similar presentations


Presentation on theme: "CS2011 Introduction to Programming I Selections (I)"— Presentation transcript:

1 CS2011 Introduction to Programming I Selections (I)
Chengyu Sun California State University, Los Angeles

2 Mortgage Calculator Revisited
The formula How do we include the case where monthly interests rate is 0? Check if the interest rate entered is 0, and based on the result, select different part of the program to execute.

3 What's Needed in a Programming Language
Branch Statement Check if the interest rate entered is 0, and based on the result, select different part of the program to execute. Boolean Expression Boolean Value

4 Boolean Expressions Used to check a condition, and the result would be either true or false (i.e. boolean) For example: interestRate == 0 radius > 0

5 Relational Operators Notice the difference between = and ==
Name < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != No equal to Notice the difference between = and == In Java, relational operators can only be used on numbers

6 Boolean Values and Variables
boolean is a data type just like int and double There are only two possible boolean values: true and false Boolean variables can be use to store boolean values (usually the results of some condition checks) Example: SimpleBooleans.java

7 Example: Addition Quiz
Generate two random numbers between 0 and 10 Ask the user to enter the sum of the two numbers Display true or false depending on whether the answer is correct Use java.util.Random to generate the random numbers

8 Branch Statements if statement condition if-else statement condition
true condition if-else statement true condition false

9 if statement if ( boolean-expression ) { statement(s); } If the boolean expression evaluates to true, execute the statement(s) in {} {} can be omitted if there's only one statement

10 If-else statement if ( boolean-expression ) { statement(s); } else {
If the boolean expression evaluates to true, execute the statement(s) in {} after if (a.k.a. the if block), otherwise execute the statements in {} after else (a.k.a. the else block) {} can be omitted if there's only one statement in the block

11 Examples Modify Addition Quiz so it display correct/incorrect instead of true/false Use if statement Use if-else statement Modify Mortgage Calculator to handle the case where the interest rate is 0

12 Check Multiple Conditions
Example: LetterGrade.java Conditions Letter Grade score >= 90 A 80 <= score < 90 B 70 <= score < 80 C 60 <= score < 70 D score < 60 F

13 Use Nested if-else Statements
Write proper conditions Understand that certain conditions are already met when we get to the else block Use proper syntax else{ if(){ } else if(){ } else{


Download ppt "CS2011 Introduction to Programming I Selections (I)"

Similar presentations


Ads by Google