0% found this document useful (0 votes)
0 views21 pages

C Programming Session 8

The document explains the use of if-else statements and switch statements in C programming, detailing their syntax, functionality, and examples. It covers various forms of if statements, including if-else, if-else-if ladder, and nested if, as well as the switch statement and its fall-through behavior. Additionally, it compares the two constructs, highlighting their similarities and differences in terms of execution and expression evaluation.

Uploaded by

abhinavdaksha2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views21 pages

C Programming Session 8

The document explains the use of if-else statements and switch statements in C programming, detailing their syntax, functionality, and examples. It covers various forms of if statements, including if-else, if-else-if ladder, and nested if, as well as the switch statement and its fall-through behavior. Additionally, it compares the two constructs, highlighting their similarities and differences in terms of execution and expression evaluation.

Uploaded by

abhinavdaksha2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

C if else Statement

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.

There are the following variants of if statement in C language.

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

Program to find the largest number of the three.

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

Enter three numbers?


12 23 34
34 is largest
If-else Statement
The if-else statement is used to perform two operations for a single condition. The if-
else statement is an extension to the if statement using which, we can perform two
different operations, i.e., one is for the correctness of that condition, and the other is
for the incorrectness of the condition. Here, we must notice that if and else block
cannot be executed simiulteneously. Using if-else statement is always preferable since
it always invokes an otherwise case with every if condition. The syntax of the if-else
statement is given below.

1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }

Flowchart of the if-else statement in C


Let's see the simple example to check whether a number is even or odd using if-else
statement in C language.

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

Program to check whether a person is eligible to vote or not.

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

Enter your age?18


You are eligible to vote...
Enter your age?13
Sorry ... you can't vote

If else-if ladder Statement


The if-else-if ladder statement is an extension to the if-else statement. It is used in the
scenario where there are multiple cases to be performed for different conditions. In if-
else-if ladder statement, if a condition is true then the statements defined in the if block
will be executed, otherwise if some other condition is true then the statements defined
in the else-if block will be executed, at the last if none of the condition is true then the
statements defined in the else block will be executed. There are multiple else-if blocks
possible. It is similar to the switch case statement where the default is executed instead
of else block if none of the cases is matched.

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. }

Flowchart of else-if ladder statement in C


The example of an if-else-if st

atement in C language is given below.

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

Program to calculate the grade of the student according to the


specified marks.

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

Enter your marks?10


Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...

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.

The syntax of switch statement in c language is given below:

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.

2) The case value must be an integer or character constant.

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;

Valid Switch Invalid Switch Valid Case Inva

switch(x) switch(f) case 3; ca

switch(x>y) switch(x+2.5) case 'a'; ca

switch(a+b-2) case 1+2; ca

switch(func(x,y)) case 'x'>'y'; ca


Flowchart of switch statement in C

Functioning of switch case statement


First, the integer expression specified in the switch statement is evaluated. This value is
then matched one by one with the constant values given in the different cases. If a
match is found, then all the statements specified in that case are executed along with
the all the cases present after that case including the default statement. No two cases
can have similar values. If the matched case contains a break statement, then all the
cases present after that will be skipped, and the control comes out of the switch.
Otherwise, all the cases following the matched case will be executed.
Let's see a simple example of c language switch statement.

#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

Switch case example 2

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

Nested switch case statement


We can use as many switch statement as we want inside a switch statement. Such type
of statements is called nested switch case statements. Consider the following example.

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

the value of i evaluated in outer switch: 10


The value of j evaluated in nested switch: 20
Exact value of i is : 10
Exact value of j is : 20

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.

Syntax of if-else statement is given below:

1. if(expression)
2. {
3. // statements;
4. }
5. else
6. {
7. // statements;
8. }

What is a switch statement?


A switch statement is a conditional statement used in C programming to check the
value of a variable and compare it with all the cases. If the value is matched with any
case, then its corresponding statements will be executed. Each case has some name or
number known as the identifier. The value entered by the user will be compared with all
the cases until the case is found. If the value entered by the user is not matched with
any case, then the default statement will be executed.

Syntax of the switch statement is given below:

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. }

Similarity b/w if-else and switch


Both the if-else and switch are the decision-making statements. Here, decision-making
statements mean that the output of the expression will decide which statements are to
be executed.

Differences b/w if-else and switch statement


The following are the differences between if-else and switch statement are:
o Definition

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

A switch statement can evaluate either an integer or a character.

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

It evaluates a condition to be true or false.


Switch

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

Editing in switch statement is easier as compared to the 'if-else' statement. If we


remove any of the cases from the switch, then it will not interrupt the execution of
other cases. Therefore, we can say that the switch statement is easy to modify and
maintain.

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.

Let's summarize the above differences in a tabular form.

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.

Expression It contains either logical or equality It contains a single expression whi


expression. character or integer variable.
Evaluation It evaluates all types of data, such as It evaluates either an integer, or c
integer, floating-point, character or
Boolean.

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.

Speed If there are multiple choices If we have multiple choices then th


implemented through 'if-else', then is the best option as the speed of t
the speed of the execution will be be much higher than 'if-else'.
slow.

You might also like