GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 4: Decision Making and Conditional Statements


Objectives

By the end of this module, for simple programs with real numbers, you will be able to:




Boolean variables

In order to make a decision, we need a way to represent a situation that is true, versus one that is false. In Java, there is a special type used for just that: boolean. A boolean variable (or expression) has either the value true or the value false -- both of these are reserved words in the language:

With the compiler recognizing boolean types and expressios, it is now possible to have the program make decisions at runtime, based on the values stored in variables. We achieve this through the use of conditional statements.




Why are conditionals useful in computation?

Conditionals ... a simple example

Consider this program:

About the if-statement:




If-else

Consider this program:

We can combine an else clause with an added if-clause:

Activity 1: Run the above example in and change the values of x one at a time so that you see each block execute. That is, try one value of x to make the if-block execute, then another value of x to make the else-if block execute etc. Then, modify the code by adding a new variable, z, and include a conditional that should evaluate to true before the else clause. How does it evaluate?

Note that, in a single if-else block (which can have more than two clauses), one and only one clause executes, even if multiple clauses happen to be true. The first clause to evaluate to true is the only one that gets executed.




Comparison operators

: Activity 2: Are there any circumstances under which the last block executes? Activity 3: Suppose we want to identify whether x has the largest value among x, y and z. Why doesn't the following program work?

How would you alter the program (in ThreeVariableExample.java) to make it work?




Nested conditionals

Consider this program:




Combining conditions

Consider this program:



The AND operator



The OR operator



The NOT operator

The NOT operator can be applied to a larger clause made of sub-clauses. Here, the inner clauses are first evaluated, and the result is "flipped" to see if the NOT clause turns out to be true:

Activity 4: Suppose integer variables a,b,c,d,e have values a=1, b=1, c=3, d=4, e=5. Consider the following three expressions:

  ( (a <= b) && (c+d > e) && (d > 1) )

  ( (a > c) || ( (c+1 < e) && (c-b > a) ) )

  ! ( (b == d-c) && (a > b) || (c < d) )
     
Try to evaluate each expression by hand. Use the Java Visualizer to see if the result matches with your hand-evaluation.




Boolean operator summary



We can capture the behavior of the and, or, and not operators using a truth table:

x y x && y x || y !x
True True True True False
True False False True False
False True False True True
False False False False True




When things go wrong



Activity 5: Identify the four errors in this piece of code:



Activity 6: Fix the errors in the if condition to make this print "Success".

First, try to find the problems without compiling. Then, fix the code and see if you were right while reading.




Meta

This has been a challenging module: intricate code, with many moving parts, and looooong. A few things to keep in mind:







What's in our toolbox at this point:

Our current toolbox by this lecture




Next class:

We'll go through some more in-class exercises on conditionals and decision making.


Assignments for next lecture:

None.