SlideShare a Scribd company logo
1 of 34
Programming in
C
1
Flow of
Control
 Flow of control
The order in which statements are
executed
 Transfer of control
When the next
statement executed is
not the next one in
sequence
2
Flow of
Control
3
 Control structures
combination of individual statements into a logical
unit that regulates the flow of execution in a
program or function
Sequence
Selection (Making Decisions)
Repetition (Looping)
Boolean
Expressions
4
 Evaluate to true or false
 Forms
Relational expression: <expr> <relational operator> <expr>
 Examples:
7 < 5
a + b > 6
Logical expression: <Boolean expr> <logical operator> <Boolean
expr>
 Examples:
(x < 7) && (y > 3)
Relational
Operators
5
Standard Algebraic
Relational Operator
C Relational
Operator
C Condition
Example Meaning of C Condition
Inequality
< < x < y x is less than y
 <= x <= y x is less than or equal to y
> > x > y x is greater than y
 >= x >= y x is greater than or equal to y
Equality
= == x == y x is equal to y
 != x != y x is not equal to y
4th: Ch 4 p.
46
3rd: Ch 5 p.
46
Logical Operators (Compound
Relationals)
6
 && (logical AND)
Returns true if both conditions are
true
 || (logical OR)
Returns true if either of its conditions is true
 ! (logical NOT, logical negation)
Is a unary operator, only takes one operand
following
Reverses the truth/falsity of its condition
Returns true when its condition is false
Ch 6 p.
72
Logical Operators Truth
Table
7
P Q P && Q P || Q !P
true true true true false
true false false true false
false true false true true
false false false false true
Precedence of
Operators
8
1. (), []
2. Unary +, unary -, !,
++, --
3. Type casting
4. * , / , %
5. + , -
6. <, <=, >, >=
7. ==, !=
8. &&
9. ||
10. =
The if Selection
Structure
 Selection structure
used when we want the computer to choose
between two alternative courses of action
9
The if Selection
Structure
 if
Statement
true block
Boolean
Expression
10
true
false
The if Selection
Structure
11
General form of if:
if (Boolean Expression)
{
statement1;
statement2;
...
}
The if-else Selection
Structure
12
 if
Only performs an action if the condition is
true
 if-else
A different action is performed when
condition is true and when condition is false
if-else Selection
Structure
false true
if-else
statement
false block
Boolean
Expression
13
true block
The if-else Selection
Structure
14
General form of if-else:
if (expression)
{
statement1A;
statement2A;
...
}
else
{
statement1B;
statement2B;
...
}
The if-else Selection
Structure
 Nested if-else structures
Test for multiple cases by placing if-else
selection structures inside if-else selection
structures.
15
Nested if-else
Structures
16
The if-else-if
Construct
Once a condition is met, the rest of the statements are
skipped
17
The if-else-if
Construct
The standard way to indent the previous
code is
18
The if-else Selection
Structure
 Compound statement:
Set of statements within a pair of
braces
Example:
19
The if-else Selection
Structure
–Without the braces, only one statement is executed.
e.g. given the following code:
• The statement,
will be executed independent of the value of grade.
• The statement,
will execute only if grade is
greater than or equal to 90.
20
The dangling
else
Note: the compiler matches an else with the closest
unmatched if The above will be treated as
21
The dangling
else
If the else is to match the outer if, use
braces.
22
if-else
Construct
 To avoid confusion, and possible errors, it is
best to use braces even for single statements.
However, code will be longer
23
Conditional
s
 C uses an integer to represent Boolean
values
Zero is interpreted as false
Any other integer value is interpreted as
true
24
Conditional
s
 is not a syntax error in
C.
The expression, n = 0, assigns zero to n and the
value of the expression is 0. Zero is interpreted as
false, and the false branch of the if statement will
be taken.
 is not a syntax error in
C.
The expression assigns 5 to n. 5 is interpreted
as true, and the true branch of the if statement
will be taken.
25
Conditional
s
 Remember to use the == operator to test for
equality.
 To help catch the error when the equality check
involves a constant, put the constant on the
left hand side of the ==.
For example, use
instead of
Since is not a valid assignment in C, the
compiler will detect this error when == is
intended.
26
The switch Multiple-Selection
Structure
 switch
