Decision Making Statements in C Programming
Introduction:-
Decision making statements allow a program to take decisions based on conditions.
They help control the flow of execution in a program.
______________________________
*In C programming, decision making is done using:
- if statement
- if-else statement
- else-if ladder
- switch statement
--------------------------------------------------
1. if Statement
The if statement executes a block of code only when the condition is true.
Syntax:
if(condition) {
// code to execute
}
Example:
#include <stdio.h>
int main() {
int age = 18;
if(age >= 18) {
printf("You can vote");
}
return 0;
}
--------------------------------------------------
2. if-else Statement
The if-else statement executes one block if condition is true, otherwise another block.
Syntax:
if(condition) {
// true block
}
else {
// false block
}
Example:
#include <stdio.h>
int main() {
int num = 10;
if(num % 2 == 0)
printf("Even Number");
else
printf("Odd Number");
return 0;
}
--------------------------------------------------
3. else-if Ladder
Used to check multiple conditions.
Syntax:
if(condition1) {
}
else if(condition2) {
}
else {
}
Example:
#include <stdio.h>
int main() {
int marks = 85;
if(marks >= 90)
printf("Grade A");
else if(marks >= 70)
printf("Grade B");
else
printf("Grade C");
return 0;
}
--------------------------------------------------
4. switch Statement
Used to select one option from many choices.
Syntax:
switch(variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example:
#include <stdio.h>
int main() {
int day = 1;
switch(day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Invalid Day");
}
return 0;
}
--------------------------------------------------
Why Decision Making is Important
- Controls program flow
- Helps build logic
- Used in real-world applications
- Makes programs intelligent
--------------------------------------------------
Conclusion
Decision making statements help programs choose actions based on conditions.
After understanding this topic, you can learn loops in C programming.
--------------------------------------------------
Comments
Post a Comment