Summary: Using if Statements for Making Choices

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


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net