Useful when variable or expression is tested for
multiple values
Consists of a series of case labels and an optional
default case
27
The switch Multiple-Selection Structure
With Breaks
case a case a
action(s)
brea
k
case b case b
action(s)
brea
k
case z case z
action(s)
brea
k
default
action(s)
28
The switch Multiple-Selection Structure
Without Breaks
case a case a
action(s)
case b case b
action(s)
case z case z
action(s)
default
action(s)
29
switch Statement
Syntax
switch (switch_expression)
{
case constant1:
statementSequence1
break;
case constant2:
statementSequence2
break;
…
case constantN:
statementSequenceN
break;
default:
defaultStmtSequence
}
30
switch
Statement
 The switch_expression is compared against the
values
constant1, constant2, …, constantN
constant1, constant2, …, constantN must be
simple constants or constant expressions.
 Can be a char or an int
 Best to use the same type constant as the switch
expression
If not, a type conversion will be done.
31
switch Statement
Reminder
 The switch statement ends
break statement
end of the switch statement
 When executing the statements after a case
label, it continues to execute until it reaches a
break statement or the end of the switch.
 If you omit the break statements, then after
executing the code for one case, the computer
will continue to execute the code for the next
case.
32
Example
of
switch
33
Programming in
C
TH E EN D
34

More Related Content

Similar to Ch05-converted.pptx

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1
sotlsoc
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
PRN USM
 

Similar to Ch05-converted.pptx (20)

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Selection
SelectionSelection
Selection
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
Ch04
Ch04Ch04
Ch04
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test Bank
 
M C6java5
M C6java5M C6java5
M C6java5
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 

More from ShivamChaturvedi67 (15)

ddc cinverter control design process.ppt
ddc cinverter control design process.pptddc cinverter control design process.ppt
ddc cinverter control design process.ppt
 
ffCCMPFCInductorDesignwithPowderCore.ppt
ffCCMPFCInductorDesignwithPowderCore.pptffCCMPFCInductorDesignwithPowderCore.ppt
ffCCMPFCInductorDesignwithPowderCore.ppt
 
transformer-and-dc-motor control designs
transformer-and-dc-motor control designstransformer-and-dc-motor control designs
transformer-and-dc-motor control designs
 
magnetism_qr.pptx
magnetism_qr.pptxmagnetism_qr.pptx
magnetism_qr.pptx
 
Update7622.pptx
Update7622.pptxUpdate7622.pptx
Update7622.pptx
 
_8_EE462L_Fall2011_DC_DC_BuckBoost_PPT.ppt
_8_EE462L_Fall2011_DC_DC_BuckBoost_PPT.ppt_8_EE462L_Fall2011_DC_DC_BuckBoost_PPT.ppt
_8_EE462L_Fall2011_DC_DC_BuckBoost_PPT.ppt
 
02_Design.pptx
02_Design.pptx02_Design.pptx
02_Design.pptx
 
Education_selecting key discovery tools for education research_v1_2021.pptx
Education_selecting key discovery tools for education research_v1_2021.pptxEducation_selecting key discovery tools for education research_v1_2021.pptx
Education_selecting key discovery tools for education research_v1_2021.pptx
 
java01.ppt
java01.pptjava01.ppt
java01.ppt
 
android.ppt
android.pptandroid.ppt
android.ppt
 
1_5_Python_to_Java.pptx
1_5_Python_to_Java.pptx1_5_Python_to_Java.pptx
1_5_Python_to_Java.pptx
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Lecture-3.ppt
Lecture-3.pptLecture-3.ppt
Lecture-3.ppt
 
Lecture-2.ppt
Lecture-2.pptLecture-2.ppt
Lecture-2.ppt
 
Lecture-1.ppt
Lecture-1.pptLecture-1.ppt
Lecture-1.ppt
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 

Recently uploaded (20)

Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 

