C Programming Session 8
C Programming Session 8
The if-else statement in C is used to perform the operations based on some specific
condition. The operations specified in if block are executed if and only if the given
condition is true.
o If statement
o If-else statement
o If else-if ladder
o Nested if
If Statement
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario
where we need to perform the different operations for the different conditions. The
syntax of the if statement is given below.
1. if(expression){
2. //code to be executed
3. }
Flowchart of if statement in C
Let's see a simple example of C language if statement.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("Enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. return 0;
10. }
Output
Enter a number:4
4 is even number
enter a number:5
1. #include <stdio.h>
2. int main()
3. {
4. int a, b, c;
5. printf("Enter three numbers?");
6. scanf("%d %d %d",&a,&b,&c);
7. if(a>b && a>c)
8. {
9. printf("%d is largest",a);
10. }
11. if(b>a && b > c)
12. {
13. printf("%d is largest",b);
14. }
15. if(c>a && c>b)
16. {
17. printf("%d is largest",c);
18. }
19. if(a == b && a == c)
20. {
21. printf("All are equal");
22. }
23. }
Output
1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. else{
10. printf("%d is odd number",number);
11. }
12. return 0;
13. }
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number
1. #include <stdio.h>
2. int main()
3. {
4. int age;
5. printf("Enter your age?");
6. scanf("%d",&age);
7. if(age>=18)
8. {
9. printf("You are eligible to vote...");
10. }
11. else
12. {
13. printf("Sorry ... you can't vote");
14. }
15. }
Output
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number==10){
7. printf("number is equals to 10");
8. }
9. else if(number==50){
10. printf("number is equal to 50");
11. }
12. else if(number==100){
13. printf("number is equal to 100");
14. }
15. else{
16. printf("number is not equal to 10, 50 or 100");
17. }
18. return 0;
19. }
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
1. #include <stdio.h>
2. int main()
3. {
4. printf("Congrats ! you scored grade A ...");
5. }
6. else if (marks > 60 && marks <= 85)
7. {
8. printf("You scored grade B + ...");
9. }
10. else if (marks > 40 && marks <= 60)
11. {
12. printf("You scored grade B ...");
13. }
14. else if (marks > 30 && marks <= 40)
15. {
16. printf("You scored grade C ...");
17. }
18. else
19. {
20. printf("Sorry you are fail ...");
21. }
22. }
Output
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us
to execute multiple operations for the different possibles values of a single variable
called switch variable. Here, We can define various statements in the multiple cases for
the different values of a single variable.
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }
Rules for switch statement in C language
1) The switch expression must be of an integer or character type.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break
statement found in the case, all the cases will be executed present after the matched
case. It is known as fall through the state of C switch statement.
Let's try to understand it by the examples. We are assuming that there are following
variables.
1. int x,y,z;
2. char a,b;
3. float f;
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
1. #include <stdio.h>
2. int main()
3. {
4. int x = 10, y = 5;
5. switch(x>y && x+y>0)
6. {
7. case 1:
8. printf("hi");
9. break;
10. case 0:
11. printf("bye");
12. break;
13. default:
14. printf(" Hello bye ");
15. }
16.
17. }
Output
hi
C Switch statement is fall-through
In C language, the switch statement is fall through; it means if you don't use a break
statement in the switch case, all the cases after the matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given
below.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4.
5. printf("enter a number:");
6. scanf("%d",&number);
7.
8. switch(number){
9. case 10:
10. printf("number is equal to 10\n");
11. case 50:
12. printf("number is equal to 50\n");
13. case 100:
14. printf("number is equal to 100\n");
15. default:
16. printf("number is not equal to 10, 50 or 100");
17. }
18. return 0;
19. }
Output
enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Output
enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
1. #include <stdio.h>
2. int main () {
3.
4. int i = 10;
5. int j = 20;
6.
7. switch(i) {
8.
9. case 10:
10. printf("the value of i evaluated in outer switch: %d\n",i);
11. case 20:
12. switch(j) {
13. case 20:
14. printf("The value of j evaluated in nested switch: %d\n",j);
15. }
16. }
17.
18. printf("Exact value of i is : %d\n", i );
19. printf("Exact value of j is : %d\n", j );
20.
21. return 0;
22. }
Output
if-else vs switch
What is an if-else statement?
An if-else statement in C programming is a conditional statement that executes a
different set of statements based on the condition that is true or false. The 'if' block will
be executed only when the specified condition is true, and if the specified condition is
false, then the else block will be executed.
1. if(expression)
2. {
3. // statements;
4. }
5. else
6. {
7. // statements;
8. }
1. switch(expression)
2. {
3. case constant 1:
4. // statements;
5. break;
6. case constant 2:
7. // statements;
8. break;
9. case constant n:
10. // statements;
11. break;
12. default:
13. // statements;
14. }
if-else
Based on the result of the expression in the 'if-else' statement, the block of statements
will be executed. If the condition is true, then the 'if' block will be executed otherwise
'else' block will execute.
Switch statement
The switch statement contains multiple cases or choices. The user will decide the case,
which is to execute.
o Expression
If-else
It can contain a single expression or multiple expressions for multiple choices. In this,
an expression is evaluated based on the range of values or conditions. It checks both
equality and logical expressions.
Switch
It contains only a single expression, and this expression is either a single integer object
or a string object. It checks only equality expression.
o Evaluation
If-else
An if-else statement can evaluate almost all the types of data such as integer, floating-
point, character, pointer, or Boolean.
Switch
o Sequence of Execution
If-else
In the case of 'if-else' statement, either the 'if' block or the 'else' block will be executed
based on the condition.
Switch
In the case of the 'switch' statement, one case after another will be executed until
the break keyword is not found, or the default statement is executed.
o Default Execution
If-else
If the condition is not true within the 'if' statement, then by default, the else block
statements will be executed.
Switch
If the expression specified within the switch statement is not matched with any of the
cases, then the default statement, if defined, will be executed.
o Values
If-else
Values are based on the condition specified inside the 'if' statement. The value will
decide either the 'if' or 'else' block is to be executed.
Switch
In this case, value is decided by the user. Based on the choice of the user, the case will
be executed.
o Use
If-else
A switch statement compares the value of the variable with multiple cases. If the value
is matched with any of the cases, then the block of statements associated with this case
will be executed.
o Editing
If-else
Editing in 'if-else' statement is not easy as if we remove the 'else' statement, then it will
create the havoc.
Switch
o Speed
If-else
If the choices are multiple, then the speed of the execution of 'if-else' statements is
slow.
Switch
The case constants in the switch statement create a jump table at the compile time.
This jump table chooses the path of the execution based on the value of the expression.
If we have a multiple choice, then the execution of the switch statement will be much
faster than the equivalent logic of 'if-else' statement.
If-else switch
Definition Depending on the condition in the 'if' The user will decide which stateme
statement, 'if' and 'else' blocks are executed.
executed.
Sequence of First, the condition is checked. If the It executes one case after another
execution condition is true then 'if' block is keyword is not found, or the defau
executed otherwise 'else' block executed.
Default If the condition is not true, then by If the value does not match with a
execution default, else block will be executed. default, default statement is execu
Editing Editing is not easy in the 'if-else' Cases in a switch statement are ea
statement. modify. Therefore, we can say tha
editing of any case will not interrup
other cases.