Conditional Statement - if..else Statement in C

In this tutorial you will learn about the  Conditional Statements or if else Statement in C which include if statement , if -else statement and nested if else of C programming Language .

In C programming language we can executed the program sequentially until there is no condition around the statements .



If in the program execution where we can use different type of statements to process our C program further .

if you are interested in knowing more about Elements of C then read this post "Elements of C"

The  different types of statement available to process our program are :-


  • Conditional Statements
  • Unconditional Statements
  • Jumping Statements
  • Looping Statements



Conditional Statements in C


These are the types of the statements where we use conditions to process the program using some relational operators to produce true or false value .

In C programming language conditional statements can be executed with the help of following two methods

if statements

if Else statements


If Statements / simple If

Here we able to specify only one condition and one print statement.

 The demerit of this statement is it can't go to the default part .

Syntax for If statements

  1. if (test expression)
  2. {
  3. // statements to be executed if the test expression is true
  4. }


#include <stdio.h>
#include <conio.h>
void main()
{
int a; 
clrscr();
printf(“enter a number\n”); 
scanf(“%d”,&a); 
if(a%2==0)
printf(“number is even”); 
getch();
}


If else Statements


We are able to specify only one condition but two print statement means one for true  part and another false part .

Syntax for if-else statements

  1. if (test expression) {
  2. // statements to be executed if the test expression is true
  3. }
  4. else {
  5. // statements to be executed if the test expression is false
  6. }


#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
 
clrscr();
printf(“enter two number\n”); scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is greater”); 
else
printf(“b is greater”); 
getch();
}




Nested If


Here we are able to specify n number of conditions and n number of print statements .

But the problem here is is it very complex type of statement and you will get confuse about starting and ending of if block because of nested structure.

Syntax for nested if-else


if(condition1)
if(condition2)
print statement1; 
else
print statement2;
.
.
else if(conditionn)
print statement-1;
 else
print statementn;




#include <stdio.h>
#include <conio.h>
void main()
{
int y;
clrscr();
printf(“enter a year\n”); 
scanf(“%d”,&y); 
if(y%100!=0)  
if(y%4==0) 
printf(“leap year”); 
else
printf(“not”); 
else if(y%400==0)
printf(“leap year”); 
else
printf(“not”); 
getch();
}




#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter three integer number\n”); 
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
if(a>c)
printf(“a is greatest”);
 else
printf(“c is greatest”); 
else
if(b>c)
printf(“b is greatest”); 
else
printf(“c is greatest”);
 getch();
}

Ladder else-if


It is the type of conditional statements where we can specify n number of conditions and n number of print statements .

It is very simple as compare to nested if because here  no nesting of condition is there and we can specify one condition and one print statement and so on .



if(condition1)
print statement1; 
else if(condition2) 
print statement2;
.
.
else
print statement;


Example -1 If -else ladder 


  1. // Program to relate two integers using =, > or < symbol
  2. #include
  3. int main() {
  4. int number1, number2;
  5. printf("Enter two integers: ");
  6. scanf("%d %d", &number1, &number2);
  7. //checks if the two integers are equal.
  8. if(number1 == number2) {
  9. printf("Result: %d = %d",number1,number2);
  10. }
  11. //checks if number1 is greater than number2.
  12. else if (number1 > number2) {
  13. printf("Result: %d > %d", number1, number2);
  14. }
  15. //checks if both test expressions are false
  16. else {
  17. printf("Result: %d < %d",number1, number2);
  18. }
  19. return 0;
  20. }




To know about the best compiler for C read this post "Top Ide for C and C++ Developers - 2019".


If you think anything written above is inappropriate or want to add more information about the above topic then comment down below.








Conditional Statement - if..else Statement in C Conditional Statement - if..else Statement in C Reviewed by Digital Roy on January 11, 2020 Rating: 5

No comments:

Powered by Blogger.