Flylib.com

Books Software

 
 
 

Appendix D. EXPRESSIONS, STATEMENTS, AND PROGRAM FLOW

I l @ ve RuBoard

Appendix D. EXPRESSIONS, STATEMENTS, AND PROGRAM FLOW

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

Summary: Expressions and Statements

In C, expressions represent values, and statements represent instructions to the computer.

Expressions

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)) .

Statements

A statement is a command to the computer. Any expression followed by a semicolon forms a statement, although not necessarily a meaningful one. Statements can be simple or compound. Simple statements terminate in a semicolon, as shown in these examples:

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 themselves can be compound) enclosed in braces. The following while statement is an example:

while (years < 100)
{
    wisdom = wisdom + 1;
    printf("%d %d\n", years, wisdom);
    years = years + 1;
}
I l @ ve RuBoard
I l @ ve RuBoard

Summary: The while Statement

Keyword

The keyword for the while statement is while .

General Comments

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.

Form

while (

expression

)

statement

The statement portion is repeated until the expression becomes false or zero.

Examples

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

Summary: The for Statement

Keyword

The for statement keyword is for .

General Comments

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.

Form

for (

initialize ; test ; update

)

statement

The loop is repeated until test becomes false or zero.

Example

for (n = 0;  n < 10 ; n++)
      printf("%d %d\n", n, 2 * n+1);
I l @ ve RuBoard