If statement in C


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:
if statement in c

 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").


 Example 2:
if statement

Output:
In the above program you can see that the if condition is true(that means '1'). so the if block is executed "a equal 5" is printed first then after if block "i dont know" is printed.



Thank you ...

Please Show your support by subscribing below..



Previous Post
Next Post
Related Posts

0 comments:

Popular Posts