Northcentral Technical College Introduction To Programming
10-152-310
Introduction to Programming with JavaScript NTC 10-152-310

Learning Plan 5 Combining loop and if structures

# Combining loop and if structures

#### File: /Learning Plan 5/loopAndIfs.html

## Combining Loops and Ifs - It's getting interesting now!

#### Question: What can we do when we combine loops and ifs?

##### (Answer: Everything! That pretty much sums up programming.)

- Repetition (Loops) and Logic (Ifs)
  - Flow control
    - What should I do inside this iteration?
    - What color should I draw this pixel?
    - Should I add or subtract?
    - What code should I run for this record?
    - Is this a valid number or should I ask the user to enter it again?
    - Should a new page be started?
    - Should a total summary page be printed now?
  - Ending the loop
    - Should I stop this loop now?
    - Did they enter the code to end the loop?
    - Did I reach some value where I'm supposed to stop the loop?
    - Is everything OK? Is there an error that should stop the loop?
    - Are there any more records?

### Adding Logic to a Loop

- We've seen this loop before
- Let's add an if statement and output something different depending on the value of the loop counter.

- Let's expand this some more. (And add constants.)

### Full Example

- Here are some full examples: Process Student Enrollments
- Here are some full examples: Process Inventory Items

### Let's Practice!

- 5.2 Lab 2
  - 5.2 Assignment 2

### How do I stop this thing?

> Shutting down a loop when you want to.

- There are several ways to stop a loop. Some of them require that we do some type of test inside the loop. Here's the most basic example with the test being the while() test. This is very common.

    This is the firstStop() function.

- Sometimes we need to test a condition and then quit before we do some task. We don't want to add something to a total if they entered "quit", for instance. We can do this with an `if` statement like this:

    This is the secondStop() function.

- We can also take different paths depending on what was entered.

    This is the thirdStop() function.

### Stopping Loops with a Boolean

> This technique gives the developer more control and is very handy when we start using functions.

- This technique requires a boolean variable to work. When we want to stop we just set the variable to `false` like this:

    This is the stopWithBooleanVar() function.

- This is what we did in the above code.
  - We created a boolean variable and set it to `true`.
  - We made a `while` loop with the variable as its test.
  - Then we set the variable to `false` when we wanted to quit the loop.
- Does this look odd to you?
- Why didn't we have to do this?
- If a variable has been set to `true` or `false` then it is already a `boolean expression`. We don't have to test its value, it's already `true` or `false`. So, this is correct and is what you will see in books and on the job:

### Testing Input Data

> If a user can enter the wrong thing then they will enter the wrong thing.

- So far, we have been relying on the user of our applications (you and me) to enter what we asked them to. But, they won't always do it right and we don't want our programs to just fail. The first technique for checking data that we'll learn is to make sure that they did enter a number when we ask them to. For that we use this function:

`isNaN("100");`

- `NaN` means `N`ot `a` `N`umber. The `isNaN()` function returns `true` if what the user entered was NOT a number. It returns `false` if it CAN be converted to a number with the `Number()` function.
- Here's some examples with some live code:

    This is the isNaNDemos() function.

- Here's how it's used.

- Here's a larger example

- And one more even bigger example. You can never have too many examples!

### Breaking News!

- There is one more way of stopping loops that is very useful and is used heavily. It's the `break;` statement.
- If JavaScript encounters a `break;` statement inside of a `while` or a `for` loop, it just stops the loop. Just like that!
- This allows us to stop a loop whenever we want to, not just at the top or bottom of the loop.
- We can also avoid the sometimes tricky logic of using a boolean variable for stopping. Once the `break;` is encountered the loop just stops!

### More Practice!

- 5.2 Lab 3
  - 5.2 Assignment 3