Program Looping
The general format:
for (expression1; expression2; expression3;)
statement;
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 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 \})
Example:
main()
{
int count;
while (count <= 9)
{
printf("count=%d", count);
++count;
}
}
for ( ; expression ; ) while ( expression )
General format:
do
statement;
while (expression);
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)
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);
}
for ( count = 0; count < 10; count++ )
{
if ( count == 5 )
break;
}
/* 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: ;
}
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.");
}
}
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