2.10. Control Constructs

Control constructs determine the flow of execution of the program. With no control constructs, statements execute sequentially. Some control constructs are selection statements meaning that it selects which code to execute. Other control constructs are loops, where the control construct determines how many times the loop will execute.

C keywords related to selection statements include: if, else, switch, case, break, default.

C keywords related to loops include: for, while, do, break, continue.

2.10.1. if block

if( condition ) statement;

if( condition ) {
   statement block;
}

if( condition ) {
   statement block1;
} else {
   statement block2;
}
if( x < 10 && y < x )
   printf( "x is less than 10, but greater than y" );
else {
   if( x < 10 )
      printf( "x is less than 10, and not greater than y" );
   else {
      if( y < x )
         printf( "x is not less than 10 and is greater than y" );
      else
         printf( "x is not less than 10 and not greater than y" );
   }
}

Note that the two pairs of brackets are not needed, but they add to the clarity of the program. Avoid the ambiguous else …

if( condition1 )
   if( condition2 )
      statement;
else {          /* WRONG */
   statement block;
}

2.10.2. Relational and Logical Operators

Critical to any control construct are conditions statements that evaluate to either True or False (boolean statements).

Note

In C, extend the Boolean statements to integers. 0 and NULL are False, and any other valid data value is True.

2.10.2.1. Relational Operators

Relational operators evaluate how two items compare to each other.

Operator Meaning
== Equal to each other (note: a == b NOT a = b)
!= Not equal
> Greater than
< Less than
<= Less than or equal
>= Greater than or equal

Note

C does not have relational operators to evaluate strings or other data structures – only the fundamental data types. See Basic data types. Other functions may be used for these evaluations, such as strcmp().

2.10.2.2. Logical Operators

Logical operators allow us to combine other boolean expressions to form one based on the desired logic.

Operator Meaning Example
&& AND a && b
|| OR a || b
! NOT !a

Parenthesis ( ) may also be used to control the order in which boolean expressions are evaluated.

2.10.3. while loops

while( operator ) {
   statement block;
}

2.10.4. For loop

Here we present another looping construct. It is most often used when iterating through a sequence of a pre-defined length.

for( expr1; expr2; expr3 ) {
   code block;
}

Is equivalent to:

expr1;
while( expr2 ) {
   code block;
   expr3;
}
  • expr1 is the initializer of the for loop.
  • expr2 is the condition for continuing with the loop.
  • expr3 is evaluated after each execution of the code block and is usually used to facilitate iterating through the loop.
#include <stdio.h>

#define MAX 5

int main(void)
{
    int i, x=1, y=1;

    for( i = 1; i < MAX; i++ ) {
        x = i * y;
        y = x*i;
    }
    printf( "x = %d, y = %d\n", x, y);

    return 0;
}

Now compete Homework 3 - Control Constructs and Newton’s Square Root Algorithm.