C if Statement
A if statement if statement consists of a Boolean expression followed by one or more statements.
Syntax
In the C language if statement syntax:
if(boolean_expression)
{
/* statement to execute if the boolean expression is true */
}
If the Boolean expression is true, then the code block inside the if statement is executed. If the Boolean expression is false, then the first set of code after the if statement ends (after the closing brace) is executed.
C treats anynon-zeroandnon-nullvalue as true, and treatszeroor null as false.
Flowchart
Example
Example
#include
<
stdio.h
int
main
(
)
{
/*Local variable definition
*/
int
a
=
10
;
/*
Use the if statement to check a Boolean condition
*/
if
(
a
<
20
)
{
/*If the condition is true, the following statement is output
*/
printf
(
"
a is less than 20
\
n
"
)
;
}
printf
(
"
The value of a is %d
\
n
"
,
a
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
a 小于 20
a 的值是 10
C Judgment