0% found this document useful (0 votes)
14 views35 pages

CPP Module 3sequence Control

Module III covers sequence control in C programming, focusing on decision-making structures such as if, if-else, and switch-case statements. It explains the use of relational operators for evaluating conditions and provides examples of programs that demonstrate how to implement these decision structures. Additionally, it includes practice programs to reinforce the concepts learned.

Uploaded by

hinalpatel.1103
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)
14 views35 pages

CPP Module 3sequence Control

Module III covers sequence control in C programming, focusing on decision-making structures such as if, if-else, and switch-case statements. It explains the use of relational operators for evaluating conditions and provides examples of programs that demonstrate how to implement these decision structures. Additionally, it includes practice programs to reinforce the concepts learned.

Uploaded by

hinalpatel.1103
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

Module III:

Sequence Control
Decision Structure
Decision Making or Conditional
Statement

It allows us to control
Decision Making
C program statements are whether a program
statements are used to
executed sequentially. segment is executed or
control the flow of program.
not.

It evaluates condition or
logical expression first and
If result is true then it takes
based on its result (either
one path else it takes
true or false), the control is
another path.
transferred to particular
statement.
Decision Making Statements in C
Decision Making Statements
are
One way Decision: if (Also known as simple if)
Two way Decision: if…else
Multi way Decision: if…else if…else if…else
Two way Decision: ?: (Conditional Operator)
n-way Decision: switch…case
Relational Operators
Relational Operator is used to compare two expressions.
It gives result either true or false based on relationship of two expressions.

Math C Meaning Example Result


> > is greater than 5 > 4 true
≥ >= is greater than or equal 5 >= 4 true
to
< < is less than 5 < 4 false
≤ <= is less than or equal to 5 <= 4 false
≠ != is not equal to 5 != 4 true
= == is equal to 5 == 4 false

If statement
if

if is a keyword.
Flowchart of
if
Syntax
if(condition)
{ False
// Body of the if
conditio
// true part n
}
True

If condition is …
if is single branch
decision making true then
only body will
statement.
be executed.
WAP to print Zero if given number is 0

Program Output
1 #include<stdio.h> Enter Number:0
2 void main() Zero
3 {
4 int a;
5 printf("Enter Number:");
6 scanf("%d",&a);
7 if(a == 0)
8 {
9 printf("Zero");
10 }
11 }
WAP to print Positive or Negative Number

Program Output
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a >= 0)
8 {
9 printf("Positive Number");
10 }
11 if(a < 0)
12 {
13 printf("Negative Number");
14 }
15 }
Modulus
Operator
 % is modulus operator in C
 It divides the value of one expression (number)
by the value of another expression (number),
and returns the remainder.
 Syntax: express1 % express2
 E.g.
 7%2 Answer: 1
 6%2 Answer: 0
 25%10 Answer: 5
 37%28 Answer: 9
WAP to print Odd or Even Number

Program Output
1 #include<stdio.h> Enter Number:12
2 void main() Even Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:11
6 scanf("%d",&a); Odd Number
7 if(a%2 == 0)
8 {
9 printf("Even Number");
10 }
11 if(a%2 != 0)
12 {
13 printf("Odd Number");
14 }
15 }

If..else statement
if...else
if…else is two branch decision making statement
If condition is true then true part will be executed else false part will be
executed
else is keyword Flowchart of if…else

Syntax
if(condition)
True False
condition
{
// true part
}
else
{
… …
// false part
} …
WAP to print Positive or Negative Number using if…else

Program Output
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a >= 0)
8 {
9 printf("Positive Number");
10 }
11 else
12 {
13 printf("Negative Number");
14 }
15 }
WAP to print Odd or Even Number using if…else

Program Output
1 #include<stdio.h> Enter Number:12
2 void main() Even Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:11
6 scanf("%d",&a); Odd Number
7 if(a%2 == 0)
8 {
9 printf("Even Number");
10 }
11 else
12 {
13 printf("Odd Number");
14 }
15 }
WAP to find largest number from given 2 numbers using if

Program Output
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 if(a > b)
8 {
9 printf("%d is largest", a);
10 }
11 if(a < b)
12 {
13 printf("%d is largest", b);
14 }
15 }
WAP to find largest number from given 2 numbers
using if…else

