Decision making using if statement

222 views 19 slides Mar 30, 2020
Slide 1
Slide 1 of 19
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

About This Presentation

This tutorial helps beginners to understand, how a variety of if statements help in decision making in c programming. It also contains flow charts and illustrations to improve comprehension


Slide Content

Decision Making Using if
Statement
Prepared By:
Dr. Chandan Kumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly

Decision Making Using if Statement
The statements which are used to take decision
Decision based on some test expression
These statements are used to decide to execute
some code/s and ignore some code/s
Four variant of decision making statement (if)
if statement
if-else statement
nested if statement
else-if statement

if statement
This statement is also known as only if
statement or one way branching statement
checks whether the given expression is true of
false
If true then body of the if statement is executed
If false then body of the if statement is ignored
Syntax
if(test_expression)
{
statement/s; // body of if
}

Flow Chart if statement

Write a C program to print the number entered by user only if the
number entered is even
#include <stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf(“Enter a number :”);
scanf(“%d”,&num);
if ( num%2==0)
{
printf(“ \nNumber %d is an even number”,num);
}
getch();
}
Output
Enter a number: 2
Number 2 is an even number

if-else statement
Also known as two way branching statement
Used when programmer wants to execute
statement/s when the test expression is true
and execute another statement/s if the test
expression is false
Only one block is executed at a time

if-else statement
Syntax
if(test_expression)
{
statement/s; // body of if
}
else
{
statement/s; // body of else
}

Flow chart if-else

Write a C program to print the number entered by user only if the
number entered is even or odd
#include <stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf(“Enter a number :”);
scanf(“%d”,&num);
if ( num%2==0)
{
printf(“ \nNumber %d is an even number”,num);
}
else
{
printf(“ \nNumber %d is an odd number”,num);
}
getch();
}

if-else statement
Output 1
Enter a number: 2
Number 2 is an even number
Output 2
Enter a number: 3
Number 3 is an odd number

Nested if statement
An if statement inside another if statement
Programmers used this statement when want to
check one condition inside another condition
When an if else statement is present inside the
body of another “if” or “else”

Nested if statement
Syntax
if(test_expression1/ condition 1)
{ //Nested if else inside the body of "if"
if(test_expression2/ condition 2)
{ //Statements inside the body of nested "if”}
else
{ //Statements inside the body of nested "else”}
}
else
{ //Statements inside the body of "else" }

Nested if statement

Write a C program to print largest number among three numbers
#include<stdio.h>
#include<conio.h>
Void main()
{
int num1,num2,num3;
clrscr();
printf(“Enter three numbers: );
scanf(“%d%d%d”,&num1,&num2,&num3);
if(num1>num2)
{
if(num1>num3)
printf(“ %d is the largest number”,num1);
else
printf(“ %d is the largest number,num3);
}

Nested if statement
else
{
if(num2>num3)
printf(“ %d is the largest number”,num2);
else
printf(“ %d is the largest number,num3);
}
getch();
}
Output1:
Enter three numbers: 10 12 11
12 is the largest number

else-if statement
Used in program when if statement having
multiple decisions
Syntax
if(test_expression1)
{ //execute code }
.
.
.
else if(test_expression n)
{ //execute code }
else
{ //execute code }

else-if statement

Write a C program to print largest number among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf(“Enter three numbers: );
scanf(“%d%d%d”,&num1,&num2,&num3);
if (num1>num2 && num1>num3)
printf(“ %d is the largest number”,num1);
else if (num2>num1 && num2>num3)
printf(“ %d is the largest number”,num2);
else
printf(“ %d is the largest number”,num3);
getch();
}
Tags