Professional Documents
Culture Documents
Lecture 7
Lecture 7
Lecture 7
Topics Covered
SKASC 1
18CSS26 & ARTIFICIAL INTELLIGENCE
DECISION MAKING AND BRANCHING
INTRODUCTION
SKASC 2
INTRODUCTION 18CSS26 & ARTIFICIAL INTELLIGENCE
if (test expression)
SKASC 4
18CSS26 & ARTIFICIAL INTELLIGENCE
Entry
test False
expression?
True
SKASC 6
18CSS26 & ARTIFICIAL INTELLIGENCE
– if (code is 1)
Person is Male
– if (room is dark)
put on lights
Example
#include <stdio.h>
int main()
{
int a= 50;
int b = 100;
if (a<b)
{
printf("%d is less than %d",a,b);
}
return 0;
}
Output
50 is less than 100
SKASC 8
18CSS26 & ARTIFICIAL INTELLIGENCE
1.Simple if-statement
2.if…...else statement
3.Nested if…..else statement
4.else if ladder
SKASC 9
18CSS26 & ARTIFICIAL INTELLIGENCE
SIMPLE IF STATEMENT
if (test expression)
{
statement-block;
}
statement-x;
SKASC 10
18CSS26 & ARTIFICIAL INTELLIGENCE
SIMPLE IF STATEMENT
• The ‘statement block’ may be a single statement or a group
of statements.
Note:
SKASC 11
18CSS26 & ARTIFICIAL INTELLIGENCE
Test True
Expression
?
Statement-block
False
Statement-x
Next Statement
SKASC 12
18CSS26 & ARTIFICIAL INTELLIGENCE
SIMPLE IF STATEMENT
#include<stdio.h>
int main( )
{
int a;
printf("enter the value :");
scanf ("%d", &a);
if(a>0)
printf(“Positive integer");
return 0;
}
Output
Enter the value : 10
Positive integer
SKASC 13
18CSS26 & ARTIFICIAL INTELLIGENCE
SIMPLE IF STATEMENT
#include<stdio.h>
int main()
{
int a=5, b=10;
if(a>10 && a>b)
printf(“a is greater than 10”);
printf(“a is greater than b”);
return 0;
}
Output
a is greater than b
SKASC 14
18CSS26 & ARTIFICIAL INTELLIGENCE
SIMPLE IF STATEMENT
SKASC 15
Multiple if statements- Example 18CSS26 & ARTIFICIAL INTELLIGENCE
#include <stdio.h>
int main()
{ Output
int x, y; enter the value of x:3
printf("enter the value of x:"); enter the value of y:3
scanf("%d", &x); x is equal to y
printf("enter the value of y:"); End of Program
scanf("%d", &y);
if (x>y)
Output
{
enter the value of x:5
printf("x is greater than y\n");
enter the value of y:7
}
x is less than y
if (x<y)
End of Program
{
printf("x is less than y\n");
}
Output
if (x==y)
enter the value of x:10
{
printf("x is equal to y\n");
enter the value of y:5
}
x is greater than y
printf("End of Program"); End of Program
return 0;
SKASC 16
}
18CSS26 & ARTIFICIAL INTELLIGENCE
if(test_expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x;
SKASC 17
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 18
18CSS26 & ARTIFICIAL INTELLIGENCE
True False
Test
Test
expression?
expression?
True-block
True-blockstatements
statements False-block
False-blockstatements
statements
Statement-x
Statement-x
SKASC 19
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 21
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 22
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 23
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 25
18CSS26 & ARTIFICIAL INTELLIGENCE
ELSE IF LADDER
• A multi-path decision is a chain of if statement in which
the statement associated with each else is an if statement.
• As soon as a true condition is found, the statement
associated with it is executed and the control is
transferred to the statement-x
SKASC 28
18CSS26 & ARTIFICIAL INTELLIGENCE
ELSE IF LADDER
• General Form
if ( condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else if (condition n)
statement n;
else
default statement;
statement-x;
SKASC 29
18CSS26 & ARTIFICIAL INTELLIGENCE
ELSE IF LADDER
SKASC 30
18CSS26 & ARTIFICIAL INTELLIGENCE
ELSE IF LADDER
#include<stdio.h>
int main()
{
Output
int num1, num2, num3;
printf("Enter first number\t:");
Enter first number :12
scanf("%d",&num1); Enter second number :4
printf("Enter second number\t:"); Enter third number :5
scanf("%d",&num2);
printf("Enter third number\t:"); First number 12 is greatest
scanf("%d",&num3);
if((num1>=num2) && (num1>=num3))
{ Output
printf("\nFirst number %d is greatest",num1);
Enter first number :25
}
else if((num2>=num1) && (num2>=num3))
Enter second number :-98
{ Enter third number :87
printf("\nSecond number %d is greatest",num2);
} Third number 87 is greatest
else if((num3>=num1) && (num3>=num2))
{
printf("\nThird number %d is greatest",num3);
}
return 0;
} SKASC 31
18CSS26 & ARTIFICIAL INTELLIGENCE
ELSE IF LADDER
#include<stdio.h> else if(marks>=65){
int main() printf("B Grade");
{ }
int marks;
printf(" Enter the marks for C
else if(marks>=50){
Programming:\n"); printf("C Grade");
scanf("%d",&marks); }
if(marks>=95){ else if(marks>=40){
printf("O Grade");
printf("P Grade");
}
else if(marks>=85){ }
printf("S Grade"); else{
} printf("Fail");
else if(marks>=75){ }
printf("A Grade");
return 0;
}
}
SKASC 32
18CSS26 & ARTIFICIAL INTELLIGENCE
Summary
SKASC 33
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 34