| CSCI A201/A597Lecture Notes Fourteen Second Summer 2000 |
What's the purpose of if statements?
| It allows us to include decisions in our programs. |
| What do we do with their results? | We can branch our course of action. |
| How do you code this branching situation? |
| Use the space above. |
| How would you code this slightly modified situation? |
| I see there's one minor change. |
| In general, in our programs, we have the flowchart clear in mind and we just need to translate it in Java. | We need to do that with care, as illustrated above. |
| Ocasionally we will have to do the reverse, when we read someone else's code. | Let's see some examples. |
| OK, here's a bigger one. | I can hardly wait. |
Example 1
Assume that option is an int variable
that can be only 1, 2 or 3
before each of the code fragments illustrated below. So this is
given. Which of the fragments below (part of Example 1) set i
to the value that option has? We will look at this kind
of problems with the help of diagrams.
| Code | Diagram |
|---|---|
if (option == 1) i = 1; else if (option == 2) i = 2; else i = 3; |
|
In this particular case it's easy to see that the code sets i
to the same value as option (given the assumption about the
possible values that option can have).
| |
For the remaining of these notes we will draw the diagrams and fill them with text in class.
| Code | Diagram |
|---|---|
i = 1; if (option >= 2) i = i + 1; if (option == 3) i = i + 1; |
|
| Code | Diagram |
|---|---|
if (option == 1) i = 1; if (option == 2) i = 2; else i = 3; |
|
| Code | Diagram |
|---|---|
i = 1;
if (option > 1)
if (option > 2)
i = 3;
else
i = 2; |
|
| Code | Diagram |
|---|---|
i = 1 if (option == 1); i = 2 if (option == 2); i = 3 if (option == 3); | Incorrect syntax, no diagram. |
Example 2
Consider this code fragment
Which one of the following are equivalent to it? Note that the structure of the diagram is as important as what gets written inside the boxes. We sketch the diagrams below.if (x > y) z = z + 1; else z = z + 2;
| Code | Diagram |
|---|---|
if (x > y) z = z + 1; if (x <= y) z = z + 2; |
|
| Code | Diagram |
|---|---|
if (! (x > y)) z = z + 2; else z = z + 1; |
|
| Code | Diagram |
|---|---|
z = z + 1; if (x <= y) z = z + 1; |
|
| Code | Diagram |
|---|---|
if (x > y) z = z + 1; else if (x <= y) z = z + 2; |
|
| Code | Diagram |
|---|---|
z = z + 2; if (!(x > y)) z = z - 1; |
|
Two code fragments will be considered equivalent when they behave in the same way (same output, same internal state) for identical inputs, for all possible inputs.
Other kinds of problems:
Problem 1 Consider the following two program fragments:
| Fragment 1 | Fragment 2 |
|---|---|
if (x == 5) x = x + 1; else x = 8; | if (x == 5) x = x + 1; if (x != 5) x = 8; |
Check all that apply:
x is 6 initially then x is 8 after executing
fragment one, and
x is 6 after executing
fragment two.
x always has the value 8
after executing fragment two.
x has either the value 5
or the value 8 after executing fragment one.
Problem 2
Assume that x and y are integer variables.
Consider the following nested if statement.
if (x > 3)
if (x <= 5)
y = 1;
else if (x != 6)
y = 2;
else
y = 3;
else
y = 4;
If y has the value 2 after executing the
above program fragment, then what do you know about x? Problem 3
Assume that x and y are integer variables.
and the code fragment shown below:
if (x > 3) {
if (x <= 5)
y = 1;
else if (x != 6)
y = 2;
} else
y = 3;
Questions: x is 1 before the fragment gets
executed what's the value of y after the fragment is
executed?
x
need to have before the fragment gets executed for y
to be 3 at the end of the fragment?
| What do you think about these problems? | They make for good practice. |
| And I think you should work them all out. | Weren't we supposed to start loops today. |
| Yes, and we can still do it. | All right, let's do it! What's the motivation? |
| Remember the investment problem from the first week? | Lecture notes two. |
| Exactly. Here's the code in Java, most of it. | What is in red and what is in blue? |
double balance = 10000;
int year = 0;
year = year + 1;
balance = balance + balance * 0.05;
System.out.println("Year: " + year);
| The code in blue should be executed only once. | The part in red looks like what you'd want to do repeatedly until your balance doubles (reaches 20000). |
Indeed. Java has a while statement
which is able to do the iteration for us.
| And you'd only need to tell it when to stop or, rather, for how long it should go on. |
| Exactly. Here's the code: |
double balance = 10000;
int year = 0;
while (balance < 20000) {
year = year + 1;
balance = balance + balance * 0.05;
}
System.out.println("Year: " + year);
| Can I draw a flowchart for this? |
| You sure may. | Here it is: |
Last updated: July 12, 2000 by Adrian German for A201