SlideShare une entreprise Scribd logo
1  sur  28
Flow Control Statements
(Decision Making)
Control Statements
A program consists of a number of statements which are usually executed in sequence.
Programs can be much more powerful if we can control the order in which statements
are run.
Statements fall into three general types;
1. Assignment, where values, usually the results of calculations, are stored in variables.
2. Input / Output, data is read in or printed out.
3. Control, the program makes a decision about what to do next.
11/29/2020 2
Control Statements
Control statements in C are used to write powerful programs by:
1. Repeating important sections of the program.
2. Selecting between optional sections of a program.
11/29/2020 3
Control Statements
Control
statements
Selection
Statements
The if else
statement
The switch
statements
Iteration
statements
The while loop &
Do while loop
The for loop
The break
statement
Continue
statement
Goto statement
11/29/2020 4
Control Statements cont.
Structure Meaning Example
Sequential It means the instructions are carried
out in sequence
a+=5;
a=a+b;
Selection Sequence of the instruction are
determined by the result of the
condition
if (x>y)
a = a+1;
else
a= a-1;
Iteration Here the statements are repeatedly
executed
while (a<=10)
printf (“%d” , a);
Encapsulation Here the an iteration statement can
have selection inside it or vice versa
while (a<=10)
{
if(a>5)
printf (%d” , a);
}
11/29/2020 5
‘C’ language provides four general structure by which statements can be executed
SELECTION STATEMENT
11/29/2020 6
Types of Selection Statement
1. Simple if Selection statement
2. Nested if Selection statement
3. if else Selection statement
4. Nested if else Selection statement
5. else if ladder Selection statement
11/29/2020 7
Simple if Selection statement
It is used to control the flow of execution of the statements and also to
test logically whether the condition is true or false.
if the condition is true then the statement following the “if “ is executed if
it is false then the statement is skipped.
11/29/2020 8
Syntax:
if ( condition )
{
statement ;
}
Test Condition
Executable X - Statement
True
Selection Statement
Properties of an if statement:
1. if the condition is true then the simple or compound statements are executed.
2. If the condition is false it will skip the statement.
3. The condition is given in parenthesis and must be evaluated as true or false.
4. If a compound structure is provided, it must be enclosed in opening and closing braces
11/29/2020 9
//Biggest of Two Numbers
#include <stdio.h>
void main()
{
int a, b;
printf(“Enter the A and B Value:n”);
scanf(“%d,%d”, &a, &b);
if (a > b)
{
printf(“A is Big”);
}
}
11/29/2020 10
Basic Relational Operators
1. Basic Logic Operators can be used to combine more than one
condition in an “if” statement or invert the result of a condition
2. The 3 Basic Logic Operators are
&& , || , !
(and, or, not)
11/29/2020 11
1. Basic Relational Operators can be used in C to make “if” statement
conditions more useful
2. The 6 Basic Relational Operators are
> , <, >= , <= , == , !=
(greater than, less than, greater than or equal to, less than or equal
to, equal to, not equal to)
Basic Logic Operators
The if else statement
It is used to execute some statements when the condition is true and execute some other
statements when the condition is false depending on the logical test.
11/29/2020 12
Syntax:
if ( condition )
{
statement 1 ; (if the condition is true this statement will be executed)
}
else
{
statement 2 ; (if the condition is false this statement will be executed)
}
Test Condition
Executable X - Statement
True
Executable Y - Statement
False
if else statements
if (result >= 50)
{
printf ( “ Passedn “ ) ;
printf ( “ Congratulationsn “ );
}
else
{
printf ( “ Failedn “ ) ;
printf ( “ Good luck in the resitsn “ ) ;
}
11/29/2020 13
// Biggest of Two Numbers
#include <stdio.h>
void main()
{
int a, b;
printf(“Enter the A and B Value:n”);
scanf(“%d,%d”, &a, &b);
if (a > b)
{
printf(“A is Big”);
}
else
{
printf(“B is Big”);
}
}
11/29/2020 14
// Given Number is ODD or EVEN Number
#include <stdio.h>
void main()
{
int n;
printf(“Enter the Number:n”);
scanf(“%d”, &n);
if (n % 2 == 0)
{
printf(“Given Number is Even Number”);
}
else
{
printf(“Given Number is Odd Number”);
}
}
11/29/2020 15
Nested if….. else statement
11/29/2020 16
Syntax:
if ( condition 1)
{
if ( condition 2)
statement 1 ;
else
statement 2 ;
}
else
{
if (condition 3)
statement 3;
else
statement 4;
}
when a series of if…else statements are occurred in a program, we can write an entire if…else
statement in another if…else statement called nesting
11/29/2020 17
Test Condition_1
Executable X2 - Statement
Test Condition_2
Executable X1 - Statement
Test Condition_3
Executable X4 - Statement Executable X3 - Statement
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
// Biggest of Three Numbers
#include<stdio.h>
void main()
{
int a, b, c;
printf(“Enter the Three Numbers:n”);
scanf(“%d%d%d”,&a,&b,&c);
if (a > b)
{
if (a > c)
printf(“A is Big”);
else
printf(“C is Big”);
}
else
{
if (b > c)
printf(“B is Big”);
else
printf(“C is Big”);
}
}
11/29/2020 18
else if Ladder or Multiple if else Statements
11/29/2020 19
Syntax:
if (condition_1)
executed statement_1;
else if (condition_2)
executed statement_2;
else if (condition_3)
executed statement_3;
----------------------
----------------------
else if (condition_n)
executed statement_n;
else
executed statement_x;
When a series of decisions are involved we have to use more than one if – else statement called as
multiple if’s. Multiple if – else statements are much faster than a series of if – else statements, since the if
structure is exited when any one of the condition is satisfied.
11/29/2020 20
Test Condition_1
Test Condition_2
Exec. Stat_1
Test Condition_3
TRUE
Test Condition_n
Exec. Stat_2
Exec. Stat_3
Exec. Stat_nExec. Stat_X
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
else if Ladder
if (result >= 75)
printf ( “ Passed: Grade An “ ) ;
else if (result >= 60)
printf ( “ Passed: Grade Bn “ ) ;
else if (result >= 45)
printf ( “ Passed: Grade Cn “ ) ;
else
printf ( “ Failedn “ ) ;
11/29/2020 21
main()
{
int n1,n2;
int val;
char op;
printf("Enter a simple expression ");
scanf("%d%c%d",&n1,&op,&n2);
if(op == '+')
val = n1 + n2;
else if(op == '-')
val = n1 - n2;
else if(op == '/')
val = n1 / n2;
else if(op == '*')
val = n1 * n2;
11/29/2020 22
else
{
printf(“?? operator %cn",op);
exit(1);
}
printf("%d%c%d = %dn",n1,op,n2);
}
/*This program reads in a simple expression with a very restricted format and prints out its value. */
Sample Program
Write a program to calculate the sales commission for the
data given below:
Sales value (Rs) Commission(%)
Less than 1000 No commission
Above 1000 but below 2000 5% of sales
Above 2000 but below 5000 8% of sales
Above 5000 10% of sales
11/29/2020 23
#include<stdio.h>
#include<conio.h>
void main()
{
float sales, com;
printf(“Enter the sales value :”);
scanf(“%f”, &sales);
if(sales<=1000)
com = 0;
else if(sales>1000 && sales <=2000)
com = sales*5/100;
else if(sales>2000 && sales <=5000)
com = sales*5/100;
else
com = sales * 10/100;
printf(“The commission for the sales value %f is %f”, sales, com);
}
11/29/2020 24
THE SWITCH STATEMENT
• The control statements which allow us to make a decision from the number of choices is called switch (or)
Switch-case statement.
• It is a multi way decision statement, it test the given variable (or) expression against a list of case value.
11/29/2020 25
switch (expression)
{
case constant 1:
simple statement (or)
compound statement;
case constant 2:
simple statement (or)
compound statement;
case constant 3:
simple statement (or)
compound statement;
}
switch (expression)
{
case constant 1:
simple statement (or)
compound statement;
case constant 2:
simple statement (or)
compound statement;
default :
simple statement (or)
compound statement;
}
Example Without Break Statement
#include<stdio.h>
void main ()
{
int num1,num2,choice;
printf(“Enter the Two Numbers:n”);
scanf(“%d%d”,&num1,&num2);
printf(“1 -> Additionn””);
printf(“2->Subtractionn”);
printf(“3->Multiplicationn”);
printf(“4->Divisionn”);
printf(“Enter your Choice:n”);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
Printf(“Sum is %dn”, num1+num2);
case 2:
Printf(“Diif. is %dn”, num1-num2);
case 3:
Printf(“Product is %dn”, num1*num2);
case 4:
Printf(“Division is %dn”, num1/num2);
default:
printf (“Invalid Choice…..n”);
}
} 11/29/2020 26
#include<stdio.h>
void main ()
{
int num1,num2,choice;
printf(“Enter the Two Numbers:n”);
scanf(“%d%d”,&num1,&num2);
printf(“1 -> Additionn””);
printf(“2->Subtractionn”);
printf(“3->Multiplicationn”);
printf(“4->Divisionn”);
printf(“Enter your Choice:n”);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
printf(“Sum is %dn”, num1+num2);
break;
case 2:
printf(“Diif. is %dn”, num1-num2);
break;
case 3:
printf(“Product is %dn”, num1*num2);
break;
case 4:
printf(“Division is %dn”, num1/num2);
break;
default:
printf (“Invalid Choice…..n”);
}
}
Example With Break Statement
Rules for Switch
• The expression in the switch statement must be an integer or character constant.
• No real numbers are used in an expression.
• The default is optional and can be placed anywhere, but usually placed at end.
• The case keyword must be terminated with colon (:);
• No two case constant are identical.
• The values of switch expression is compared with case constant in the order specified i.e. from top to bottom.
• The compound statements are no need to enclose within pair of braces.
• Integer Expression used in different case statements can be specified in any order.
• A switch may occur within another switch, but it is rarely done. Such statements are called as nested switch
statements.
• The switch statement is very useful while writing menu driven programs.
11/29/2020 27
Limitations of using a switch statement
• Only One variable can be tested with the available case statements with the values
stored in them (i.e., you cannot use relational operators and combine two or more
conditions as in the case of if or if – else statements).
• Floating – point, double, and long type variables cannot be used as cases in the
switch statement.
• Multiple statements can be executed in each case without the use of pair of braces
as in the case of if or if – else statement.
11/29/2020 28

