Introduction
The if statement is the first selection structure. if is used when a question requires a yes or no answer. If you want to choose an answer from several possibilities then use the switch statement.
Program/Example
The general format for an if statement is:
if ( condition ) simple or compound statement.
Following are the properties of an if statement:
-
If the condition is true then the simple or compound statements are executed.
-
If the condition is false it does not do anything.
-
The condition is given in parentheses and must be evaluated as true (nonzero value) or false (zero value).
-
If a compound statement is provided, it must be enclosed in opening and closing braces.
Following are the test conditions:
(7)// a non-zero value returns True. (0)// zero value returns False. (i==0) // True if i=0 otherwise False. (i = 0) // False because value of the expression is zero.
No comments:
Post a Comment