C Decision Making: if, switch
#include <stdio.h>
int main() {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
2024 is a leap year
switch Statement
#include <stdio.h>
int main() {
char op = '*';
int a = 6, b = 3;
switch (op) {
case '+': printf("%d\n", a + b); break;
case '-': printf("%d\n", a - b); break;
case '*': printf("%d\n", a * b); break;
case '/': printf("%d\n", a / b); break;
default: printf("Invalid operator\n");
}
return 0;
}
18
Rule
switch works only with integer and character constants, not with floats or strings.Track your progress through the C course