Lecture 7

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

18CSS26 & ARTIFICIAL INTELLIGENCE

Topics Covered

▪ DECISION MAKING AND BRANCHING


▪ Introduction
▪ Decision Making with if Statements
1.Simple if-statement
2.if…...else statement
3.Nested if…..else statement
4.else if ladder

SKASC 1
18CSS26 & ARTIFICIAL INTELLIGENCE
DECISION MAKING AND BRANCHING
INTRODUCTION

• A C program is a set of statements which are normally


executed sequentially in order in which they appear.
• In some cases, it is required to change the order of
execution of statements based on certain conditions or
repeat a group of statements until certain specified
conditions are met.

SKASC 2
INTRODUCTION 18CSS26 & ARTIFICIAL INTELLIGENCE

• C language possesses such decision making capabilities by


supporting the following statements:
1.if statement
2.switch statement
3.Conditional operator statement
4.goto statement
• These statements are known as decision-making
statements.
• These are used to control the flow of execution, they are
also known as control statements.
SKASC 3
18CSS26 & ARTIFICIAL INTELLIGENCE

Decision Making with if Statement

• The ‘if’ statement is a powerful decision-making


statement used to control the flow of execution of
statements.
• It is basically a two-way decision statement and is used
in conjunction with an expression.
• General Form

if (test expression)

SKASC 4
18CSS26 & ARTIFICIAL INTELLIGENCE

Decision Making with if Statement

• It allows the computer to evaluate the expression first


and then, depending on whether the value of the
expression (relation or condition) is 'true' (or non-
zero) or 'false' (zero), it transfers the control to a
particular statement.
• This point of program has two paths to follow, one for
the true condition and the other for the false
condition
SKASC 5
18CSS26 & ARTIFICIAL INTELLIGENCE

Decision Making with if Statement


Two-way branching

Entry

test False
expression?

True

SKASC 6
18CSS26 & ARTIFICIAL INTELLIGENCE

Decision Making with if Statement


• Some examples of decision making using if statements are,
– if (bank balance is zero)
borrow Money

– if (code is 1)
Person is Male

– if (room is dark)
put on lights

– if (age is more than 55)


person is retired
SKASC 7
18CSS26 & ARTIFICIAL INTELLIGENCE

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

Decision Making with if Statement

• The following are the various forms of if statements:

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

• The general form of simple if statement is.

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.

• If the test expression is true, the statement-block will be


executed; otherwise the statement-block will be skipped and
the execution will jump to the statement-x.

Note:

• When the condition is true both the statement-block and the


statement-x are executed in sequence.

SKASC 11
18CSS26 & ARTIFICIAL INTELLIGENCE

Flowchart of simple if control


Entry

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

• More than one test condition can be included in


a test expression by using && and || conditions.
• Example: if(weight<50 && height>170)
• Here the if statement will return true only if both
the conditions are true i.e if weight is less than 50
and height is greater than 170.

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

THE IF…..ELSE STATEMENT

• The ‘if…else’ statement is the extension of simple if


statement. The general form is,

if(test_expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x;
SKASC 17
18CSS26 & ARTIFICIAL INTELLIGENCE

THE IF…..ELSE STATEMENT

• If the test expression is true, then the true-block


statements, immediately following the if statements are
executed; otherwise the false-block statements are
executed, not both.
• In both the cases, the control is transferred
subsequently to the statement-x.

SKASC 18
18CSS26 & ARTIFICIAL INTELLIGENCE

THE IF…..ELSE STATEMENT


Entry

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

THE IF…..ELSE STATEMENT


#include <stdio.h>
Output
int main() Enter your age:17
{ You are not eligible for voting

int age; Output


printf("Enter your age:"); Enter your age:18
You are eligible for voting
scanf("%d",&age);
if(age >=18)
printf("You are eligible for voting");
else
printf("You are not eligible for
voting");
return 0;
}
SKASC 20
18CSS26 & ARTIFICIAL INTELLIGENCE

THE IF…..ELSE STATEMENT


// Check whether an integer is odd or even
Output
#include <stdio.h> Enter an integer: 13
int main() { 13 is an odd integer
int number;
printf("Enter an integer: "); Output
scanf("%d", &number); Enter an integer: 14
// True if the remainder is 0 14 is an even integer.
if (number%2 == 0)
{
printf("%d is an even integer.",number);
}
else
{
printf("%d is an odd integer.",number);
}
return 0;
}

SKASC 21
18CSS26 & ARTIFICIAL INTELLIGENCE

THE IF…..ELSE STATEMENT


#include <stdio.h>
int main() Output
{ Enter a number:-9
int x; The number is Negative

printf("\n Enter a number:");


scanf("%d",&x); Output
Enter a number:5
if(x>0) { The number is Positive
printf("\n The number is Positive"); }
else
{ printf("\n The number is Negative");}
return 0;
}

SKASC 22
18CSS26 & ARTIFICIAL INTELLIGENCE

NESTING OF IF …ELSE STATEMENTS


• When a series of decisions are involved, we may have to
use more than one if …else statement in nested form.

• If the condition-1 is false the statement-3 will be


executed; otherwise it continues to perform the 2nd test.

• If the condition-2 is true, the statemen-1 will be


evaluated otherwise the statement-2 will be evaluated and
then the control is transferred to statement-x.

SKASC 23
18CSS26 & ARTIFICIAL INTELLIGENCE

NESTING OF IF …ELSE STATEMENTS


General Form
if (test_condition1)
{
if (test_condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
SKASC 24
18CSS26 & ARTIFICIAL INTELLIGENCE

NESTING OF IF …ELSE STATEMENTS

SKASC 25
18CSS26 & ARTIFICIAL INTELLIGENCE

NESTING OF IF …ELSE STATEMENTS


#include<stdio.h>
int main( )
{ int n; Output
printf("enter a number: "); enter a number: 0
scanf("%d",&n); Zero
if(n<=0)
{
if(n<0)
Output
{ enter a number: -7
printf("%d is negative",n); -7 is negative
}
else Output
{ enter a number: 10
printf("zero");
}
10 is positive
}
else
{
printf("%d is positive",n);
}
return 0; SKASC 26
}
18CSS26 & ARTIFICIAL INTELLIGENCE

NESTING OF IF …ELSE STATEMENTS


//Nested if-else statement
#include<stdio.h>
int main() Output
{ Enter three numbers 12 45 67
int a, b, c;
printf("Enter three numbers\t");
67 is greatest
scanf("%d %d %d",&a,&b,&c);
if(a>b) Output
{ //if bodystarts Enter three numbers 23 -5 8
if(a>c)
23 is greatest
printf("%d is greatest", a);
else
printf("%d is greatest",c); Output
}//if body ends Enter three numbers -99 45 33
else 45 is greatest
{//else body starts
if(b>c)
printf("%d is greatest ",b);
else
printf("%d is greatest",c);
}//else body ends
}
SKASC 27
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

• The conditions are evaluated from the top.

• When all the n conditions become false, then the final

else containing the default-statement will be executed.

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

❖ A C program is a set of statements which are normally


executed sequentially in order in which they appear.
❖ Decision-making statements are used to control the flow
of execution, they are also known as control statements.
❖ The ‘if’ statement is a powerful decision-making
statement used to control the flow of execution of
statements.
❖ If the test expression is true, the statement-block will be
executed; otherwise the statement-block will be skipped
and the execution will jump to the statement-x.

SKASC 33
18CSS26 & ARTIFICIAL INTELLIGENCE

SKASC 34

You might also like