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 :-
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
The demerit of this statement is it can't go to the default part .
Syntax for If 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
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
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 .
Example -1 If -else ladder
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.
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
if (test expression)
{
// statements to be executed if the test expression is true
}
#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
if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}
#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
// Program to relate two integers using =, > or < symbol
#include
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
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
Reviewed by Digital Roy
on
January 11, 2020
Rating:
No comments: