Control Structures in the C Programming Language

What are Control Structures?

Control structures determine the flow of a program. There are two basic types of control structures in most programming languages. There are selection type controls that make use of comparison and/or logical operators to select a course of action for the program, and there are loop type structures that repeat a set of instructions until some condition is met.

 

Selection Type Controls of a C Program

A selection type control is one that selects one of two courses of action based on some condition. There are two common forms; the 'if' statement and the 'switch' statement. The two code segments below demonstrate using these two selection control types to accomplish the same purpose.

 

The 'if' statement:

The 'if' statement uses comparison and logical operators to determine whether a condition is true or false. If the condition is true, a set of code following the 'if' statement is executed. The 'if' statement can have the option of using an 'else' or 'else if' statement to string multiple condition checks together in series. However, if you find yourself using several 'else if' statements together, you might want to consider using a 'switch' statements instead which is covered next. The 'if' statement is best suited for cases where there is a single yes or no aka true or false decision that must be made. When the possible answers to the question are numerous, the 'switch' statement may be more appropriate.

 

The 'switch' statement:

The 'switch' statement identifies a variable to be evaluated and defines a set of instructions to be executed for any value of that variable. This can be useful in defining state machines or branching to different sets of instructions based on user or sensor input.

 

The code examples here evaluate an int variable named "option" and executes different sets of instructions for values 1 through 3, and returns an "Invalid Entry" for all other values. Both code examples accomplish about the same thing, but when we have multiple possibilities to evaluate, the 'switch' statement is more efficient because it evaluates the condition only once, then branches to the appropriate set of instructions. The chain of 'if' statements evaluates the variable for each 'if' statement before moving on to the next one. In this short example, the difference in performance is probably insignificant, but if we have 100 possible options, it will become measurable.

/* An example of useing an 'if' statement */
        if(option == 1){
            printf("You selected option 1\n");
        }else if(option == 2)
        {
            printf("You selected option 2\n");
        }else if(option == 3)
        {
            printf("You selected option 3\n");
        }else
        {
            printf("Invalid Entry\n");
        }
 /* An example of using a 'switch' statement */      
        switch(option)
        {
            case 1:
                printf("You selected option 1\n");
                break;
            case 2:
                printf("You selected option 2\n");
                break;
            case 3:
                printf("You selected option 3\n");
                break;
            default:
                printf("Invalid Option\n");
                break;
        }

Loop Type Controls of a C Program

Often in programming we will find cases where we want to repeat a set of instructions over an over again. To do this we will use something called a loop. Sometimes we will want to loop until some condition is met. Other times we will want to loop a set number of times. In most programs there will be at least one loop that loops forever. There are different types of loops for all of these instances. Here we will cover 'for' loops, 'while' loops, and 'do...while' loops.

The 'for' loop:

The 'for' loop is used when we want to loop for a determined number of iterations. The syntax of a for loop is. Note the syntax in the the 'for' loop example.

 

  • 'i = 0' initializes the control variable.
  • 'i < 3' is the conditional evaluation.
  • 'i++' increments the control variable.

 

This example loops 3 times which can be seen in the demo output window, but 'for' loops can be infinite as well by simply only including the semi-colons within the parenthesis as shown in the infinite 'for' loop example.

/* An example of using a 'for' loop */
    int i;
    /* This for loop example loops 3 times */
    for(i = 0 ; i < 3 ; i++){
        /* Do something here */
        printf("In for loop %d times\n", i+1);
    }
The for loop flow chart illustrates how the for loop behaves in C programs. The for loop is used to perform a specific number of loops.
A for loop can be used to make our C program loop for a specific number of times.
/*An infinite 'for' loop*/
for( ; ; ){
    /*Do something here*/
}

The 'while' loop:

The 'while' loop will only execute if the conditional statement is true, but once it is true, the 'while' loop will continue to loop until it is false. This leads to two potential pitfalls. The conditional statement must be carefully established so that the loop will be entered when and only when intended. Unless we want a forever loop, code must also be included within the loop to make the condition false or the program will hang within the loop forever.

The while loop executes as long as the conditions is true.
/*A 'while' loop*/
while (i < 3){
    /*Do something here*/
}
/* Infinite 'while' loop*/
while (true){
    /*Do something here*/
}

The 'do ... while' loop:

The 'do ... while' loop will always execute its code the first time it appears in a thread, and will continue to do so while the condition set by the while portion is true. Care must be taken to make sure the loop can be escaped when intended.

/*A 'do ... while' loop*/
do{
    /*Do something here*/
}while (i < 3);
A do while loop always executes the first time it appears in a c program.
Home | Products | Electronics | Programming | SpazzOuts | Contact
© Copyright 2014 - 2017 SpazzTech LLC. All Rights Reserved
Made with Adobe Muse