Program Looping

Program Control

The for Statement

The general format:

for (expression1; expression2; expression3;)
     statement;

  1. expression1 is usually an assignment statement that assigns a value to a variable.
  2. expression2 is the condition, typically a relational expression.
  3. expression3 is an increment
  4. if expression2 evaluates to false, then the for statement terminates and execution passes to the next statement following the statement.
  5. if expression2 evaluates to true then the statement is executed. Then the variable is incremented according to expression3.

Examples:

for ( i  =  0;  i  < 100;  i++){
j = 10 * i;
}

for ( i  = 100;  i  < 10;  i--){
j = 10 * i;
}

for ( i  = 0;  i  < 1000;  i  += 5){
j = 10 * i;
}

i = 1;
for ( ;  i  < 1000;  i  += 5){
j = 10 * i;
}

for ( i  = 0;  i  <  1000;  i + +){
	for ( j  =  0;  j  < 10;  j++)
	k = j* i;
}

The while Statement

The while statement is useful for having the program loop through one statement or a block of statements as long as a specified condition is true.

The general format:

while (expression)
     statement; (or a block that begins with \{ and ends with \})

  1. the expression is evaluated
  2. if it evaluates to false, the while statement terminates and the execution passes to the first statement following statement.
  3. if the expression evaluates to true, then the statement(s) is executed and the execution returns to step 1.

Example:

main()
{
     int count;
     while (count <= 9)
     {
          printf("count=%d", count);
          ++count;
     }
}

The do ... while loop

General format:

do
     statement; 
while (expression);

The if and if-else Statements

The general format is

if (expression) statement1; or a block that begins with { and ends with }

The expression evaluates to either TRUE or FALSE.

Examples:

if (a > b)
 z = a;

if (a > b)
 z = a;
 z = b;

/* Demonstrates the use of if statements */  
#include <stdio.h>  
int main()  
{  
    int x, y;  
    /* Input the two values to be tested */  
    printf("\nInput an integer value for x: ");  
    scanf("%d", &x);  
    printf("\nInput an integer value for y: ");  
    scanf("%d", &y);  
    /* Test values and print result */  
    if (x == y)  
      printf("x is equal to y");  
    if (x > y)  
      printf("x is greater than y");  
    if (x < y)  
      printf("x is smaller than y");  
    return 0;  
}

Two or more simple expressions can be combined using AND - && or OR - ||, e. g. if (grade >= 70 && grade <= 79)

Else - if

if (a > b)
 z = a;
else 
z = b;

Example:

main()
{
     int grade;
     char last_name[30];
     /* last name can be up to 30 chars */
     printf("enter your name followed by a space then your grade\n");
     scanf("%s %d", & last_name,&grade);
     if (grade >= 90)
          printf("\n%s, congratulations, you have an A !",last_name);
     else
     printf("\n%s, sorry, you don't have an A", last_name);
}

The break and continue statements

The goto statement

/* Demonstrates the goto statement */
#include <stdio.h>
void main()
{
  int n;
start: ;
  puts("Enter a number between 0 and 10: ");
  scanf("%d", &n);
  if (n < 0 ||n > 10 )
    goto start;
  else if (n == 0)
    goto location0;
      else if (n == 1)
        goto location1;
          else
            goto location2;
location0: ;
  puts("You entered 0.");
  goto end;
location1: ;
  puts("You entered 1.");
  goto end;
location2: ;
  puts("You entered something between 2 and 10.");
end: ;
}

The switch statement

General Format:

switch (expression)
{
case template_1: statement(s);
case template_2: statement(s);
......
case template_n: statement(s);
default:  statement(s);
}

Example:

#include <stdio.h>
void main()
{
  int reply;
  puts("Enter a number between 1 and 5:");
  scanf("%d", &reply);
  switch (reply)
  {
    case 1:
      puts("You entered 1.");
    case 2:
      puts("You entered 2.");
    case 3:
      puts("You entered 3.");
    case 4:
      puts("You entered 4.");
    case 5:
      puts("You entered 5.");
    default:
      puts("Out of range, try again.");
  }
}

About this document ...

This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 -html_version 3.0 c2.tex.

The translation was initiated by Vijay & on Thu Feb 5 10:38:53 EST 1998


Vijay &
Thu Feb 5 10:38:53 EST 1998