Contenu connexe

Tendances

Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & ElseAbdullah Bhojani
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
PL SQL Quiz | PL SQL Examples
PL SQL Quiz |  PL SQL ExamplesPL SQL Quiz |  PL SQL Examples
PL SQL Quiz | PL SQL ExamplesShubham Dwivedi
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If conditionyarkhosh
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
COM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingCOM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingHemantha Kulathilake
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 

Tendances (20)

Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
SQL MCQ
SQL MCQSQL MCQ
SQL MCQ
 
Branching in C
Branching in CBranching in C
Branching in C
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
PL SQL Quiz | PL SQL Examples
PL SQL Quiz |  PL SQL ExamplesPL SQL Quiz |  PL SQL Examples
PL SQL Quiz | PL SQL Examples
 
3. control statement
3. control statement3. control statement
3. control statement
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
Unit 2
Unit 2Unit 2
Unit 2
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
Lec 10
Lec 10Lec 10
Lec 10
 
Ansi c
Ansi cAnsi c
Ansi c
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
COM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingCOM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & Branching
 
Bsit1
Bsit1Bsit1
Bsit1
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 

Similaire à Decision Making and Branching

Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVINGGOWSIKRAJAP
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptSanjjaayyy
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptxishaparte4
 
Branching statements
Branching statementsBranching statements
Branching statementsArunMK17
 
