SlideShare a Scribd company logo
DECISION MAKING AND
BRANCHING
3-May-20 Decision making branching 1
DECISION MAKING…
3-May-20 Decision making branching 2
• Decision making is about deciding the order of
execution of statements based on certain
conditions or repeat a group of statements
until certain specified conditions are met.
• Since they control the flow of statements also
called as Control Statements
• Two types:
- Conditional control statements
-unconditional control statements
Decision Making -Examples
• C language handles decision-making by supporting
the following statements:
Conditional Statements are:
if statement
switch statement
conditional operator statement (? : operator)
Unconditional Statements are:
goto statement
3-May-20 Decision making branching 3
DECISION MAKING WITH ‘IF’
- If is powerful decision making statement hence
called as two way decision making statement
Syntax:
If(test expression);
3-May-20 Decision making branching 4
False
DECISION MAKING WITH ‘IF’
- The if statement may be implemented in different
forms depending on the complexity of conditions to
be tested. The different forms are
• Simple if statement
• if....else statement
• Nested if....else statement
• Using else if statement
3-May-20 Decision making branching 5
SIMPLE ‘IF’
- The statements inside the body of “if” execute if
the given condition returns true.
- If the condition returns false then the statements
inside “if” are skipped.
- If condition is true both the block of statements
and next statement is executed
- The statement-block may be single or block of
statements
3-May-20 Decision making branching 6
SIMPLE ‘IF’
SYNTAX FLOWCHART
3-May-20 Decision making branching 7
if (test condition)
{
block of
statements;
... ... ...
}
statement-x;
SIMPLE ‘IF’
Program:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y"); }}
O/P:
3-May-20 Decision making branching 8
x is greater than y
‘IF… ELSE’ STATEMENT
- It consists of two blocks of statements each enclosed
inside if block and else block
- If the condition is true, statements True block statement
will be executed
- otherwise statements inside else block are executed.
- In either case, only one block is executed ,not both .
3-May-20 Decision making branching 9
‘IF… ELSE’ STATEMENT
Syntax FLOWCHART
3-May-20 Decision making branching 10
if (test condition)
{
true block statements;
... ... ...
}
else
{
false block statements;
... ... ...
}
statement x;
Example: If… else Statement
Program
#include<stdio.h>
void main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2 == 0)
printf("%d is even",n);
else
printf("%d is odd",n);
getch();
}
Output:
Enter a number: 3
3 is odd
3-May-20 Decision making branching 11
NESTING OF ‘IF … ELSE’ STATEMENT
- executes two different codes depending upon
whether the test expression is true or false.
- Sometimes, a choice has to be made from more
than 2 possibilities.
- allows to check for multiple test expressions and
execute different codes for more than two
conditions.
3-May-20 Decision making branching 12
Syntax: Nested if else
if(test condition1)
{
If(test condition2)
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
statement –x;
3-May-20 Decision making branching 13
3-May-20 Decision making branching 14
Flowchart: Nested if… else
Example: Nested if…else
Program:
void main()
{
float a,b,c;
printf(“ Enter Three values”);
scanf(“%f%f%f”, &a,&b,&c);
printf(“largest value is”);
if(a>b)
{
if(a>c)
printf(“%fn”,a)
else
printf(“%fn”,c)
}
else
{
if(c>b)
printf(“%fn”,c)
else
printf(“%fn”,b)
}
getch();
}
Output:
Enter Three values
22.22 87.43 67.45
Largest value is 87.43
3-May-20 Decision making branching 15
else…if Ladder
- to test set of conditions in sequence called as
multipath decisions
- statements are evaluated from top to bottom
- As soon as the true statement is found, the
statement associated with it is executed
- the control is transferred to the statement-x
(Skipping the rest of the conditions)
- when all the ‘n’ conditions become false, the
final else, containing the default-statement will
be executed3-May-20 Decision making branching 16
Syntax: else…if Ladder
if(condition-1)
{
statements;
}
else if
(condition-2)
{
statements;
}
else if
(condition-3)
{
statements;
}
:
:
else
{
statements;
}3-May-20 Decision making branching 17
3-May-20 Decision making branching 18
Example: else…if Ladder
#include<stdio.h>
void main()
{
int mark;
printf(“Enter Mark:”);
scanf(“%d”, &mark);
if(mark= = 100)
{
printf(“Centum Result”);
}
else if
if((mark >=75 )&&(mark<=99)
{
printf(“Merit Result”);
}
else if
if((mark>=60 )&&(mark<=75)
{
printf(“First Class Result”);
}
else if
if(mark>=40 )&&(mark<=59)
{
printf(“Pass Result”);
}
else
printf(“Fail Result”);
}
getch();
}
3-May-20 Decision making branching 19
Output:
Enter Marks: 79
First Class Result
3-May-20 Decision making branching 20
SWITCH CASE
- Switch is also called as multiway decision statement
- allows to choose only one choice among the many
given choices
- allows a variable to be tested for equality against a list
of values
- The expression in switch evaluates to return an
integral value, which is then compared to the values
present in different cases.
- It executes that block of code which matches the case
value.
- If there is no match, then default block is executed (if
present)3-May-20 Decision making branching 21
SYNTAX: SWITCH CASEswitch(expression)
{
case value-1 :
block-1;
break; /* optional */
case value-2 :
block-2;
break; /* optional */
case value-n :
block-n;
break; /* optional */
:
default :
default-block;
break; /* Optional */
}
3-May-20 Decision making branching 22
SWITCH CASE
- The expression is an integer expression or
characters
- value-1,value-2 are constants or constant
expression also called as case labels
- block1,block2 are statement lists may contain zero
or more statements
- Case labels end with colon(:)
- Break statement at the end of each block signals
the end of particular case and cause an exit from
switch
- Default is an optional case3-May-20 Decision making branching 23
3-May-20 Decision making branching 24
3-May-20 Decision making branching 25
Rules for SWITCH Statement
- The switch expression must be an integral type
- Case labels must be constant or constant
expression.
- Case labels must be unique end with colon(:)
- Break statement transfers the control out of the
switch statement
- Break and default is an optional.
- There can be atmost one default label
- Default may be placed anywhere but usually
placed at the end
3-May-20 Decision making branching 26
The ?: operator
- The conditional operator (? :) is a ternary
operator(it takes three operands), makes two-way
decisions.
- Conditional operator is closely related
with if..else statement.
- If the condition is true then expression1 is
executed else expression2 is executed.3-May-20 Decision making branching 27
The ?: operator
Program:
#include <stdio.h>
int main()
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
puts(mark >= 40 ? "Passed" :
"Failed");
return 0;
}
Output
Enter mark:
39 Failed
Explanation
The program checks the
condition mark >=40, if it is
true "Passed" is printed
else "Failed".
3-May-20 Decision making branching 28
GOTO STATEMENT
- Got statement is used to branch unconditionally
from one point to another in the program
- the control jumps directly to the label mentioned
in the ‘goto’ statement
- A label is an identifier required for ‘goto’
statement to a place where the branch is to be
made
- Label can be anywhere in the program either
before or after the goto label
- It breaks the normal sequence of the program
3-May-20 Decision making branching 29
GOTO STATEMENT
Defining a label: label_name:
- label_name should be a valid identifier name.
- : (colon) should be used after the label_name.
-If the label: is before the statement goto label;
a loop will be formed and some statements will be
executed repeatedly called as Backward jump
-If the label: is placed after the goto label;
some statements will be skipped and the jump is
known as forward jump
3-May-20 Decision making branching 30
SYNTAX: GOTO STATEMENT
Forward jump
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
Backward Jump
label:
statement;
... .. ...
... .. ...
... .. ...
goto label;
3-May-20 Decision making branching 31
;
Flowchart of gotostatemet
3-May-20 Decision making branching 32
EXAMPLE: BackwardGOTO STATEMENT
include <stdio.h>
int main()
{
int number;
number=1;
repeat:
printf("%dn",number);
number++;
if(number<=10)
goto repeat;
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
3-May-20 Decision making branching 33
EXAMPLE: GOTO STATEMENT
#include <stdio.h>
void main()
{
int number;
printf("Enter an integer number: ");
scanf("%d",&number);
if(number<=0)
goto end;
printf("Number is : %d", number);
end: printf("Bye Bye !!!");
}
Output:
First run:
Enter an integer
number: 123
Number is : 123
Bye Bye !!!
Second run:
Enter an integer
number: 0
Bye Bye !!!
3-May-20 Decision making branching 34
MAHENDIRAN N
ASSISTANT PROFESSOR
Department of Computer Science
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
35

More Related Content

PPTX
STACKS IN DATASTRUCTURE
PPTX
Inheritance in java
PPSX
Break and continue
PPTX
Presentation on C Switch Case Statements
PPT
Decision making and branching
PPTX
Control statements in c
PPTX
Unit 4 python -list methods
STACKS IN DATASTRUCTURE
Inheritance in java
Break and continue
Presentation on C Switch Case Statements
Decision making and branching
Control statements in c
Unit 4 python -list methods

What's hot (20)

PPTX
Encapsulation
PPTX
Scope rules : local and global variables
PPTX
Arrays in c
PPTX
Doubly Linked List
PPTX
Binary Tree Traversal
PPTX
Queue ppt
PDF
Features of c
PPT
Difference between stack and queue
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
stack & queue
PPTX
Types of Tree in Data Structure in C++
PPT
Structure in C
PPTX
Subtractor (1)
PPTX
Data structure array
PPTX
Pointer in c
PPTX
Union in C programming
PPT
Two dimensional array
PPT
Pin diagram 8085 microprocessor(For College Seminars)
PDF
History of C/C++ Language
PPT
Array in c
Encapsulation
Scope rules : local and global variables
Arrays in c
Doubly Linked List
Binary Tree Traversal
Queue ppt
Features of c
Difference between stack and queue
BINARY TREE REPRESENTATION.ppt
stack & queue
Types of Tree in Data Structure in C++
Structure in C
Subtractor (1)
Data structure array
Pointer in c
Union in C programming
Two dimensional array
Pin diagram 8085 microprocessor(For College Seminars)
History of C/C++ Language
Array in c
Ad

Similar to Decision makingandbranching in c (20)

PPTX
C statements
PPT
Decision making in C(2020-2021) statements
PPTX
Introduction to Selection control structures in C++
PPTX
Decision Making.pptx
PDF
Controls & Loops in C
PPTX
Mca i pic u-3 handling input output and control statements
PPTX
handling input output and control statements
PPTX
Btech i pic u-3 handling input output and control statements
PPTX
Diploma ii cfpc u-3 handling input output and control statements
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PPTX
Control Statement programming
PPTX
Bsc cs pic u-3 handling input output and control statements
PPT
Flow of Control
PPTX
CONTROL STMTS.pptx
PPTX
Decision Making Statement in C ppt
PPTX
Basics of Control Statement in C Languages
PDF
Unit 2=Decision Control & Looping Statements.pdf
PPT
Visula C# Programming Lecture 3
PPTX
C++ decision making
C statements
Decision making in C(2020-2021) statements
Introduction to Selection control structures in C++
Decision Making.pptx
Controls & Loops in C
Mca i pic u-3 handling input output and control statements
handling input output and control statements
Btech i pic u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Control Statement programming
Bsc cs pic u-3 handling input output and control statements
Flow of Control
CONTROL STMTS.pptx
Decision Making Statement in C ppt
Basics of Control Statement in C Languages
Unit 2=Decision Control & Looping Statements.pdf
Visula C# Programming Lecture 3
C++ decision making
Ad

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Institutional Correction lecture only . . .
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
PPH.pptx obstetrics and gynecology in nursing
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Sports Quiz easy sports quiz sports quiz
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
RMMM.pdf make it easy to upload and study
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Decision makingandbranching in c

  • 1. DECISION MAKING AND BRANCHING 3-May-20 Decision making branching 1
  • 2. DECISION MAKING… 3-May-20 Decision making branching 2 • Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. • Since they control the flow of statements also called as Control Statements • Two types: - Conditional control statements -unconditional control statements
  • 3. Decision Making -Examples • C language handles decision-making by supporting the following statements: Conditional Statements are: if statement switch statement conditional operator statement (? : operator) Unconditional Statements are: goto statement 3-May-20 Decision making branching 3
  • 4. DECISION MAKING WITH ‘IF’ - If is powerful decision making statement hence called as two way decision making statement Syntax: If(test expression); 3-May-20 Decision making branching 4 False
  • 5. DECISION MAKING WITH ‘IF’ - The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are • Simple if statement • if....else statement • Nested if....else statement • Using else if statement 3-May-20 Decision making branching 5
  • 6. SIMPLE ‘IF’ - The statements inside the body of “if” execute if the given condition returns true. - If the condition returns false then the statements inside “if” are skipped. - If condition is true both the block of statements and next statement is executed - The statement-block may be single or block of statements 3-May-20 Decision making branching 6
  • 7. SIMPLE ‘IF’ SYNTAX FLOWCHART 3-May-20 Decision making branching 7 if (test condition) { block of statements; ... ... ... } statement-x;
  • 8. SIMPLE ‘IF’ Program: #include <stdio.h> void main( ) { int x, y; x = 15; y = 13; if (x > y ) { printf("x is greater than y"); }} O/P: 3-May-20 Decision making branching 8 x is greater than y
  • 9. ‘IF… ELSE’ STATEMENT - It consists of two blocks of statements each enclosed inside if block and else block - If the condition is true, statements True block statement will be executed - otherwise statements inside else block are executed. - In either case, only one block is executed ,not both . 3-May-20 Decision making branching 9
  • 10. ‘IF… ELSE’ STATEMENT Syntax FLOWCHART 3-May-20 Decision making branching 10 if (test condition) { true block statements; ... ... ... } else { false block statements; ... ... ... } statement x;
  • 11. Example: If… else Statement Program #include<stdio.h> void main() { int n; printf("Enter a number:"); scanf("%d",&n); if(n%2 == 0) printf("%d is even",n); else printf("%d is odd",n); getch(); } Output: Enter a number: 3 3 is odd 3-May-20 Decision making branching 11
  • 12. NESTING OF ‘IF … ELSE’ STATEMENT - executes two different codes depending upon whether the test expression is true or false. - Sometimes, a choice has to be made from more than 2 possibilities. - allows to check for multiple test expressions and execute different codes for more than two conditions. 3-May-20 Decision making branching 12
  • 13. Syntax: Nested if else if(test condition1) { If(test condition2) { statement block1; } else { statement block2; } } else { statement block3; } statement –x; 3-May-20 Decision making branching 13
  • 14. 3-May-20 Decision making branching 14 Flowchart: Nested if… else
  • 15. Example: Nested if…else Program: void main() { float a,b,c; printf(“ Enter Three values”); scanf(“%f%f%f”, &a,&b,&c); printf(“largest value is”); if(a>b) { if(a>c) printf(“%fn”,a) else printf(“%fn”,c) } else { if(c>b) printf(“%fn”,c) else printf(“%fn”,b) } getch(); } Output: Enter Three values 22.22 87.43 67.45 Largest value is 87.43 3-May-20 Decision making branching 15
  • 16. else…if Ladder - to test set of conditions in sequence called as multipath decisions - statements are evaluated from top to bottom - As soon as the true statement is found, the statement associated with it is executed - the control is transferred to the statement-x (Skipping the rest of the conditions) - when all the ‘n’ conditions become false, the final else, containing the default-statement will be executed3-May-20 Decision making branching 16
  • 17. Syntax: else…if Ladder if(condition-1) { statements; } else if (condition-2) { statements; } else if (condition-3) { statements; } : : else { statements; }3-May-20 Decision making branching 17
  • 18. 3-May-20 Decision making branching 18
  • 19. Example: else…if Ladder #include<stdio.h> void main() { int mark; printf(“Enter Mark:”); scanf(“%d”, &mark); if(mark= = 100) { printf(“Centum Result”); } else if if((mark >=75 )&&(mark<=99) { printf(“Merit Result”); } else if if((mark>=60 )&&(mark<=75) { printf(“First Class Result”); } else if if(mark>=40 )&&(mark<=59) { printf(“Pass Result”); } else printf(“Fail Result”); } getch(); } 3-May-20 Decision making branching 19
  • 20. Output: Enter Marks: 79 First Class Result 3-May-20 Decision making branching 20
  • 21. SWITCH CASE - Switch is also called as multiway decision statement - allows to choose only one choice among the many given choices - allows a variable to be tested for equality against a list of values - The expression in switch evaluates to return an integral value, which is then compared to the values present in different cases. - It executes that block of code which matches the case value. - If there is no match, then default block is executed (if present)3-May-20 Decision making branching 21
  • 22. SYNTAX: SWITCH CASEswitch(expression) { case value-1 : block-1; break; /* optional */ case value-2 : block-2; break; /* optional */ case value-n : block-n; break; /* optional */ : default : default-block; break; /* Optional */ } 3-May-20 Decision making branching 22
  • 23. SWITCH CASE - The expression is an integer expression or characters - value-1,value-2 are constants or constant expression also called as case labels - block1,block2 are statement lists may contain zero or more statements - Case labels end with colon(:) - Break statement at the end of each block signals the end of particular case and cause an exit from switch - Default is an optional case3-May-20 Decision making branching 23
  • 24. 3-May-20 Decision making branching 24
  • 25. 3-May-20 Decision making branching 25
  • 26. Rules for SWITCH Statement - The switch expression must be an integral type - Case labels must be constant or constant expression. - Case labels must be unique end with colon(:) - Break statement transfers the control out of the switch statement - Break and default is an optional. - There can be atmost one default label - Default may be placed anywhere but usually placed at the end 3-May-20 Decision making branching 26
  • 27. The ?: operator - The conditional operator (? :) is a ternary operator(it takes three operands), makes two-way decisions. - Conditional operator is closely related with if..else statement. - If the condition is true then expression1 is executed else expression2 is executed.3-May-20 Decision making branching 27
  • 28. The ?: operator Program: #include <stdio.h> int main() { int mark; printf("Enter mark: "); scanf("%d", &mark); puts(mark >= 40 ? "Passed" : "Failed"); return 0; } Output Enter mark: 39 Failed Explanation The program checks the condition mark >=40, if it is true "Passed" is printed else "Failed". 3-May-20 Decision making branching 28
  • 29. GOTO STATEMENT - Got statement is used to branch unconditionally from one point to another in the program - the control jumps directly to the label mentioned in the ‘goto’ statement - A label is an identifier required for ‘goto’ statement to a place where the branch is to be made - Label can be anywhere in the program either before or after the goto label - It breaks the normal sequence of the program 3-May-20 Decision making branching 29
  • 30. GOTO STATEMENT Defining a label: label_name: - label_name should be a valid identifier name. - : (colon) should be used after the label_name. -If the label: is before the statement goto label; a loop will be formed and some statements will be executed repeatedly called as Backward jump -If the label: is placed after the goto label; some statements will be skipped and the jump is known as forward jump 3-May-20 Decision making branching 30
  • 31. SYNTAX: GOTO STATEMENT Forward jump goto label; ... .. ... ... .. ... ... .. ... label: statement; Backward Jump label: statement; ... .. ... ... .. ... ... .. ... goto label; 3-May-20 Decision making branching 31 ;
  • 32. Flowchart of gotostatemet 3-May-20 Decision making branching 32
  • 33. EXAMPLE: BackwardGOTO STATEMENT include <stdio.h> int main() { int number; number=1; repeat: printf("%dn",number); number++; if(number<=10) goto repeat; return 0; } Output: 1 2 3 4 5 6 7 8 9 10 3-May-20 Decision making branching 33
  • 34. EXAMPLE: GOTO STATEMENT #include <stdio.h> void main() { int number; printf("Enter an integer number: "); scanf("%d",&number); if(number<=0) goto end; printf("Number is : %d", number); end: printf("Bye Bye !!!"); } Output: First run: Enter an integer number: 123 Number is : 123 Bye Bye !!! Second run: Enter an integer number: 0 Bye Bye !!! 3-May-20 Decision making branching 34
  • 35. MAHENDIRAN N ASSISTANT PROFESSOR Department of Computer Science Sri Ramakrishna College of Arts and Science Coimbatore - 641 006 Tamil Nadu, India 35