C Ripped Apart :: Section 3
Author: Mike Ware
Website: [warebiz] :: "The Programmer's Domain" - http://warebiz.tripod.com
Email: warebiz@yahoo.com
Copyright © 2003 Michael Shawn Ware, All Rights Reserved.


You are here --> [Section 3 :: Controlling Execution With Control Structures]

"Jump To Articles"
    --> Introduction To Control Structures
    --> Selection Statements
    --> Switch Statements
    --> While Loops
    --> Do While Loops
    --> For Loops


Introduction To Control Structures

Controlling the flow of a program is a very important aspect of programming. There may be many different ways to find the solution to a particular problem, but you will want to find the most efficient solution. C++ has four types of control structures: sequential, selection (decision), repetition (loops), and subprograms (functions). These structures simply control the execution or program flow of a program.

Sequential
Sequential means that program flow moves from one statement to the next, that statement moves to the next, and so on. It is the simplest form of structures, and it is not likely that a programmer will use sequential structuring when developing complex programs.

Selection (decision)
Selection structures make decisions and perform commands dependent upon the decision. Selection structures involve "if statements" which are basically statements like "if this, then do that" and "if not this, then do that". With "if statements", we can also include "nested if statements", which are "if statements" inside other "if statements". Another form of selection structures is called a "switch statement", which is very efficient for certain situations but not others. "Switch statements" focus on the value of a particular variable and perform different "cases" accordingly.

Repetition
Repetition structures are used when something needs to be repeated a certain number of times through the use of a "loop". A loop is simply a statement that completes iterations or cycles until a certain value is reached. Once the test expression has been met, execution moves to the next executable statement. For instance, if you were to ask the user to enter ten values to find the average of the numbers, you could write a loop that would continue letting the user enter numbers until ten numbers had been entered.

Subprograms (functions)
A function is a subprogram that performs a specific task and may perhaps return a value to the statement that called or invoked it. Functions make programming very powerful because once a programmer develops a function to handle a particular situation, he can later use that function to accomplish goals in other programs. A function can call another function, which may call another function, and so on. It is the standard method to organize your program and break your program down into logical steps.

After a very brief intro to the different types of control structures, it's time to move on and find out how you can use each type. We will skip over sequential structures since they are self-explanatory and move on to selection statements. Read on for more information about "if statements"...

Selection Statements

There are generally three versions of selection statements to cover: one-way, two-way, and multi-way. One-way decision statements do some particular thing or they do nothing. Two-way decision statements do one thing or do another. Multi-way decision statements can do many different things depending on the value of an expression.

One-Way Decisions
One-way decisions are handled with an "if statement" and either do some particular thing or do nothing at all. The decision is based on a logical expression which either evaluates to true or false, 1 or 0 respectively. It is important to remember that logical expressions always evaluate to a value of true or false, and a nonzero value is always true and a value of zero is always false. If the logical expression is evaluated to true, then the corresponding statement is executed; if the logical expression evaluates to false, control goes to the next executable statement. The form of a one-way decision statement is as follows:


if (logical expression)
      stmtT;

The stmtT can be a simple or compound statement. Simple involves a single statement. Compound involves one or more statements enclosed with curly braces { }. A compound statement is called a block statement.

Example 1: simple statement


    int c = 3;
    .
    .
    .
    if ( c > 0 )
         printf("c = %d \n", c);

Example 2: compound statement


    int c = 10;
    int b = 5;
    .
    .
    .
    if ( c > 5 )
    {
            b = 2 * b + c;
            printf("%d\n", c * c * b);
    }

Two-Way Decisions
Two-way decisions are handled with "if / else statements" and either do one particular thing or do another. Similar to one-way decisions, the decision is based on a logical expression. If the expression is true, stmtT will be executed; if the expression is false, stmtF will be executed. The form of a two-way decision statement is as follows:


    if ( logical expression )
            stmtT;
    else
            stmtF;

stmtT and stmtF can be simple or compound statements. Remember that compound statements are always enclosed with curly braces { }.

Example 1:


    int c = 10;
    .
    .
    .
    if ( c > 0 )
            printf("c is a positive integer");
    else
            printf("c is a negative integer");

Example 2:


    int num;
    .
    .
    [num gets a value]
    .
    .
    if ( num % 2 == 0 )
            printf("num is an even integer");
    else
            printf("num is an odd integer");


Practical use example:

Suppose a programmer needs to write code that will calculate the earnings by workers who are paid an hourly wage, with weekly hours greater than 40 being paid "time and a half". Suppose weekly hours and hourly rate are known in the program. Two options of code to handle this situation are as follows:

Option 1 using simple statements:


    if ( weeklyHours <= 40 )
            earnings = hourlyRate * weeklyHours;
    else
            earnings = 40 * hourlyRate + ( weeklyHours - 40 ) * hourlyRate * 1.5;

