[an error occurred while processing this directive] CSE 142 Lab Resources: Assertions

# University of Washington, CSE 142

## Lab Resources: Assertions

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

# Basic lab instructions

• Mouse over if you're not sure what they mean!
• Talk to your classmates for help.
• You may want to bring your textbook to future labs to look up syntax and examples.
• Stuck? Confused? Have a question? Ask a TA for help, or look at the book or past lecture slides.
• Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave.
• Feel free to complete problems in any order.
• Make sure you've signed in on the sign-in sheet before you leave!

# Assertions

Assertion problems test your ability to reason through code. For example, consider the following:

```public static void mystery(int x, int y) {
int z = 0;

// Point A
while (x != y) {
// Point B
z++;

if (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}```

For each point, we want to figure out whether `z == 0`, `x > y`, and `x == y` are always true, sometimes true, or never true. Let's first think about `Point A`. We know a few things about `Point A`:

• `x > y, x == y`:
`x` and `y` are equal to whatever was passed in as a parameters (any integers!) Are there any restrictions on the values that `x` and `y` could hold?
• `z == 0`:
`z` = 0 executes immediately before `Point A`. Is it possible for `z == 0` to be false at A?

`x > y` `z == 0` `x == y`
Point A
```public static void mystery(int x, int y) {
int z = 0;

// Point A
while (x != y) {
// Point B
z++;

if (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}
```

# Assertions: Engaging Point E

Now let's skip to Point E, where we're going to need to do a little bit more reasoning.

• `x == y`:
This code has a `while` loop that loops while `x != y`. This means that when we break out of the loop, `x == y`. Since `Point E` is after the while loop, what do we know about `x == y` there?
• `x > y`:
If `x == y` must be true, is it possible for `x > y` to be true?
• `z == 0`:
Evaluating whether `z == 0` is tricky. `z` is initially equal to 0. However, as soon as we go into the while loop, we execute `z++` (which would mean `z != 0`). So `z == 0` is true at `Point E` if we don't go into the `while` loop, and untrue if we do go into the `while` loop. Will we necessarily go through the `while` loop?

# Assertions: Engaging Point E

`x > y` `z == 0` `x == y`
Point E
```public static void mystery(int x, int y) {
int z = 0;

// Point A
while (x != y) {
// Point B
z++;

if (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}
```

# Assertions: Battling Point B

Okay, now we need to figure out what's going on inside the `while` loop! Let's take a look at `Point B` first.

• `x == y`:
We only enter/continue looping through a `while` loop while its test is true. So we only encounter `Point B` immediately after verifying that `x != y` is still true. Can x or y have changed values by `Point B`?
• `x > y`:
For an assertion to be `sometimes` true, we just need to be able to think of a case where it's true, and a case where it's false. `x` and `y` could have any initial values (since they're just whatever was passed in as parameters). Is there any reason that `x > y` must be true or false?
• `z == 0`:
`z` is initially equal to 0, and on our first iteration through the `while` loop, we hit `Point B` before we alter `z`'s value. However, after our first pass over `Point B`, we execute z++ (and `z != 0`). So will `z == 0` always, sometimes, or never be true at `Point B`?

# Assertions: Battling Point B

`x > y` `z == 0` `x == y`
Point B
```public static void mystery(int x, int y) {
int z = 0;

// Point A
while (x != y) {
// Point B
z++;

if (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}
```

# Assertions: Calculating Point C

Now let's take a look at `Point C`!

• `x > y`:
If we reach `Point C`, we entered the `if` statement: `if (x > y)`. This means that we will only reach `Point C` when ` x > y!` Is it possible for `x > y` to be false at `Point C`?
• `x == y`:
Since `x > y` must be true, this means that `x != y`. Additionaly, to enter/continue looping through the `while` loop, `x != y` must be true. `x` and `y` don't change values between the `while` loop test and `Point C`. Is it possible for `x == y` to be true at `Point C`?
• `z == 0`:
To get to `Point C`, we must pass `z++`. Is it possible for `z == 0` to be true at `Point C`?

# Assertions: Calculating Point C

`x > y` `z == 0` `x == y`
Point C
```public static void mystery(int x, int y) {
int z = 0;

// Point A
while (x != y) {
// Point B
z++;

if (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}
```

# Assertions: Deducing Point D

Finally, we need to figure out what's going on with `Point D`!

• `x > y`:
If `x > y` were true, then we would go into the `if` branch. But `Point D` is in the `else` branch. So what do we know about `x > y` at `Point D`?
• `x == y`:
Not going into `if (x > y)` tells us that `x <= y`. But can `x == y`? Recall that to enter/continue looping through the `while` loop, `x != y` must be true. Also x and y are not changed between the `while` loop test and `Point D`. So what do we know about `x == y` at `Point D`?
• `z == 0`:
To get to Point D, we must pass `z++`. Is it possible for `z == 0` to be true when we get to `Point D`?

# Assertions: Deducing Point D

`x > y` `z == 0` `x == y`
Point D
```public static void mystery(int x, int y) {
int z = 0;

// Point A
while (x != y) {
// Point B
z++;

if (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}
```

# You're done!

Nice work! Assertions are tough. Make sure to practice some more problems before the midterm! 