Professional Documents
Culture Documents
COIMBATORE
Programming in C
Class – I BSc CSA ‘A’
Classroom Code - k4embjr
LECTURE-07
Facilitator – Prof.A.Shobana
Assistant Professor
Department of Computer Applications
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 2
Decision making and Branching
Lecture Videos
https://
drive.google.com/file/d/1XZTu2KowtzItdyR7KpEygA
YZc_i9m_il/view?usp=sharing
References
1. https://www.youtube.com/watch?v=Fm9qaSijX8U
2. https://www.youtube.com/watch?v=Y8cR0P5S21M
SKASC 3
Decision Making And Branching Introduction
1. if statement
2. switch statement
4. goto statement
• These are used to control the flow of execution, they are also
known as control statements.
SKASC 5
Decision Making with if Statement
• The ‘if’ statement is a powerful decision-making statement used to
control the flow of execution of statements.
• General Form
if (test expression)
SKASC 6
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 7
Decision Making with if Statement
Two-way branching
Entry
test False
expression?
True
SKASC 8
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
SKASC 9
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 10
Decision Making with if Statement
if (test expression)
{
statement-block;
}
statement-x;
SKASC 12
SIMPLE IF STATEMENT
• The ‘statement block’ may be a single statement or a group
of statements.
Note:
Test True
Expression
?
Statement-block
False
Statement-x
Next Statement
SKASC 14
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 15
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 16
SIMPLE IF STATEMENT
SKASC 17
Multiple if statements- Example
#include <stdio.h>
int main()
{
Output
int x, y;
enter the value of x:3
printf("enter the value of x:");
scanf("%d", &x);
enter the value of y:3
printf("enter the value of y:");
x is equal to y
scanf("%d", &y); End of Program
if (x>y)
{ Output
printf("x is greater than y\n"); enter the value of x:5
} enter the value of y:7
if (x<y) x is less than y
{ End of Program
printf("x is less than y\n");
}
if (x==y) Output
{ 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 18
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 19
THE IF…..ELSE STATEMENT
SKASC 20
THE IF…..ELSE STATEMENT
Entry
True False
Test
expression?
Statement-x
SKASC 21
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
scanf("%d",&age); You are eligible for voting
if(age >=18)
printf("You are eligible for voting");
else
printf("You are not eligible for
voting");
return 0;
}
SKASC 22
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 23
THE IF…..ELSE STATEMENT
#include <stdio.h>
int main() Output
{ Enter a number:-9
The number is Negative
int x;
printf("\n Enter a number:");
Output
scanf("%d",&x); Enter a number:5
if(x>0) { The number is Positive
SKASC 25
NESTING OF IF …ELSE STATEMENTS
General Form
if (test_condition1)
{
if (test_condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
SKASC 26
NESTING OF IF …ELSE STATEMENTS
SKASC 27
NESTING OF IF …ELSE STATEMENTS
#include<stdio.h>
int main( )
{ int n; Output
printf("enter a number: "); enter a number: 0
scanf("%d",&n);
if(n<=0)
Zero
{
if(n<0) Output
{ enter a number: -7
printf("%d is negative",n);
-7 is negative
}
else
{ Output
printf("zero"); enter a number: 10
}
}
10 is positive
else
{
printf("%d is positive",n);
}
return 0;
}
SKASC 28
NESTING OF IF …ELSE STATEMENTS
//Nested if-else statement printf("%d is greatest",c);
#include<stdio.h> }//if body ends
int main() else Output
{ {//else body starts Enter three numbers 12 45 67
int a, b, c; if(b>c) 67 is greatest
printf("Enter three printf("%d is greatest ",b); Output
numbers\t");
else Enter three numbers 23 -5 8
scanf("%d %d 23 is greatest
printf("%d is greatest",c);
%d",&a,&b,&c);
}//else body ends
if(a>b) Output
} Enter three numbers -99 45 33
{ //if bodystarts
if(a>c) 45 is greatest
printf("%d is greatest", a);
else
SKASC 29
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
• When all the n conditions become false, then the final else containing
SKASC 30
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 31
ELSE IF LADDER
SKASC 32
ELSE IF LADDER
SKASC 33
ELSE IF LADDER
else if(marks>=65){
#include<stdio.h> printf("B Grade");
int main()
}
{
else if(marks>=50){
int marks;
printf("C Grade");
printf(" Enter the marks for C Programming:\n");
scanf("%d",&marks);
}
if(marks>=95){ else if(marks>=40){
printf("O Grade"); printf("P Grade");
} }
else if(marks>=85){ else{
printf("S Grade"); printf("Fail");
}
}
else if(marks>=75){
return 0;
printf("A Grade");
}
}
SKASC 34
Summary
• Decision-making statements are used to control the flow of execution,
they are also known as control statements.
https://forms.gle/nBWzSS3wDC2Sa8Hm9
Additional References
• https://www.programiz.com/c-programming/c-if-else-statement
• https://www.javatpoint.com/c-if-else
• https://en.wikipedia.org/wiki/Conditional_statement
• http://codewiki.wikidot.com/c:if-else
SKASC 38