User:Yiorgos Bagakis/prototyping/if statements

From XPUB & Lens-Based wiki
< User:Yiorgos Bagakis
Revision as of 00:53, 15 November 2010 by Yiorgos Bagakis (talk | contribs) (Created page with "IF STATEMENTS: > source: http://www.cprogramming.com/tutorial/lesson2.html > Conditional statements are very useful for controlling the flow of programs -> what section of a c...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

IF STATEMENTS:

> source:

http://www.cprogramming.com/tutorial/lesson2.html

> Conditional statements are very useful for controlling the flow of programs -> what section of a code should be executed?

> In this way depending on a users input the program executes a certain action if the given conditions are true or false

> A TRUE statement evaluates to a nonzero number

> A FALSE statement evaluates to zero

> If the comparison is TRUE the operator will return 1

> If the comparison is FALSE the operator will return 0

> Conditional operators:

> greater than 2 > 1 is TRUE < less than 1 < 2 is TRUE >= greater than or equal 2 >= 2 is TRUE <= less than or equal 1 <= 2 is TRUE == equal to 2 == 2 is TRUE != not equal to 2 != 1 is TRUE

> Basic syntax:

The structure of an if statement is as follows:


>IF:

if ( TRUE ) {

 Execute all statements inside the braces

}


>ELSE:

if ( TRUE ) {

 // Execute these statements if TRUE

} else {

 // Execute these statements if FALSE

}


>ELSE IF:

if ( <condition> ) {

 // Execute these statements if <condition> is TRUE

} else if ( <another condition> ) {

 // Execute these statements if <another condition> is TRUE and
 // <condition> is FALSE

}