17 Oct Decision Making Statements in C
Decision-making statements in C language are the same as in other programming languages such as C++, Java, etc. Decision-making is needed when you want a set of instructions to be executed in a condition and other sets of instructions in another condition.
The following are the decision-making statements in C,
- if statement
- if-else statement
- if-else if-else statement
- switch statement
if Β statement
If the condition is true, the statement gets executed. If the condition is false, it does nothing.
Syntax
The following is the syntax for the if statement in C language,
1 2 3 4 |
if(condition true) statement gets executed; |
The following syntax is for multiple statements,
1 2 3 4 5 6 |
if(condition true) { multiple statement gets executed; multiple statement gets executed; } |
The following is an example showing the usage of the if statement in C language.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> #include<conio.h> void main() { int i = 10; if(i>5) printf(βThe value is more than 5β); getch(); } |
The following is the output, wherein the if statement executes since the value of i is more than 5:
if-else statement
If the condition is true, then a set of statements is executed. If the condition is false, another set of statements gets executed.
Syntax
The following is the syntax for the if-else statements in C language,
1 2 3 4 5 6 |
if(condition true) statement gets executed; else another statement gets executed; |
The following is an example showing the usage of the if-else statement in C language,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include<conio.h> void main() { int i = 2; if(i>5) printf(βThe value is more than 5β); else printf(βThe value is less than 5β); getch(); } |
Hereβs the output,
You can also work on nested if-else. Let us see how we can do it for the preceding example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include <conio.h> void main() { int i = 5; if(i>=5) { if(i==5) { printf("The value is equal to 5"); } else { printf("The value is more than 5"); } } else { printf("The value is less than 5"); } getch(); } |
The following is the output, showing what value gets displayed while using nested if-else,
if-else if-else statement
The if-else if-else statement is useful when the if-statement has more than one condition.
Syntax
The following is the syntax for the else-if statements in C languages,
1 2 3 4 5 6 7 8 |
if(condition true) statement gets executed; else if() statement gets executed; else another statement gets executed; |
The following is an example showing the usage of else-if statement in C languages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include<conio.h> void main() { int i = 20; if(i>100) printf(βThe value is more than 100β); else if(i>75) printf(βThe value is more than 75β); else if(i>50) printf(βThe value is more than 20β); else printf(βThe value is less than 50β); getch(); } |
The following is the output, displaying the result for values less than 50,
Switch Statement
Use the switch statement, if you want to take a decision from more than one choice. For example, grades A, B, C, D, E, etc based on the marks of students.
The following is the syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
switch(expression) { case value1 : break; case value2 : break; case value3 : break; . . . default : } |
The following is an example showing the usage of the switch statement in C language,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> void main () { int marks = 75 ; switch(marks) { case 90: printf("Grade A"); break; case 75: printf("Grade B"); break; case 60: printf("Grade C"); break; } getch(); } |
The following is the output, displaying the result for 75 marks,
In this lesson, we learned how to work with Decision Making Statements in C Programming. We saw how to implement if, if-else, else-if, and switch statements in C.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments