You are on page 1of 38

SRI KRISHNA ARTS AND SCIENCE COLLEGE

COIMBATORE

Department of COMPUTER APPLICATIONS

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

• 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 4
INTRODUCTION
• 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 5
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 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

• if (age is more than 55)


person is retired

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

• 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 11
SIMPLE IF STATEMENT

• The general form of simple if statement is.

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.

• 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 13
Flowchart of simple if control
Entry

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

• 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 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

• 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 20
THE IF…..ELSE STATEMENT
Entry

True False
Test
expression?

True-block statements False-block statements

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

printf("\n The number is Positive"); }


  else
  { printf("\n The number is Negative");}
  return 0;
}
SKASC 24
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 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

• 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 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

#include<stdio.h> if((num1>=num2) && (num1>=num3))


Output
int main() { printf("\nFirst number %d is
greatest",num1); Enter first number :12
{ Enter second number :4
}
int num1, num2, num3; Enter third number :5
else if((num2>=num1) && (num2>=num3))
printf("Enter first number\t:");
{ printf("\nSecond number %d is First number 12 is greatest
scanf("%d",&num1); greatest",num2);
printf("Enter second number\ }
t:");
else if((num3>=num1) && (num3>=num2)) Output
scanf("%d",&num2); Enter first number :25
{ printf("\nThird number %d is
printf("Enter third number\t:"); greatest",num3);
Enter second number :-98
scanf("%d",&num3); Enter third number :87
}
return 0; Third number 87 is greatest
}

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.

• 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 35
Test Your Knowledge

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

You might also like