| I l @ ve RuBoard |
Summary: Expressions and Statements
Summary: The while Statement
Summary: The for Statement
Summary: The do while Statement
Summary: Using if Statements for Making Choices
Summary: Multiple Choice with switch
Summary: Program Jumps
| I l @ ve RuBoard |
| I l @ ve RuBoard |
In C, expressions represent values, and statements represent instructions to the computer.
An expression is a combination of operators and operands. The simplest expression is just a constant or a variable with no operator, such as 22 or beebop . More complex examples are 55 + 22 and vap = 2 * (vip + (vup = 4)) .
A
statement
is a command to the computer. Any expression followed by a semicolon forms a statement, although not
Declaration statement
int toes;
Assignment statement
toes = 12;
Function call statement
printf("%d\n", toes);
Control statement
while (toes < 20) toes = toes + 2;
Null statement
; /* does nothing */
Compound statements
, or
blocks
, consist of one or more statements (which
while (years < 100)
{
wisdom = wisdom + 1;
printf("%d %d\n", years, wisdom);
years = years + 1;
}
| I l @ ve RuBoard |
| I l @ ve RuBoard |
The keyword for the while statement is while .
The while statement creates a loop that repeats until the test expression becomes false, or zero. The while statement is an entry-condition loop; the decision to go through one more pass of the loop is made before the loop has been traversed. Therefore, it is possible that the loop is never traversed. The statement part of the form can be a simple statement or a compound statement.
while ( expression ) statement
The statement portion is repeated until the expression becomes false or zero.
while (n++ < 100)
printf(" %d %d\n",n, 2*n+1);
while (fargo < 1000)
{
fargo = fargo + step;
step = 2 * step;
}
| I l @ ve RuBoard |
| I l @ ve RuBoard |
The for statement keyword is for .
The for statement uses three control expressions, separated by semicolons, to control a looping process. The initialize expression is executed once, before any of the loop statements are executed. If the test expression is true (or nonzero), the loop is cycled through once. Then the update expression is evaluated, and it is time to check the test expression again. The for statement is an entry-condition loop; the decision to go through one more pass of the loop is made before the loop has been traversed. Therefore, it is possible that the loop is never traversed. The statement part of the form can be a simple statement or a compound statement.
for ( initialize ; test ; update ) statement
The loop is repeated until test becomes false or zero.
for (n = 0; n < 10 ; n++)
printf("%d %d\n", n, 2 * n+1);
| I l @ ve RuBoard |