## Chapter 4: Basic Control Flow

## Chapter Goals

- To be able to implement decisions using `if` statements
- To understand statement blocks
- To learn how to compare integers, floating-point numbers, and strings
- To develop strategies for processing input and handling errors
- To understand the Boolean data type
- To avoid infinite loops and off-by-one errors

## The `if` Statement

- The `if` statement is used to implement a decision.
- It has two parts: a test and a body.
- Example:

  >     if (area < 0)
  >        cerr << "Error: Negative area.\n";

- Multiple statements can be grouped together in a block statement by enclosing them in braces `{ }`:

  >     if (area < 0)
  >     {
  >        cerr << "Error: Negative area.\n";
  >        return 1;
  >     }

## The `if` Statement (Syntax 4.1 : `if` Statement)

Syntax 4.1 : `if` Statement
    if (condition) statement
Example: |
    if (x >= 0) y = sqrt(x);
Purpose: | Execute the statement if the condition is true.

## The `if` Statement (Syntax 4.2 : Block Statement)

Syntax 4.2 : Block Statement
    {
       statement1
       statement2
       ...
       statementn
    }
Example: |
    {
       double length = sqrt(area);
       cout << area << "\n";
    }
Purpose: | Group several statements into a block that can be controlled by another statement.

## The `if` Statement (area1.cpp)

## The `if/else` Statement

- The `if/else` consists of a condition of two alternatives.
  - The first condition is performed is the condition is true.
  - The second condition is performed if the condition is false.

  >     if (area >= 0)
  >        cout << "The side length is " << sqrt(area) << "\n";
  >     else
  >        cout << "Error: Negative area.\n";

- The `if/else` statement is a better choice than a pair of `if` statements with complementary conditions.

  >     if (area >= 0) /* complementary conditions */
  >        cout << "The side length is " << sqrt(area) << "\n";
  >     if (area < 0) /* complementary conditions */
  >        cout << "Error: Negative area.\n";

## The `if/else` Statement (Syntax 4.3 : `if/else` Statement)

Syntax 4.3 : `if` Statement
    if (condition) statement1 else statement2
Example: |
    if (x >= 0) y = sqrt(x); else cout << "Bad input\n";
Purpose: | Execute the first statement if the condition is true, the second statement if the condition is false.

## The `if/else` Statement (Syntax 4.3 : `if/else` Statement)

## Relational Operators

- C++ has six relational operators to implement conditions:

  C++ | Description | Example | Notes
  `>` | greater than | `a > 5` |
  `>=` | greater than or equal to | `x >= 5` | Be careful to not write => Remember that the symbols appear in the order you say them
  `<` | less than | `x < 10` |
  `<=` | less than or equal to | `x <= 100` | Be careful to not write =< Remember that the symbols appear in the order you say them
  `==` | equal to | `a == 5` | Don't confuse with = which is assignment.
  `!=` | not equal | `a != 5` | The ! is supposed to be the line that "crosses through" the equal sign.

## Relational Operators (comparing strings)

- The relational operators listed above can also be used to compare strings using lexicological comparison.
- "car" is less than "cargo"

  > `c` | `a` | `r`

  > `c` | `a` | `r` | `g` | `o`

  "cargo" is less than "cathode"

  > `c` | `a` | `r` | `g` | `o`

  > `c` | `a` | `t` | `h` | `o` | `d` | `e`

## Input Validation

- Input validation is an application of the if statement that verifies the user has given reasonable input.
- Example

  >     double area;
  >     cin >> area;

  user types "`five`" and hits return! (Causing `cin` to fail).
- After every input a good program will test:

  >     if (cin.fail())

- The actual input should also be validated, even if the type is correct.

  >     if (area < 0)

- Other strategies:

  -     if (cin)
           /* the stream did not fail */
        else
           /* the stream failed */

  - `if (cin >> x) ...`

## Input Validation (`area3.cpp`)

## Simple Loops

- A loop is a block of code that can be performed repeatedly.
- A loop is controlled by a condition that is checked each time through the loop.
- The `while` statement makes a check before each execution of the code.

  >     /* how long does it take an investment to double? */

  >     while( balance < 2 * initial_balance)
  >     {
  >        balance = balance * ( 1 + rate / 100);
  >        year++;
  >     }

## Simple Loops (Syntax 4.4 : `while` Statement)

Syntax 4.4 : `while` Statement
    while (condition) statement
Example: |
    while (x >= 10) x = sqrt(x);
Purpose: | Execute the statement while the condition remains true.

## Processing a Sequence of Inputs (Sentinels)

- Whenever you read a sequence of input values, you need to have some method of terminating the input.
- A number used to signal termination is called a sentinel.
- Sentinels only work if there is some restriction on the input.
- Common sentinel values are 0 or -1.

## Processing a Sequence of Inputs (`sentinel.cpp`)

## Processing a Sequence of Inputs (Causing the Stream to Fail)

- When reading input from the console, you can close the stream manually.
  - Ctrl + Z in Windows
  - Ctrl + D in UNIX
- Reading from a closed stream causes the stream to enter the failed state.

## Processing a Sequence of Inputs (`maxtemp.cpp`)

## Using Boolean Variables

- The bool type can hold exactly two values, denoted false and true.
- Boolean variables are named after George Boole (1815-1864), a pioneer in the study of logic.
- Example:

  >     bool more = true;
  >     while (more)
  >     {
  >        cin >> next;
  >        if (cin.fail())
  >           more = false;
  >        else
  >        {
  >            // process next
  >        }
  >     }

- Don't:

  >     while(more == false) /* don't */

  >     while(more != false) /* don't */