Flylib.com

Books Software

 
 
 

Summary: The do while Statement

I l @ ve RuBoard

Summary: The do while Statement

Keywords

The keywords for the do while statement are do and while .

General Comments

The do while statement creates a loop that repeats until the test expression becomes false or zero. The do while statement is an exit-condition loop; the decision to go through one more pass of the loop is made after the loop has been traversed. Therefore, the loop must be executed at least once. The statement part of the form can be a simple statement or a compound statement.

Form

do

statement

while (

expression

);

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

Example

do
    scanf("%d", &number)
while(number != 20);
I l @ ve RuBoard
I l @ ve RuBoard

Summary: Using if Statements for Making Choices

Keywords

The keywords for if statements are if and else .

General Comments

In each of the following forms, the statement can be either a simple statement or a compound statement. A "true" expression, more generally , means one with a nonzero value.

Form 1

if (

expression

)

statement

The statement is executed if the expression is true.

Form 2

if (

expression

)

statement1

else

statement2

If the expression is true, statement1 is executed. Otherwise, statement2 is executed.

Form 3

if (

expression1

)

statement1

else if (

expression2

)

statement2

else

statement3

If expression1 is true, statement1 is executed. If expression1 is false but expression2 is true, statement2 is executed. Otherwise, if both expressions are false, statement3 is executed.

Example

if (legs == 4)
    printf("It might be a horse.\n");
 else if (legs > 4)
     printf("It is not a horse.\n");
 else    /* case of legs > 4 */
{
     legs++;
     printf("Now it has one more leg.\n")
}
I l @ ve RuBoard
I l @ ve RuBoard

Summary: Multiple Choice with switch

Keyword

The keyword for the switch statement is switch .

General Comments

Program control jumps to the statement bearing the value of expression as a label. Program flow then proceeds through the remaining statements unless redirected again. Both expression and labels must have integer values (type char is included), and the labels must be constants or expressions formed solely from constants. If no label matches the expression value, control goes to the statement labeled default , if present. Otherwise, control passes to the next statement following the switch statement. After control goes to a particular label, all the subsequent statements in the switch are executed until the end of the switch , or a break statement, is encountered , whichever comes first.

Form

switch (

expression

)
{
    case

label1 : statement1

case

label2 : statement2

default               :

statement3

}

There can be more than two labeled statements, and the default case is optional.

Examples

switch (value)
    case 1  : find_sum(ar, n);
              break;
    case 2  : show_array(ar, n);
              break;
    case 3  : puts("Goodbye!");
              break;
    default : puts("Invalid choice, try again.");
              break;
}
switch (letter)
{
    case `a' :
    case `e' : printf("%d is a vowel\n", letter);
    case `c' :
    case `n' : printf("%d is in \"cane\"\n", letter);
    default  : printf("Have a nice day.\n");
}

If letter has the value 'a' or 'e' , all three messages are printed; 'c' and 'n' cause the last two to be printed. Other values print only the last message.

I l @ ve RuBoard