Option 2 using a simple and compound statement:


    if ( weeklyHours <= 40 )
            earnings = hourlyRate * weeklyHours;
    else
    {
            offHours = weeklyHours - 40;
            regpay = 40 * hourlyRate;
            earnings = regpay + offHours * hourlyRate * 1.5;
    }

Multi-Way Decisions
Multi-way decision statements involve using "if / else if" statements, "nested ifs", or "switch" statements. These statements are used to evaluate a logical expression that could have several possible values. "if / else if" statements are often used to choose between ranges of values. Switch statements are discussed in the next article [see Switch Statements].

The form of a multi-way decision using "if / else if" statements is as follows:


    if ( logical expression )
            stmtT1;
    else if ( logical expression )
            stmtT2;
    else if ( logical expression )
            stmtT3;
    .
    .
    .
    else if ( logical expression )
            stmtTN;
    else
            stmtF;

If the first logical expression is evaluated to true, then stmtT1 is executed. If the second logical expression is true, then stmtT2 is executed and so on down the line of logical expression "tests". If none of the logical expressions are true, then the statement after "else" is executed which is stmtF.

The form of a multi-way decision using "nested ifs" is as follows:


    if ( conditionA )
    {
            if ( conditionB )
                     stmtBT;
            else
                     stmtBF;
    }
    else
            stmtAF;

If conditionA is evaluated to true, then execution moves into the nested if and evaluates conditionB. If conditionA is evaluated to false, then stmtAF is executed.

Example 1:


    int x;
    .
    .
    [x gets a value]
    .
    if ( x > 0 )
            printf("x is positive");
    else if ( x = 0 )
            printf("x is zero");
    else
            printf("x is negative");

