The Basics of C Programming

Branching and Looping

In C, both if statements and while loops rely on the idea of Boolean expressions. Here is a simple C program demonstrating an if statement:

#include int main() { int b; printf("Enter a value:"); scanf("%d", &b); if (b < 0) printf("The value is negativen"); return 0; }


This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the Boolean expression evaluates to True, then C executes the single line immediately following the if statement (or a block of lines within braces immediately following the if statement). If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.

Here's slightly more complex example:

#include <stdio.h>

int main()
    int b;
    printf("Enter a value:");
    scanf("%d", &b);
    if (b < 0)
        printf("The value is negative\n");
    return 0;

In this example, the else if and else sections evaluate for zero and positive values as well.

Here is a more complicated Boolean expression:

if ((x==y) && (j>k))

This statement says, "If the value in variable x equals the value in variable y, and if the value in variable j is greater than the value in variable k, then set the variable z to 1, otherwise set the variable q to 10." You will use if statements like this throughout your C programs to make decisions. In general, most of the decisions you make will be simple ones like the first example; but on occasion, things get more complicated.

Notice that C uses == to test for equality, while it uses = to assign a value to a variable. The && in C represents a Boolean AND operation.

Here are all of the Boolean operators in C:

  equality          ==
  less than         <
  Greater than      >
  <=                <=
  >=                >=
  inequality        !=
  and               &&
  or                ||
  not               !

You'll find that while statements are just as easy to use as if statements. For example:

while (a < b)
    printf("%d\n", a);
    a = a + 1;

This causes the two lines within the braces to be executed repeatedly until a is greater than or equal to b. The while statement in general works as illustrated to the right.

C also provides a do-while structure:

#include <stdio.h>

int main()
    int a;

    printf("Enter a number:");
    scanf("%d", &a);
    if (a)
        printf("The value is True\n");
    return 0;

The for loop in C is simply a shorthand way of expressing a while statement. For example, suppose you have the following code in C:

while (x<10)
    blah blah blah
    x++; /* x++ is the same as saying x=x+1 */

You can convert this into a for loop as follows:

for(x=1; x<10; x++)
    blah blah blah

Note that the while loop contains an initialization step (x=1), a test step (x<10), and an increment step (x++). The for loop lets you put all three parts onto one line, but you can put anything into those three parts. For example, suppose you have the following loop:

while (a < b)

You can place this into a for statement as well:

for (a=1,b=6; a < b; a++,printf("%d\n",a));

It is slightly confusing, but it is possible. The comma operator lets you separate several different statements in the initialization and increment sections of the for loop (but not in the test section). Many C programmers like to pack a lot of information into a single line of C code; but a lot of people think it makes the code harder to understand, so they break it up.