Statements

These simple examples demonstrate the use of common C statements.

if  |  for  |  while  |  do  |  continue  |  break  |  switch

if

The if statement allows conditions to be tested and code can be skipped or executed based on the result of the test.

Relational operators are:

   <   Less than
   >   Greater than
   ==  Equal to
   !=  Not equal to
   <=  Less than or equal to
   >=  Greater than or equal to

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Count;

  Count = 1;
  if (Count == 1)
  {
    printf("The count is: %d\n", Count);
    return 0;
  }
}

ALERT!

C uses two equal signs to specify 'equal to'. The statement if (Count = 1) is valid and will compile with no indication of error or warning. But this is not an 'equal to' condition test. The result of if (Count = 1) will always be true! Always check your if statements to ensure that you are using TWO equal signs when you intend to test for an 'equal to' condition.

Top

for

The for statement has three parts, initialize, continue condition, and increment. This for statement starts Count at zero and while Count is less than or equal to 7, Count is incremented by one.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Count;

  for (Count = 0; Count <= 7; Count++)
  {
    printf("The count is: %d\n", Count);
  }
  return 0;
}

Count to 100 by 5s starting with 5.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Count;

  for (Count = 5; Count <= 100; Count = Count + 5)
  {
    printf("The count is: %d\n", Count);
  }
  return 0;
}
Top

while

The while statement continues to execute while a condition is true. In this example, "Hi" will only be printed once because the next statement sets Done to one which will cause the while to end.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Done;

  Done = 0;
  while (Done == 0)
  {
    printf("Hi\n");
    Done = 1;
  }
  return 0;
}
Top

do

The do statement continues to execute while a condition is true. The main difference between do and while is that do is always done at least once because the termination condition is tested last. In this example, "Hi" will be printed even though the condition 'Done equal 0' is false.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Done;

  Done = 1;
  do
  {
    printf("Hi\n");
  }
  while (Done == 0);
  return 0;
}
Top

continue

The continue statement is used inside for, while, and do loops to skip all code from the continue statement to the end of the loop block and continue with the next iteration of the loop.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Count;

  for (Count = 0; Count <= 7; Count++)
  {
    if (Count == 3)
    {
      continue;
    }
    printf("The count is: %d\n", Count);
  }
  return 0;
}
Top

break

The break statement is used inside for, while, and do loops to break out of the loop and proceed to the next statement after the loop block.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Count;

  for (Count = 0; Count <= 7; Count++)
  {
    if (Count == 3)
    {
      break;
    }
    printf("The count is: %d\n", Count);
  }
  return 0;
}
Top

switch

The switch statement allows a program to efficiently handle multiple-branch decisions. The break statement prevents the code from falling into other cases. See what happens if you don't code the break statement by deleting the first break statement, then run the program and observe the results.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Choice;

  Choice = 1;
  switch (Choice)
  {
    case 1 :
      printf("Choice is equal to 1\n");
      break;
    case 2 :
      printf("Choice is equal to 2\n");
      break;
    default :
      printf("Choice is not equal to 1 or 2\n");
  }
  return 0;
}

A switch statement using char instead of int.

#include <stdio.h>

int main(int argc, char *argv[])
{
  char Choice;

  Choice = 'a';
  switch (Choice)
  {
    case 'a' :
      printf("Choice is equal to a\n");
      break;
    case 'b' :
      printf("Choice is equal to b\n");
      break;
    default :
      printf("Choice is not equal to a or b\n");
  }
  return 0;
}
Top