Ch05-converted.pptx

  • 2. Flow of Control  Flow of control The order in which statements are executed  Transfer of control When the next statement executed is not the next one in sequence 2
  • 3. Flow of Control 3  Control structures combination of individual statements into a logical unit that regulates the flow of execution in a program or function Sequence Selection (Making Decisions) Repetition (Looping)
  • 4. Boolean Expressions 4  Evaluate to true or false  Forms Relational expression: <expr> <relational operator> <expr>  Examples: 7 < 5 a + b > 6 Logical expression: <Boolean expr> <logical operator> <Boolean expr>  Examples: (x < 7) && (y > 3)
  • 5. Relational Operators 5 Standard Algebraic Relational Operator C Relational Operator C Condition Example Meaning of C Condition Inequality < < x < y x is less than y  <= x <= y x is less than or equal to y > > x > y x is greater than y  >= x >= y x is greater than or equal to y Equality = == x == y x is equal to y  != x != y x is not equal to y 4th: Ch 4 p. 46 3rd: Ch 5 p. 46
  • 6. Logical Operators (Compound Relationals) 6  && (logical AND) Returns true if both conditions are true  || (logical OR) Returns true if either of its conditions is true  ! (logical NOT, logical negation) Is a unary operator, only takes one operand following Reverses the truth/falsity of its condition Returns true when its condition is false Ch 6 p. 72
  • 7. Logical Operators Truth Table 7 P Q P && Q P || Q !P true true true true false true false false true false false true false true true false false false false true
  • 8. Precedence of Operators 8 1. (), [] 2. Unary +, unary -, !, ++, -- 3. Type casting 4. * , / , % 5. + , - 6. <, <=, >, >= 7. ==, != 8. && 9. || 10. =
  • 9. The if Selection Structure  Selection structure used when we want the computer to choose between two alternative courses of action 9
  • 10. The if Selection Structure  if Statement true block Boolean Expression 10 true false
  • 11. The if Selection Structure 11 General form of if: if (Boolean Expression) { statement1; statement2; ... }
  • 12. The if-else Selection Structure 12  if Only performs an action if the condition is true  if-else A different action is performed when condition is true and when condition is false
  • 13. if-else Selection Structure false true if-else statement false block Boolean Expression 13 true block
  • 14. The if-else Selection Structure 14 General form of if-else: if (expression) { statement1A; statement2A; ... } else { statement1B; statement2B; ... }
  • 15. The if-else Selection Structure  Nested if-else structures Test for multiple cases by placing if-else selection structures inside if-else selection structures. 15
  • 17. The if-else-if Construct Once a condition is met, the rest of the statements are skipped 17
  • 18. The if-else-if Construct The standard way to indent the previous code is 18
  • 19. The if-else Selection Structure  Compound statement: Set of statements within a pair of braces Example: 19
  • 20. The if-else Selection Structure –Without the braces, only one statement is executed. e.g. given the following code: • The statement, will be executed independent of the value of grade. • The statement, will execute only if grade is greater than or equal to 90. 20
  • 21. The dangling else Note: the compiler matches an else with the closest unmatched if The above will be treated as 21
  • 22. The dangling else If the else is to match the outer if, use braces. 22
  • 23. if-else Construct  To avoid confusion, and possible errors, it is best to use braces even for single statements. However, code will be longer 23
  • 24. Conditional s  C uses an integer to represent Boolean values Zero is interpreted as false Any other integer value is interpreted as true 24
  • 25. Conditional s  is not a syntax error in C. The expression, n = 0, assigns zero to n and the value of the expression is 0. Zero is interpreted as false, and the false branch of the if statement will be taken.  is not a syntax error in C. The expression assigns 5 to n. 5 is interpreted as true, and the true branch of the if statement will be taken. 25
  • 26. Conditional s  Remember to use the == operator to test for equality.  To help catch the error when the equality check involves a constant, put the constant on the left hand side of the ==. For example, use instead of Since is not a valid assignment in C, the compiler will detect this error when == is intended. 26
  • 27. The switch Multiple-Selection Structure  switch Useful when variable or expression is tested for multiple values Consists of a series of case labels and an optional default case 27
  • 28. The switch Multiple-Selection Structure With Breaks case a case a action(s) brea k case b case b action(s) brea k case z case z action(s) brea k default action(s) 28
  • 29. The switch Multiple-Selection Structure Without Breaks case a case a action(s) case b case b action(s) case z case z action(s) default action(s) 29
  • 30. switch Statement Syntax switch (switch_expression) { case constant1: statementSequence1 break; case constant2: statementSequence2 break; … case constantN: statementSequenceN break; default: defaultStmtSequence } 30
  • 31. switch Statement  The switch_expression is compared against the values constant1, constant2, …, constantN constant1, constant2, …, constantN must be simple constants or constant expressions.  Can be a char or an int  Best to use the same type constant as the switch expression If not, a type conversion will be done. 31
  • 32. switch Statement Reminder  The switch statement ends break statement end of the switch statement  When executing the statements after a case label, it continues to execute until it reaches a break statement or the end of the switch.  If you omit the break statements, then after executing the code for one case, the computer will continue to execute the code for the next case. 32