The if-else Statement in C Programming Language



Here, if and else are the keywords in C.
Syntax:
if (condition is true){
/*1st block of statements*/
}
else{
/*2nd block of statements*/

}
The keyword if tells the compiler that what follows is decision control statement. The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if-else statement using an example.

Example:

int a = 10;
if (a == 10){
printf(“You are in 1st block\n”);
printf(“The value of a is %d\n”, a);
}
else{
printf(“You are in 2nd block\n”);
printf(“The value of a is not equal to %d\n”, a);
}

In the above example we have assigned value 10 to a. In the next line, there is an if statement. It is read as “If a is equal to 10 then perform the block of statements enclosed within the braces of if part else perform the block of statements enclosed within the braces of else part”. After the execution of if-else statement normal sequence flow of the program is followed.
The output of the above example is:
You are in if block
You are in 1st block
The value of a is 10


The else-if statement


The else-if statement is nothing but the rearrangement of else with the if that follows.
Consider the below program.
if(condition)
perform this
else{
if(condition)
perform this;
}


The above program can be re-written as,
if(condition)
perform this
else if(condition)
perform this;

Here, the else-if statement is used. Else if reduces the complexity of the program making it easier to understand.


Responses

0 Respones to "The if-else Statement in C Programming Language"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy