Lecture 4 - C Conditional Statement - IF - IF Else and Nested IF Else With Example
Lecture 4 - C Conditional Statement - IF - IF Else and Nested IF Else With Example
2. If-else statement
It is also called as branching as a program decides
which statement to execute based on the result of
the evaluated condition
In this tutorial, you will learn-
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
1. In the given program we have explain initialized a
variable num with value 8.
2. A switch construct is used to compare the value
stored in variable num and execute the block of
statements associated with the matched case.
3. In this program, since the value stored in variable
num is eight, a switch will execute the case whose
case-label is 8. After executing the case, the control
will fall out of the switch and program will be
terminated with the successful result by printing
the value on the output screen.
Try changing the value of variable num and notice
the change in the output.
For example, we consider the following program
which defaults:
#include <stdio.h>
int main()
{
int language = 10;
switch (language) {
case 1:
printf("C#\n");
break;
case 2:
printf("C\n");
break;
case 3:
printf("C++\n");
break;
default:
printf("Other programming language\n");
}
}
When working with switch case in C, you group
multiple cases with unique labels. You need to
introduce a break statement in each case to branch
at the end of a switch statement.
THANK YOU
FOR COMING!