- If Condition Statement
- If-Else Condition Statement
- If Else-If Condition Statement
- Nested If-Else Condition Statement
- Example: program to find the remarks based on marks and grade
- Switch-Case Statement in C
- Syntax Of Switch-Case with Break Condition in C
- Nested Switch-Case in C
- Advantages of Switch-Case
In C programming language, the statements are executed sequentially in the order they appear in the program. But sometimes in some cases, the programmer wants to apply certain conditions to execute some parts of the program and many cases arise that the programmer may want to execute a part of the program several times. Keeping in view of those situations control statement is given much importance in a programming language which enables the programmer to mention the sequence in which the program is to be executed. Mostly the Control Statement in C is a very powerful logic that can be used in most of the programming.
These Control statements are basically mostly used in 90% of programming. Among them, Operating System programming, Boot Loader programming, device driver programming, etc.
Most importantly, with the help of it the programmer determines the flow of program in such a way so that it executes certain statements based on the condition applied. Among all of them, we will discuss the If-Else control statement here. Let’s go start without wasting time.
If Condition Statement
It comes into action when there is a situation about applying condition in the program in order to execute proper output. So, this statement helps the programmer to take decisions according to the condition and execute the block of statements of if-condition unless the condition is false. If it is false, the block of statements of if-condition is skipped and the remaining statements are executed in descending order.
Syntax of If Condition
if(test condition)
{
statement1;
Statement2;
}
Statement-x;
Here, if the condition tested is true, the block of statements associated with if-condition is executed sequentially. Otherwise, the compiler skips those blocks of statements and jumps to the statement-x. Most importantly, when the condition is true both block of statements are executed.
If Condition Example program: To calculate the total income of a pension holder per month
#include<stdio.h>
Int main()
{
int other_income,pension,age;
Int total_income;
printf(“Enter the age\n”);
if(age>=60)
{
pension=6000;
}
Other_income=4000;
Total_income=pension+other_income;
Printf(“total income of the person is %d”,total_income);
}
Basically, the above example says about the total income of a person in a month based on the age category. If the person’s age is above or equal to 60, he/she gets pension and his/her total income is calculated by taking pension and other income together. If he is under 60, total income is calculated by taking only other income. Here, his age is 60, he will get pension of Rs. 6000 and his total income will be (pension + other income) Rs.10000 per month.
If-Else Condition Statement
It is a two-way conditional control statement in C. This statement is applied in the program when there is a condition to test and accordingly take one of two possible actions. If the condition is true, then a single or block of statements is executed. Otherwise, another single or block of statements is executed. In C-programming, any nonzero value is regarded as true while zero value is regarded as false.
If-Else Syntax
If(condition)
{
Statement1;
}
{
else
{
Statement2;
}
Example: Print a message if a number is less than 100 or not
#include<stdio.h>
Int main()
{
int n;
printf (“Enter a number\n”);
scanf(“%d”,&n);
if(n<100)
printf(“it is less than 100”};
else
printf(“it is not less than 100”};
}
Output
Enter a number
45
It is less than 100
Here, in the above example a number 45 is entered and it is tested at condition, the result of which is true, so the first statement is executed. Otherwise, the else statement would have been executed.
If Else-If Condition Statement
This is the most suitable way of putting the if-conditions together when multi-path decisions are associated. In this statement, the conditions are checked from top to down. When a condition is found true, the single or block of statements associated to that condition is executed. The rest are simply skipped. If no condition is found true, the default else statement is executed at the end. Moreover, if there is no default else statement and no condition is found true, then the compiler will not act upon the patch of the statements in the program.
Syntax of If-Else-If Condition
Syntax:
if(condition1)
{
statementA;
}
else if(condition2)
{
statementB;
}
else if(condition3)
{
statementc;
}
else
{
statementD;
}
Example of If-else-if: Write a program to find the color based on the code of a colour
#include<stdio.h>
int main()
{
int code;
printf (“Enter code between 1 and 4\n”);
if(code==1)
{
printf(“red\n”);
}
else if(code==2)
{
printf(“green\n”);
}
else if(code==3)
{
printf(“white\n”);
}
else
{
printf(“yellow\n”);
}
}
Output
Enter the code between 1 and 4
2
Green
In the above example, the user entered code no-2. The compiler checks the first condition which is false then goes to the second condition which is true and execute statement associated with the condition.
Nested If-Else Condition Statement
It means there is another if…else statement inside the if block or else block. It is sometimes very confusing to understand. But it is very easy when it is understood properly. Here, if the outer if-condition is true then the compiler would enter the if-block statement. The compiler tests the inner if-condition which is there inside the outer if-condition and accordingly execute the statement. If the outer if-condition is false, then the compiler would enter the else block test the if-condition if it is there. Otherwise, simply execute the statement in else block.
Syntax of Nested If-Else
If(condition1) //outer if-condition
{
if(condition2) // inner if-condition
{
Statement1;
}
else
{
Statement2;
}
}
else
{
if(condition3) //outer if-condition
{
Statement3;
}
else
{
Statement4;
}
}
Example: program to find the remarks based on marks and grade
#include<stdio.h>
Int main()
{
int month;
Printf(“Enter the month\n”);
Scanf(“%d”,&month);
If(month==12||month==1||month==2)
{
Printf(“this is winter season\n”);
}
else if(month==3||month==4||month==5)
{
Printf(“This is the summer season”);
}
else if(month==6||month==7||month==8)
{
Printf(“this is rainy season\n”);
}
else if(month==9||month==10||month==11)
{
Printf(” this is autumn season\n”);
}
else
{
Printf(“bogus month”);
}
}
Output
Enter the month
4
This is the summer season.
In the above example, the compiler checks the first condition which is false, then goes to the second condition which is true and executes the statement.
Switch-Case Statement in C
It is a multi-directional conditional control statement. The programmer uses switch case statement when there is a choice to be made among the number of cases based on the expression input. This is highly advanced version of if- statement in programming languages.
This statement is useful because we use the if-statement to control the selection. However, the complexity level increases when the number of options increases. The whole program becomes confusing for the programmer.
Another point is that the switch case is more speed in execution than the if-else, whenever there is a number of nested if is more. because the if-else condition checks every condition from top to bottom until it matches the condition where as the switch-case directly jumps to that particular condition, executes it and breaks the loop.
So, switch case statement overcomes those problems and provides a better solution to the programmer.
Syntax of Switch-Case Statement
switch(Variable Value)
{
case constant1:
statement;
case constant2:
statement;
case constant3:
statement;
default:
statement;
}
In the above syntax, the switch() holds a variable value. This value can be any one of case condition that holds a particular constant value. If any one one of the case condition constant value will match with that switch() variable value, it will enter to that case condition and execute only that piece of code.
Example Of Switch-Case Condition in C
Let’s have a simple example to understand the working of switch case statement.
#include<stdio.h>
int main()
{
int p=3;
switch(p+1)
{
case 1:
{
printf("one\n");
}
case 2:
{
printf("two\n");
}
case 3: printf("three\n");
{
case 4: printf("four\n");
}
default:
{
printf("no number matched\n");
}
}
}
output
four
no number matched
Here, the output is “four” and “no number matched” which is not correct result as per the input given as expression. The correct answer is “four” only not the default value “no number matched”. In order to overcome the problem related to above, Break statement is introduced in switch case statement.
Break Statement in Switch-Case
This statement is generally used to come out of the body skipping the statements after the control meets the intended output. In switch case also same logic is applied when the expression and the case is matched, the control goes inside that case and executes the statement. When the break statement is encountered, it simply skips the remaining cases and comes out of the switch body.
Syntax Of Switch-Case with Break Condition in C
switch(expression)
{
case constant1:
{
statement;
break;
}
case constant2:
{
statement;
break;
}
default:
{
statement;
break;
}
}
Switch-Case With Break Statement Example Code
#include<stdio.h>
int main()
{
int p=3;
switch(p+1)
{
case 1:
{
printf("one");
break;
}
case 2:
{
printf("two");
break;
}
case 3:
{
printf("three");
break;
}
case 4:
{
printf("four");
break;
}
default:
{
printf("no number matched");
break;
}
}
return 0;
}
Output
Four
Here, the expression is matched with the case 3. So, the control goes inside that case and execute the statement. After execution, the control comes out of the switch body.
Nested Switch-Case in C
switch()
{
case constant1:
{
statement;
break;
}
case constant2:
{
switch(expression)
{
case constant1:
{
statement;
break;
}
case constant2:
{
statement;
break;
}
default:
{
statement;
break;
}
}
}
case constant3:
{
statement;
break;
}
default:
{
statement;
break;
}
}
You can in the above, how multiple switch cases can be nested. I hope you will clear most of the doubts from this article. If still, you are thinking to add here please comment below. If possible please send us your additional improvement topic to add it here. You can send your article at piembsystech@gmail.com with your photo to see your talent at PiEmbSysTech.
Advantages of Switch-Case
- Cases can have multiple orders.
- In the switch case, all characters and expressions can be used.
- We can use characters in switch statements because characters are converted to their respective ASCII value.
- We can not use floating-point and string constants.
- Multiple constants in a single case are not allowed.
- There can be nesting of switch statement inside another switch case.
This Article Written by Pitabas Choudhury. He is currently pursuing his MCA in 1st year. He is from Rajdhani Engineering College (REC), Bhubaneswar, Odisha. He has done a lot of research and analysis on C programming to understand the base of it.