Control Structures - Intro, Selection

Flow of Control:

Flow of control through any given function is implemented with three basic types of control structures:

The function construct, itself, forms another way to affect flow of control through a whole program. This will be discussed later in the course.

Some useful tools for building programs or program segments


True and False


Logical Operators:

The arithmetic comparison operators in C++ work much like the symbols we use in mathematics.  Each of these operators returns a true or a false.
  x == y        // x is equal to y
  x != y        // x is not equal to y
  x < y         // x is less than y
  x <= y        // x is less than or equal to y
  x > y         // x is greater than y
  x >= y        // x is greater than or equal to y

We also have Boolean operators for combining expressions.  Again, these operators return true or false

  x && y        // the AND operator -- true if both x and y are true
  x || y        // the OR operator -- true if either x or y (or both) are true
  !x            // the NOT operator (negation) -- true if x is false

These operators will be commonly used as test expressions in selection statements or repetition statements (loops).
 

Examples of expressions

  (x > 0 && y > 0 && z > 0)     // all three of (x, y, z) are positive
  (x < 0 || y < 0 || z < 0)     // at least one of the three variables is negative

  ( numStudents >= 20 && !(classAvg < 70))
        // there are at least 20 students and the class average is at least 70
  
  ( numStudents >= 20 && classAvg >= 70)
        // means the same thing as the previous expression

Short Circuit Evaluation:


Selection Statements


The if/else Selection Statement


Examples



   if (grade >= 68)
      cout << "Passing";

// Notice that there is no else clause.  If the grade is below 68, we move on.


   if (x == 0)
      cout << "Nothing here";
   else
      cout << "There is a value";

// This example contains an else clause.  The bodies are single statements.


   if (y != 4)
   {
      cout << "Wrong number";
      y = y * 2;
      counter++;
   }
   else
   {
      cout << "That's it!";
      success = 1;
   }

Multiple statements are to be executed as a result of the condition being true or false.  In this case, notice the compound statement to delineate the bodies of the if and else clauses.



Be careful with ifs and elses.  Here's an example of an easy mistake to make.  If you don't use { }, you may think that you've included more under an if condition than you really have.

// What output will it produce if val = 2?   Does the "too bad" statement really go with the "else" here?

   if (val < 5)
      cout << "True\n";
   else
      cout << "False\n";
      cout << "Too bad!\n";

* Indentation is only for people!  It improves readability, but means nothing to the compiler.

Example links

Some common errors

What's wrong with these if-statements? Which ones are syntax errors and which ones are logic errors?

The switch statement


 

The Conditional Operator

There is a special operator known as the conditional operator that can be used to create short expressions that work like if/else statements.