Programming in C Conditional statements.pdf

rswbec 0 views 44 slides May 19, 2025
Slide 1
Slide 1 of 44
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44

About This Presentation

C Conditional Statements


Slide Content

Control Statements
in C

Conditional statements
•Are those statements which will depend
on condition for their execution.
Types of Conditional Statements
•If condition
•if-else condition
•Nested if-else
•If-else ladder

if condition
if(test condition)
{
statement1;
statement2;
..........
}
Statement1 and statement2 are conditional statements as
they depend on condition for their executing.

WAP to find greatest of 2
numbers using if
condition
•lvalue : An expression that is
an lvalue may appear as either
the left-hand or right-hand side
of an assignment.

Output
Hello

if-else condition
•if statement evaluates to true. It does nothing
when the expression evaluates to false.
•else statement is used when we want some
other statements to be executed when if
evaluates to be false.
•So if the condition is true, if is executed else,
else is executed.

if(test condition)
{
Statement1;
Statement2;
}
else
{
Statement3;
Statement4;
}

Nested if-else
•if-else construct within either the body of the if
statement or the body of an else statement or both.
This is called nesting of if’s.

•WCAP to find greatest of 3 numbers using nested if-else.
#include <stdio.h>
int main() {
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
else {
if (n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
return 0;
}

if-else Ladder
•It is used to check multiple conditions.
•Every else is associated with its previous if.
•The last else goes to work only if all the
conditions fail.
•Even in else if ladder the last else is optional.

•WACP to find the roots of a quadratic equation of the form
ax
2
+bx+c=0.
•The term (b
2
- 4ac) is known as the discriminant of a quadratic equation.

#include <math.h>
#include <stdio.h>
int main() {
float a, b, c, disc, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: \n");
scanf("%f %f %f", &a, &b, &c);
disc = b * b - 4 * a * c;
// condition for real and different roots
if (disc > 0)
{
root1 = (-b + sqrt(disc)) / (2 * a);
root2 = (-b - sqrt(disc)) / (2 * a);
printf("root1 = %0.2f and root2 = %0.2f", root1, root2);
}

// condition for real and equal roots
else if (disc == 0)
{
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %0.2f", root1);
}
// if roots are imaginary
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-disc) / (2 * a);
printf("Root1 = %0.2f + i%0.2f and Root2 = %0.2f-i
%0.2f", realPart, imagPart, realPart, imagPart);
}
return 0;
}

•WACP to find the larget among the three numbers using if else
ladder.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("The largest number is: %d", a);
} else if (b >= a && b >= c) {
printf("The largest number is: %d", b);
} else {
printf("The largest number is: %d", c);
}
return 0;
}

•WACP to calculate tax for entered salary.
•If salary > 10000, tax is 2%.
•If salary lies between 5000 and 10000, tax is 1.5
%.
•If salary is in between 3000 to 5000, Tax is 1%
and if salary is lesser then 3000, Tax is 0%

way2ITe
ch

Switch – Case
Statements

Switch-Case Statements
Syntax:

Switch-Case Statements
Flow chart:

•switch-case statement allows us to make
decisions from multiple choices.
•Integer expression must yield an integer value
like 1, 2, 3, etc.
•The keyword case is followed by an integer or
a character constant.
•Each constant in each case must be different
from all the others.
•The “do this” lines in the above form of
switch represent any valid C statement.

way2ITe
ch

break keyword is always used with the switch
statement to terminate the switch statement or to
prevent the entry in other case block.

way2ITe
ch

The Tips and Traps
1) It is not compulsory to put cases in ascending order
as we did above. We can place them in any order we
want.
way2ITe
ch

You are also allowed to use char values in case
and switch as shown in the following program.
(Program to find whether character entered is
vowel or not)

way2ITe
ch

Modified Form


If a statement doesn’t belong to any case the compiler won’t
report an error. However, the statement would never get executed.
For example, in the following program the printf( ) never goes to
work.
way2ITe
ch


If there is no default in switch, then error will not
be there.

If no case is matching, statements after switch
will execute.

Relational Expression like i>5 are not allowed in
any case.

Even float values not allowed in any case

We can check the value of any expression in a
switch.

Thus the following switch statements are legal.
switch ( i - j * k )
switch ( 3 + 9 % 4 * i )
switch ( a < 5 || b > 7 )
way2ITe
ch

•Expressions can also be used in cases provided they are
constant expressions.
•Thus case 13 + 7 is correct, however, case a + b is incorrect.
•switch may occur within another, but in practice it is
rarely done.
•Such statements would be called nested switch statements.
•The switch statement is very useful while writing menu
driven programs
•Even if there are multiple statements to be executed in each
case there is no need to enclose them within a pair of
braces (unlike if, and else).

switch versus if-else ladder
Disadvantages of switch over if-else
•A float expression cannot be tested using a switch
•Cases can never have variable expressions (for
example it is wrong to say case a+3: )
•Multiple cases cannot use same expressions. Thus
the following switch is illegal:

Advantages of switch over if-else
•Compiler generates a jump table for a switch
during compilation.
•As a result, during execution it simply refers the
jump table to decide which case should be
executed, rather than actually checking which
case is satisfied.
•As against this, if-else are slower because they are
evaluated at execution time.

Let us write some programs based on switch-
case.
1. Write a C program to design simple
calculator (+, -, *, /, %) using switch-case.

Nested Switch Statements

A nested switch in C refers to a switch statement inside
another switch statement.

This allows handling complex decision-making scenarios
where one condition depends on the result of another.

The outer switch evaluates its expression first, and based on
the matching case, the inner switch executes its code block.

Real-Life Example:

Imagine you're at a restaurant ordering a meal:

Outer Decision (Outer Switch): Choose the type of meal
(e.g., vegetarian or non-vegetarian).

Inner Decision (Inner Switch): Based on the meal type,
choose the specific dish (e.g., vegetarian → paneer or dal, non-
vegetarian → chicken or fish).

Nested Switch Statement in C

Nested Switch Statements
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,disc;
float root1,root2,real_part,img_part;
printf("enter coefficients a, b and c :");
scanf("%d %d %d",&a,&b,&c);
disc=b * b - 4 * a * c;
if(a==0)
printf("it is not a quadratic equation\n");
else
switch(disc>0)
{
case 1: root1 = (-b + sqrt(disc)) / (2 * a);
root2 = (-b - sqrt(disc)) / (2 * a);
printf("root1 = %.2f and root2 = %.2f\n", root1, root2);
break;
case 0:
switch(disc==0)
{

Nested Switch Statements
case 1: root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2f\n", root1);
break;
case 0: real_part = -b / (2 * a);
img_part = sqrt(-disc) / (2 * a);
printf("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi\n", real_part,
img_part, real_part, img_part);
break;
}
break;
}
return 0;
}
Tags