C uses the keyword
The syntax of an
The general form of
The condition following the keyword
Simplified forms:
Note that in C a non-zero value is considered to be true, whereas a 0 is considered to be false. The default scope of
if
to implement the decision control instruction. An if
statement consists of a Boolean expression followed by one or more statements. The syntax of an
if
statement is,
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
The general form of
if
statement looks like this:
if ( this condition is true )
{
execute statement 1 ;
execute statement 2 ;
…
execute statement n ;
}
The condition following the keyword
if
is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it. We express a condition using C’s ‘relational’ operators. An expression can also be used as condition. Simplified forms:
if ( this condition is true )
execute this statement ;
if ( this expression evaluates to true )
execute this statement ;
Note that in C a non-zero value is considered to be true, whereas a 0 is considered to be false. The default scope of
if
is the statement immediately after the if
. To override this default scope a pair of braces as shown above.
/* Demonstration of if statement */
#include <stdio .h>
int main( )
{
int num ;
printf ( "\nEnter a number less than 10: " ) ;
scanf ( "%d", &num ) ;
if ( num < 10 )
printf ( "\nThanks! I got your number %d", num) ;
if ( num )
printf ( "\n The number %d is non-zero", num) ;
if ( num == 0 )
printf ( "\n The number is zero" ) ;
if ( num >= 10 )
printf ( "\n The number %d is not less than 10", num ) ;
if ( 3+3/6 )
printf ( "\n The expression 3+3/5 evaluates to non-zero value") ;
if ( (3+3-6) == 0)
printf ( "\n The condition 3+3-6 evaluates to zero") ;
}
Output of above program,
Enter a number less than 10: 3
Thanks! I got your number 3
The number 3 is non-zero
The expression 3+3/5 evaluates to non-zero value
The condition 3+3-6 evaluates to zero
Related topics:
Overview of Statements in C | Decision Making Statements in C | The if-else Statement in C | The else-if Statement in C | Nested if-else Statement in C | Forms of if Statement in C | Switch Statement in C | Conditional Operator Statement in C | The null Statement in C
List of topics: C Programming
No comments:
Post a Comment