Code Point Blog
Iam presenting here ,all type of knowledge if you want to interest knowing about any topic then click below thumbnail view button and knowing.
In C, decision-making is typically achieved using conditional statements, such as:
1.if statement :The 'if' statement allows you to execute a block of code if a specified condition is true.
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
}
return 0;
}
2.if-else statementThe 'if-else' statement allows you to execute one block of code if a condition is true and another block of code if the condition is false.
#include <stdio.h>
int main() {
int num = -5;
if (num > 0) {
printf("The number is positive.\n");
} else {
printf("The number is non-positive.\n");
}
return 0;
}
3.if-else if-else statement :The 'if-else if-else' statement allows you to check multiple conditions and execute different blocks of code accordingly.
#include <stdio.h>
int main() {
int num = 0;
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0){
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
4.switch statement :The 'switch' statement allows you to choose among multiple cases based on the value of an expression
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
printf("Average.\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}