Program Output
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 if(a > b)
8 {
9 printf("%d is largest", a);
10 }
11 else
12 {
13 printf("%d is largest", b);
14 }
15 }
{ }
If body of if contains only one statement then { } are not compulsory
But if body of if contains more than one statements then { } are
compulsory

if(a >= b) Both if(a >= b)


{ printf("%d is largest", a);
are
printf("%d is largest", a);
} same

If…else if…else if…else


Ladder if
If…else if…else if…else
 if…else if…else if…else is multi branch decision making statement.
 If first if condition is true then remaining if conditions will not be
evaluated.
 If first if condition is false then second if condition will be evaluated
and if it is true then remaining if conditions will not be evaluated.
 if…else if…else if…else is also known as if…else if ladder

Syntax
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
else
statement-3;
if…else if…else ladder flowchart

conditio Fals
n1 e

Tru conditio Fals


e n2 e
… Tru Conditio
Fals
e n3 e
… Tru
e
… …
WAP to print Zero, Positive or Negative Number

Program Output
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a > 0)
8 printf("Positive Number");
9 else if(a==0)
10 printf("Zero");
11 else
12 printf("Negative Number");
13 }

Nested if
Nested if
 If condition-1 is true then condition-2 is evaluated. If it is true then
statement-1 will be executed.
 If condition-1 is false then statement-3 will be executed.
Syntax
if(condition-1)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
Nested if flowchart

False condition
True
1

False
Statement condition
3 2

True
Statement Statement
1 2

Next
Statement
WAP to print maximum from given three numbers
Program
1 void main(){
2 int a, b, c; Output
3 printf("Enter Three Numbers:"); Enter Three Numbers:7
4 scanf("%d%d%d",&a,&b,&c); 5
5 if(a>b) 9
6 { 9 is max
7 if(a>c)
8 printf("%d is max",a);
9 else
10 printf("%d is max",c);
11 }
12 else
13 {
14 if(b>c)
15 printf("%d is max",b);
16 else
17 printf("%d is max",c);
18 }
19 }

Conditional Operator
? : (Conditional Operator)
 The conditional works operator is similar to the if-else.
 It is also known as a ternary operator.
 It returns first value of expression (before colon(:)) if expression is true
and second value of expression if expression is false.

False
True

variable = Expression1 ? Expression2 : Expression3


Result value
Result value
Conditional operator flowchart
 Here, Expression1 is the condition to be evaluated.
 If the condition(Expression1) is True then Expression2 will be executed
and the result will be returned.
 Otherwise, if condition(Expression1) is false then Expression3 will be
executed and the result will be returned.

True Expression False


1

Expression Expression
2 3

Variable
WAP to find largest number from given 2 numbers using ? :

Outpu
Program
t
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b, max;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 max = a>b?a:b;
8 printf("%d is largest",max);
9 }

switch…case
switch...case
 The switch statement allows to execute one code block among many
alternatives.
 It works similar to if...else..if ladder.
Syntax  The expression is evaluated once and
switch (expression) compared with the values of each case.
{
​  If there is a match, the corresponding
case constant1: statements after the matching case are
// statements executed.
break;
case constant2:  If there is no match, the default
// statements statements are executed.
break;
 If we do not use break, all statements
.
. after the matching label are executed.
.  The default clause inside the switch
default: statement is optional.
// default statements
}
WAP that asks day number and prints day name using switch…case

void main(){ case 7:


int day; printf("Saturday");
printf("Enter day number(1-7):"); break;
scanf("%d",&day); default:
switch(day) printf("Wrong input");
{ break;
case 1: }
printf("Sunday"); }
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break; Enter day number(1-7):5
case 4: Thursday
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
Practice programs
1) Write a program to check whether entered character is vowel or not?
2) Write a program to perform Addition, Subtraction, Multiplication and Division
of 2 numbers as per user’s choice (using if…else/Nested if/Ladder if).
3) Write a program to read marks of five subjects. Calculate percentage and print
class accordingly. Fail below 35, Pass Class between 35 to 45, Second Class
between 45 to 60, First Class between 60 to 70, Distinction if more than 70.
4) Write a program to find out largest number from given 3 numbers (Conditional
operator).
5) Write a program to print number of days in the given month.

You might also like