By the end of this module, for simple programs, you will be able to:
Consider this program:
In-Class Exercise 1: Add an additional println right below the "Hey, ..." println. Compile and execute the program. Then, change the value of y to 6 and compile/execute. What is the output?
About the if-statement:
Consider this program:
We can combine an else clause with an added if-clause:
Comparison operators:
(x < y) // Strictly less than. (x <= y) // Less than or equal to. (x > y) // Strictly greater than. (x >= y) // Greater than or equal to. (x == y) // Equal to. (x != y) // Not equal to.
In-Class Exercise 2: Are there any circumstances under which the last block executes?
In-Class Exercise 3: Suppose we want to identify whether x has the largest value among x, y and z. Why doesn't the following program work?
Can you alter the program to make it work?
Consider this program:
In-Class Exercise 4: Modify the above program so that it prints out, appropriately, one of "a is the smallest", "b is the smallest" or "c is the smallest", depending on the actual values of a, b and c. Try different values of these variables to make sure your program is working correctly.
Consider this program:
In-Class Exercise 5: Let's go back to this program:
Use a two-subclause if-statement to identify whether a is the smallest of the three.
In-Class Exercise 6: Now extend this idea to identify which of the three variables has the smallest value.
The OR operator:
The NOT operator:
In-Class Exercise 7: Modify the above to determine whether all three variables have different values.
The NOT operator can be applied to a larger clause made of sub-clauses:
What is a Boolean variable?
An example:
Boolean operators:
Let's see what c could be based on all possible combinations of a and b values:
a | b | c = a && b |
false | false | false |
false | true | false |
true | true | true |
true | false | false |
Let's see what c could be based on all possible combinations of a and b values:
a | b | c = a || b |
false | false | false |
false | true | true |
true | true | true |
true | false | true |
a | !a |
true | false |
false | true |
First, reading:
say to yourself:
Say to yourself:
break it up into a hierarchical view:
In-Class Exercise 8: Draw a hierarchical picture that matches the conditional below.
Then, evaluate it to see whether the result is true or false. Write down the true/false values at intermediate levels of the hierarchy.
Writing:
In-Class Exercise 9: Identify the four errors in this piece of code:
In-Class Exercise 10: 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.
In-Class Exercise 11: Create a program to output health information based on BMI. Use at least two variables: mass and height.
mass(lb) x 703
--------------------
height(in)2
BMI stands for Body Mass Index. This is a numerical value of your weight in relation to your height. BMIs are good indicators of healthy or unhealthy weights for adult men and women, regardless of body frame size.