



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Chapter 5: Decisions Up to now, all of our programs ran sequentially - they always did the same thing every time. We'd like our programs to behave differently, depending upon the data they are using. For example, we might like a program to print a different message, depending on the day of the week the temperature the weather whether or not we have a fever But, before we can do this, we have to look at the boolean primitive type Boolean Boolean is a primitive type, representing a logical value. A boolean can have one of two values: true, false. // Unitialized declarations boolean isFreezing; boolean passedQuiz; // Initialized declarations boolean enoughMoney = false; boolean haveLeavesTurnedColor = true; Equality We'd like to be able to compare two values to see if they are equal to each other. Java provides two operators for comparing primitive types. == test for equality != test for inequality Result of evalating expression is a boolean value 3 == 3 true 3 == 7 false 3 != 7 true 3 != 3 false Can also compare booleans true == true true true == false false
true != true false true != false true Relational Operators With numbers, there are other comparisons that we can use - the ordering operators <, <=, >, >=. We use relational operators to compare two values. The result of a comparison is a boolean value. 1 < 2 true 3 > 4 false 3 > 3 false 3 >= 3 true 5 <= 4 false Similarly, given int i = 5; int j = 7 double f = 2.6; double g = 4.2; then i < j true i > 4 true g > 4 true f < g true These operators produce a boolean - can't use them to compare booleans, however. Conditional statements As noted earlier, we would like our programs to behave in different ways depending upon the value of the data the program is using. One statement for doing this is the if statement. if ( <boolean-expression> )
Operator Precedence Operator Associativity () None op++ op-- Right --op ++op, Unary + -,! Right (cast) new Right Mult: * / % Left Add: + - Left Relational Ordering: < > <= >= Left Relational Equality: == != Left And: && Left Or: || Left Assignment = Right If-Else So far, we can execute some statements if something is true, and skip them if something is false. What if we want to execute one set of statements when something is true, and a different set of statements when something is false? Well, we could use two if statements. But, Java has a different version of the if statement - the if-else statement. if ( <boolean-expression> )
Nested ifs Remember, the statements that are executed by an if-statement can be any Java statement or block of statements, including more if statements! Say we wanted to have ice cream. First we look in the freezer to see if there is any. If there is, we want to fix a bowl and eat. If not, we want to check to see if we have money. If we do, we want to go buy some ice cream. Otherwise, we decide to eat an apple instead. How would we do this in Java? if ( haveIceCream ) { System.out.println("Fix a bowl of ice cream."); S.O.PL( "Eat ice cream"); } else { if ( haveMoney ) { S.O.PL("Buy ice cream" ); } else { S.O.PL( "Eat an apple." ); } } Let's look at the flow chart for this. Lab Exercise Write a program named CheckSign that