Here is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docxHere is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docxtrappiteboni
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxjacksnathalie
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin cVikash Dhal
 

Similaire à Decision Making and Branching (20)

Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Branching statements
Branching statementsBranching statements
Branching statements
 
CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
Here is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docxHere is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docx
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docx
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 

Plus de Munazza-Mah-Jabeen (20)

Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
 
Object-Oriented Software
Object-Oriented SoftwareObject-Oriented Software
Object-Oriented Software
 
Templates and Exceptions
 Templates and Exceptions Templates and Exceptions
Templates and Exceptions
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
More About Strings
More About StringsMore About Strings
More About Strings
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Lists and Tuples
Lists and TuplesLists and Tuples
Lists and Tuples
 
Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
 
Functions
FunctionsFunctions
Functions
 
Pointers
PointersPointers
Pointers
 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
 

Dernier

Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphNetziValdelomar1
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 

Dernier (20)

Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a Paragraph
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 

Decision Making and Branching

  • 2. Control Statements A program consists of a number of statements which are usually executed in sequence. Programs can be much more powerful if we can control the order in which statements are run. Statements fall into three general types; 1. Assignment, where values, usually the results of calculations, are stored in variables. 2. Input / Output, data is read in or printed out. 3. Control, the program makes a decision about what to do next. 11/29/2020 2
  • 3. Control Statements Control statements in C are used to write powerful programs by: 1. Repeating important sections of the program. 2. Selecting between optional sections of a program. 11/29/2020 3
  • 4. Control Statements Control statements Selection Statements The if else statement The switch statements Iteration statements The while loop & Do while loop The for loop The break statement Continue statement Goto statement 11/29/2020 4
  • 5. Control Statements cont. Structure Meaning Example Sequential It means the instructions are carried out in sequence a+=5; a=a+b; Selection Sequence of the instruction are determined by the result of the condition if (x>y) a = a+1; else a= a-1; Iteration Here the statements are repeatedly executed while (a<=10) printf (“%d” , a); Encapsulation Here the an iteration statement can have selection inside it or vice versa while (a<=10) { if(a>5) printf (%d” , a); } 11/29/2020 5 ‘C’ language provides four general structure by which statements can be executed
  • 7. Types of Selection Statement 1. Simple if Selection statement 2. Nested if Selection statement 3. if else Selection statement 4. Nested if else Selection statement 5. else if ladder Selection statement 11/29/2020 7
  • 8. Simple if Selection statement It is used to control the flow of execution of the statements and also to test logically whether the condition is true or false. if the condition is true then the statement following the “if “ is executed if it is false then the statement is skipped. 11/29/2020 8 Syntax: if ( condition ) { statement ; } Test Condition Executable X - Statement True
  • 9. Selection Statement Properties of an if statement: 1. if the condition is true then the simple or compound statements are executed. 2. If the condition is false it will skip the statement. 3. The condition is given in parenthesis and must be evaluated as true or false. 4. If a compound structure is provided, it must be enclosed in opening and closing braces 11/29/2020 9
  • 10. //Biggest of Two Numbers #include <stdio.h> void main() { int a, b; printf(“Enter the A and B Value:n”); scanf(“%d,%d”, &a, &b); if (a > b) { printf(“A is Big”); } } 11/29/2020 10
  • 11. Basic Relational Operators 1. Basic Logic Operators can be used to combine more than one condition in an “if” statement or invert the result of a condition 2. The 3 Basic Logic Operators are && , || , ! (and, or, not) 11/29/2020 11 1. Basic Relational Operators can be used in C to make “if” statement conditions more useful 2. The 6 Basic Relational Operators are > , <, >= , <= , == , != (greater than, less than, greater than or equal to, less than or equal to, equal to, not equal to) Basic Logic Operators
  • 12. The if else statement It is used to execute some statements when the condition is true and execute some other statements when the condition is false depending on the logical test. 11/29/2020 12 Syntax: if ( condition ) { statement 1 ; (if the condition is true this statement will be executed) } else { statement 2 ; (if the condition is false this statement will be executed) } Test Condition Executable X - Statement True Executable Y - Statement False
  • 13. if else statements if (result >= 50) { printf ( “ Passedn “ ) ; printf ( “ Congratulationsn “ ); } else { printf ( “ Failedn “ ) ; printf ( “ Good luck in the resitsn “ ) ; } 11/29/2020 13
  • 14. // Biggest of Two Numbers #include <stdio.h> void main() { int a, b; printf(“Enter the A and B Value:n”); scanf(“%d,%d”, &a, &b); if (a > b) { printf(“A is Big”); } else { printf(“B is Big”); } } 11/29/2020 14
  • 15. // Given Number is ODD or EVEN Number #include <stdio.h> void main() { int n; printf(“Enter the Number:n”); scanf(“%d”, &n); if (n % 2 == 0) { printf(“Given Number is Even Number”); } else { printf(“Given Number is Odd Number”); } } 11/29/2020 15
  • 16. Nested if….. else statement 11/29/2020 16 Syntax: if ( condition 1) { if ( condition 2) statement 1 ; else statement 2 ; } else { if (condition 3) statement 3; else statement 4; } when a series of if…else statements are occurred in a program, we can write an entire if…else statement in another if…else statement called nesting
  • 17. 11/29/2020 17 Test Condition_1 Executable X2 - Statement Test Condition_2 Executable X1 - Statement Test Condition_3 Executable X4 - Statement Executable X3 - Statement TRUE TRUE TRUE FALSE FALSE FALSE
  • 18. // Biggest of Three Numbers #include<stdio.h> void main() { int a, b, c; printf(“Enter the Three Numbers:n”); scanf(“%d%d%d”,&a,&b,&c); if (a > b) { if (a > c) printf(“A is Big”); else printf(“C is Big”); } else { if (b > c) printf(“B is Big”); else printf(“C is Big”); } } 11/29/2020 18
  • 19. else if Ladder or Multiple if else Statements 11/29/2020 19 Syntax: if (condition_1) executed statement_1; else if (condition_2) executed statement_2; else if (condition_3) executed statement_3; ---------------------- ---------------------- else if (condition_n) executed statement_n; else executed statement_x; When a series of decisions are involved we have to use more than one if – else statement called as multiple if’s. Multiple if – else statements are much faster than a series of if – else statements, since the if structure is exited when any one of the condition is satisfied.
  • 20. 11/29/2020 20 Test Condition_1 Test Condition_2 Exec. Stat_1 Test Condition_3 TRUE Test Condition_n Exec. Stat_2 Exec. Stat_3 Exec. Stat_nExec. Stat_X TRUE TRUE TRUE FALSE FALSE FALSE FALSE
  • 21. else if Ladder if (result >= 75) printf ( “ Passed: Grade An “ ) ; else if (result >= 60) printf ( “ Passed: Grade Bn “ ) ; else if (result >= 45) printf ( “ Passed: Grade Cn “ ) ; else printf ( “ Failedn “ ) ; 11/29/2020 21
  • 22. main() { int n1,n2; int val; char op; printf("Enter a simple expression "); scanf("%d%c%d",&n1,&op,&n2); if(op == '+') val = n1 + n2; else if(op == '-') val = n1 - n2; else if(op == '/') val = n1 / n2; else if(op == '*') val = n1 * n2; 11/29/2020 22 else { printf(“?? operator %cn",op); exit(1); } printf("%d%c%d = %dn",n1,op,n2); } /*This program reads in a simple expression with a very restricted format and prints out its value. */
  • 23. Sample Program Write a program to calculate the sales commission for the data given below: Sales value (Rs) Commission(%) Less than 1000 No commission Above 1000 but below 2000 5% of sales Above 2000 but below 5000 8% of sales Above 5000 10% of sales 11/29/2020 23
  • 24. #include<stdio.h> #include<conio.h> void main() { float sales, com; printf(“Enter the sales value :”); scanf(“%f”, &sales); if(sales<=1000) com = 0; else if(sales>1000 && sales <=2000) com = sales*5/100; else if(sales>2000 && sales <=5000) com = sales*5/100; else com = sales * 10/100; printf(“The commission for the sales value %f is %f”, sales, com); } 11/29/2020 24
  • 25. THE SWITCH STATEMENT • The control statements which allow us to make a decision from the number of choices is called switch (or) Switch-case statement. • It is a multi way decision statement, it test the given variable (or) expression against a list of case value. 11/29/2020 25 switch (expression) { case constant 1: simple statement (or) compound statement; case constant 2: simple statement (or) compound statement; case constant 3: simple statement (or) compound statement; } switch (expression) { case constant 1: simple statement (or) compound statement; case constant 2: simple statement (or) compound statement; default : simple statement (or) compound statement; }
  • 26. Example Without Break Statement #include<stdio.h> void main () { int num1,num2,choice; printf(“Enter the Two Numbers:n”); scanf(“%d%d”,&num1,&num2); printf(“1 -> Additionn””); printf(“2->Subtractionn”); printf(“3->Multiplicationn”); printf(“4->Divisionn”); printf(“Enter your Choice:n”); scanf(“%d”, &choice); switch(choice) { case 1: Printf(“Sum is %dn”, num1+num2); case 2: Printf(“Diif. is %dn”, num1-num2); case 3: Printf(“Product is %dn”, num1*num2); case 4: Printf(“Division is %dn”, num1/num2); default: printf (“Invalid Choice…..n”); } } 11/29/2020 26 #include<stdio.h> void main () { int num1,num2,choice; printf(“Enter the Two Numbers:n”); scanf(“%d%d”,&num1,&num2); printf(“1 -> Additionn””); printf(“2->Subtractionn”); printf(“3->Multiplicationn”); printf(“4->Divisionn”); printf(“Enter your Choice:n”); scanf(“%d”, &choice); switch(choice) { case 1: printf(“Sum is %dn”, num1+num2); break; case 2: printf(“Diif. is %dn”, num1-num2); break; case 3: printf(“Product is %dn”, num1*num2); break; case 4: printf(“Division is %dn”, num1/num2); break; default: printf (“Invalid Choice…..n”); } } Example With Break Statement
  • 27. Rules for Switch • The expression in the switch statement must be an integer or character constant. • No real numbers are used in an expression. • The default is optional and can be placed anywhere, but usually placed at end. • The case keyword must be terminated with colon (:); • No two case constant are identical. • The values of switch expression is compared with case constant in the order specified i.e. from top to bottom. • The compound statements are no need to enclose within pair of braces. • Integer Expression used in different case statements can be specified in any order. • A switch may occur within another switch, but it is rarely done. Such statements are called as nested switch statements. • The switch statement is very useful while writing menu driven programs. 11/29/2020 27
  • 28. Limitations of using a switch statement • Only One variable can be tested with the available case statements with the values stored in them (i.e., you cannot use relational operators and combine two or more conditions as in the case of if or if – else statements). • Floating – point, double, and long type variables cannot be used as cases in the switch statement. • Multiple statements can be executed in each case without the use of pair of braces as in the case of if or if – else statement. 11/29/2020 28