If-else block in C

In this article I am going to explain about if-else statement.what is the use of if-else block?
why we use if-else block frequently in any program?


IF-ELSE STATEMENT


Mainly, if block is used to control the flow of statements, to execute only certain statements under the given condition we use if-else block. see the following example for a better understanding


 As you see in the above example if the variable 'a' is greater than or equal to 3 then the statements which are written inside if block will be executed or if the above condition is failed then else block will be executed so the output will be "your no is less than 3"

So based on the condition the execution of statements will occur. if the condition written inside if block is correct then statements which are written inside the if block will be executed and if condition failed else block will be executed

ELSE IF STATEMENT 

Instead of using an if-else statement, we can use an else-if block if we are going to use multiple conditions rather than using a single condition in the if-else block.

As you see in the above example variable 'c' is greater than 10 statements written inside if block will be executed and if c < 10 and c > 8 then else if block will be executed, if above two conditions failed then else block will be executed. from the above example, you have understood that else if block is used if you have multiple conditions. if you want to use a single condition you can go with if-else block

you will learn about Nested if-else block later, as it is an advanced topic in control statements. Now I think you have got some grip on Control statements.

Post a comment

0 Comments