0% found this document useful (0 votes)
8 views

Pps ch3

Uploaded by

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

Pps ch3

Uploaded by

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

Need of decision

making if number is odd


{
/* code */
}
else number is eve
n
{
/* code */
}
Decision Making or Conditional Statement

Decision Making statements It allows us to control whether


C program statements are
are used to control the flow of a program segment is
executed sequentially.
program. executed or not.

It evaluates condition or
logical expression first and
If result is true then it takes
based on its result (either true
one path else it takes another
or false), the control is
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 to 5 >= 4 true
< < 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 condition
// true part
}
True

If condition is …
if is single branch
decision making true then only
statement. body will 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

False
condition1

True False
condition2

… True Condition False


3
… True

… …
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 True
condition1

False
Statement3 condition2

True

Statement 1 Statement 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.

Fals
Tru e
e
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 False
Expression1

Expression Expression
2 3

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

Program Output
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.
{

case constant1:  If there is a match, the corresponding
// statements statements after the matching case are
break; executed.
case constant2:
// statements  If there is no match, the default statements
break; are executed.
.
.  If we do not use break, all statements after
. the matching label are executed.
default:
// default statements
 The default clause inside the switch
} statement is optional.
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:
Output
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