If
statement in C
As we saw in our previous post that if statement falls
under the decision making statements in C.
if is a simple decision making statement.
As a human
begin we all take decisions in our daily life.
A simple decision making example in our daily life
is as shown below,
“ If you feel thirsty, then drink water.”
The above statement is an example of simple if
statement.
In the above you will only drink water when you feel
thirsty.
In C the syntax of if statement is as follows,
if(condition)
statement;
That is if the condition is true then the next statement after if statement is executed ,if false then the next statement to if is
skipped.
comparing it with the above example statement, we can apply it as follows,
if(thirsty)
drink water;
If we need to execute multiple statements when the
condition is true then we need to follow the syntax as follows,
if(condition)
{
Statement1;
Statement2;
…..
}
we can also put braces if there is a single statement, the braces are used to indicate that this block belong to if statement. It is executed only if the if condition is true(satisfied).
Example 1:
Output :
In the above example program you can see that the if condition is not true(that means false '0'). since a is 6 and not 5 so the if block is skipped and the next statement after if block is executed.(i.e,printing "i dont know").