Subscribe Us

What is IF- ELSE Statement in C programming Language ?

  IF- ELSE Statement :

Many control statements are in the C programming language. You think... 

what is if else in c? 

=> If-else statement is a basic control flow structure of the C programming language.  If-else statement is used when a unit of code needs to be executed by any particular condition which is either true or false. If the condition is true, then If statement work on the code and the if block will execute otherwise Else block will execute. The if-else statement syntax is simple to see below

if (condition) // condition true

{

/* code will execute */

}

else // condition faluse 

{

/* code will execute */

}

Some Questions People Asked Most :

1. Are if-else Statements Loops / is if-else a loop?
Ans: No, if-else is not a loop. It is a control statement of the C programming language.
2.Are if-else statements bad or slow? 
Ans. Yes, it is slower than the switch statement. 


Upper code is for one condition, Several times you need to execute many conditions at a time, then you can't use only an if-else statement. you have to use an else-if function . I will try to discuss in detail to understand.

if(condition - 1 ) 

{ /* condition - 1 true code will execute */ }

else - if (condition -2)

{ /* condition - 2 true code will execute */ }

else - if (condition -3)

{ /* condition - 3 true code will execute */ }

else

 { /*all the condition become false then this block will execute */ }

Let's understand better through a real example. 

Suppose, you have to find the greater number between 2 numbers?

Here, 3 cases will happen 

1. maybe two numbers are equal

2. first number greater 

3. the second number is greater

let's go to the program :

if (x<y)

{ printf("y is greater than x"); }

else if (x > y)

{ printf("x is greater than y"); }

else 

{ printf (" two numbers are equal ");}


Post a Comment

0 Comments