Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Notes on Decisions - Introduction to Programming in Java | CSIS 110, Study notes of Javascript programming

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

Pre 2010

Uploaded on 08/05/2009

koofers-user-3jl-1
koofers-user-3jl-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 11
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
pf3
pf4
pf5

Partial preview of the text

Download Notes on Decisions - Introduction to Programming in Java | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 11

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> ) Evaluate the boolean-expression. (A boolean-expression is one whose value is a boolean.) If its value is true, then execute the statement. Otherwise, skip over the statement. if ( temperature <= 32 ) System.out.println("It's freezing!"); if ( itemPrice > moneyInAccount )

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> ) else Show Flow chart Let's try to write some Java code that asks the user for two numbers, and then determines which number is larger: int firstNum = stdin.nextInt(); int secondNum = stdin.nextInt(); int maximum; if ( firstNum < secondNum ) { maximum = secondNum; } else { maximum = firstNum; } S.O.PL( "The maximum of " + firstNum + " and " + secondNum + " is " + maximum );

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

  1. Asks the user for a number
  2. Examines the value of the number, and then prints the appropriate message: a. The number is positive. b. The number is negative. c. The number is 0.