Example 2:


    char ch;
    .
    .
    [ch gets a value]
    .
    if ( ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' )
            printf("ch contains a letter");
    else if ( ch >= '0' && ch <= '9' )
            printf("ch represents a digit");
    else if ( ch == ' ' || ch == '\t' || ch == '\n' )
            printf("ch is white space");
    else
            printf("ch is a misc. character");

Example 3:


    int score;
    .
    .
    [score gets a value]
    .
    .
    if ( score >= 90 )
            grade = 'A';
    else if ( score >= 80 )
            grade = 'B';
    else if ( score >= 70 )
            grade = 'C';
    else if ( score >= 60 )
            grade = 'D';
    else
            grade = 'F';

Example 4:


            if ( x > 25 )
                if ( x < 50 )
                    printf("x is a mid-range value");
                else
                    printf("x is a high value");
            else
                printf("x is a low value");

We have now discussed the options for writing code for one-way decisions, two-way decisions, and multi-way decisions, but there is one option I have saved for a section by itself. Read on for more about switch statements...

Switch Statements

A switch statement is yet another option for writing code that deals with multi-way decisions. I saved it for a section by itself because some programmers feel using a switch statement is not good programming style. Let's explore the ins and outs of a switch statement.

The form of a multi-way decision using a "switch" statement is as follows:


    switch ( int expression )
        stmt;

Where typically stmt is a block statement containing a series of case labels and optionally a default label. The expression being "tested" must be integral in nature, which means it must be of type int. Switch statements offer an alternative to an "if / else if" structure which has multiple possible paths based on the value of a single expression. A revised form for a switch statement is as follows:


    switch ( int expression )
    {
            case label - 1 :
                    stmt-list-1;
                    break;
            case label - 2 :
                    stmt-list-2;
                    break;
            case label - 3 :
                    stmt-list-3;
                    break;
            .
            .
            .
            case label - n :
                    stmt-list-n;
                    break;
            default :
                    stmt-list-default;
    }

During execution, the int expression is evaluated. If the value of the expression matches label - 1, then stmt-list-1 (and perhaps all lists below if no command break is issued) will be executed. If not, execution will move on to check label - 2 and so on. If no labels match the expression, default will be executed. Inside each case and at the end of every statement list (except default) there may be a break statement, which terminates the innermost enclosing switch or loop statement (not recommended with loops).

Here are some final notes about switch statements: the expression being tested must result in an integral value (int or single char), case labels must be integral constants (either literals or named constants), each label within a switch should be unique, and each stmt-list may contain 0 or more stmts which do not need to enclosed with curly braces { }.

Example Code Segment:

Suppose a variable named score is an int variable that contains the number of points scored on a football play. Consider the following code:


    switch ( score )
    {
            case 1 : printf("Extra Point");
                    break;
            case 2 : printf(Safety of two point conversion");
                    break;
            case 3 : printf("Field Goal");
                    break;
            case 6 : printf("Touchdown");
                    break;
            default : printf("Invalid score on a football play.");
    }

With selection and switch statements covered, it's time to talk about iteration (or looping) in a program. This, of course, involves using loop statements. Read on to enter the realm of looping...

While Loops

A loop is a statement of code that issues instructions repeatedly until the logical expression of the loop evaluates to false or zero. It is simply a way of repeating code over and over again until some particular value is reached. It can also be an effective way of making sure the user enters the correct type of input (data validation). A loop is different from an if statement in that if the logical expression is true, the stmt will be executed and control will go back to check the logical expression again, ..., and again, until eventually the logical expression becomes false. One thing to avoid is writing code that produces an infinite loop, which is a loop that never terminates until the user issues ctrl-break during execution. In order to avoid infinite loops, you need a statement(s) inside the body of the loop that changes some part of the logical expression. We will talk about three types of loops: while, do while, and for. The form of a while loop is as follows:


    while ( logical expression )
            stmt;

The while loop will iterate "while" the logical expression is true or evaluates to a nonzero value. When the logical expression evaluates to false, the loop ends and control is sent to the next executable statement. This type of loop is called an entry-level condition loop because the condition being tested (the logical expression) appears at the beginning of the loop and will be evaluated as soon as execution enters the loop sequence.

Example 1:

Example 1:


    int x = 7;
    while ( x > 0 )
    {
            printf("x = %d\n", x);
            x--;
    }


Example 2 [provides generic data validation]:

Suppose the user of a program is to input an integer in the range -10 to 10 (inclusive). Using data validation, write a section of code to prompt the user for this value:


    int k = -99;

    while ( k < -10 || k > 10 )
    {
            printf("\n Enter an integer value (between -10 and 10): ");
            scanf("%d", &k);
    }

Example 3:

Write a section of code that will display all multiples of 5 that are less than 500.


    int x = 5;
    while ( x < 500 )
    {
            printf("x = %d\n", x);
            x = x + 5;
    }

That wraps up all I have to say about while loops so let's move on to the next loop of interest. Read on for more about do while loops...

Do While Loops

A do while loop is another type of repetition statement. It is exactly the same as the while loop except it initially performs operations and then evaluates the logical expression of the loop to determine what should happen next. This type of loop is referred to as an exit condition loop sequence. Normally with a while loop, some part of the logical expression in the loop must be initialized before execution of the loop. Most programmers prefer using a do while loop when validating user input. The form of a do while loop is as follows:


    do
            stmt(s);
    while ( logical expression );

The stmt in the loop may be simple or complex. Complex statements must be enclosed with curly braces { }. Consider the following code:

Example 1:


    int number;
    do
    {
            printf("\nEnter a positive number greater than 0: ");
            scanf("%d", &number);
    }
    while ( number <= 0 || (int)number != number );

Example 2:


    int number;
    do
    {
            printf("Enter a number between 0 and 100: ");
            scanf("%d", &number);
    }
    while ( number <= 0 || number >= 100 );

Having discussed while and do while loops, there is one more loop type I would like to cover: the for loop. Read on for more about for loops...

For Loops

A for loop is another way to execute instructions that need to be repeated in C++ (repetition structure). Most programmers like to use for loops because the code can be written in a more compact manner compared to the same loop written with a while or do while loop. A for loop is also important to understand because it is the preferred method to use when processing arrays and other topics in C++ [for info on arrays see One-Dimensional Arrays and Two-Dimensional Arrays]. A for loop is generally used when you know exactly how many times a section of code needs to be repeated. The general form of a for loop is as follows:


    for ( expression1; expression2; expression3 )
            stmt(s);

where stmt(s) is a single or compound statement. expression1 is used to initialize the loop; expression2 is used to determine whether or not the loop will continue; expression3 is evaluated at the end of each iteration or cycle.

NOTE 1: expression2 is tested before the stmt(s) and expression3 are executed; it is possible that the body of the loop is never executed or tested.

NOTE 2: Any or all of the 3 expressions in a for loop can be omitted, but the 2 semi-colons must be provided. If expression1 is omitted, there is no initialization done in the loop, but you might not need any initialization. If expression2 is omitted, there is no test section so you will essentially have an infinite loop. If expression3 is omitted, there is no update as part of the for statement, but the update could be performed in the body of the loop.

In general, a for loop can be written as an equivalent while loop and vice versa.


    for ( expression1; expression2; expression3 )
            stmt(s);

is equivalent to...


    expression1;
    while ( expression2 )
    {
            stmt(s);
            expression3;
    }

Example 1:

Write a for loop that will display all odd integers between 1 and 51:


    for ( int k = 1; k <= 51; k += 2 )
            printf("%d\n", k);

Example 2:

Write a for loop that will display a "countdown" of integers from 20 to 1:


    for ( int k = 20; k >= 1; k-- )
            printf("%d\n", k);

Now that I've covered while, do while, and for loops, I can introduce you to some methods of controlling repetition in a program. Read on for more about controlling repetition with sentinel values and prime reads...

Move on to next set of topics: Section 4 - Functions and Program Flow Topics

Back to Top