Download presentation
Presentation is loading. Please wait.
Published byShon Ellis Modified over 9 years ago
1
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1
2
Introduction Here we would discuss various constructs of C language which would be used to control the flow of the program. “Control flow” is the order in which statements are executed. SACS Programming Fundamentals Workshop 2
3
Flow of Control There are three flow of control in the programming languages: –The sequential control flow –The conditional control flow –The iteration control flow We will be discussing the Conditional Control Flow in this lecture today. SACS Programming Fundamentals Workshop 3
4
What is a Condition??? What is the value of a conditional expression??? How can we facilitate the conditional expression with Boolean operators??? Use of Flow Charts… What is short circuiting??? SACS Programming Fundamentals Workshop 4
5
Condition In parentheses is a condition, also called a “logical” or “Boolean” expression. Made up of variables, constants, arithmetic expressions, and the relational operators: Math symbols:, , =, in C:, >=, ==, != SACS Programming Fundamentals Workshop 5
6
Truth Tables for the Conditional Operators pqp && q TTT TFF FTF FFF pqp || q TTT TFT FTT FFF SACS Programming Fundamentals Workshop 6
7
The ‘if’ statement Performs an action if a condition is true. The condition, which is a C expression, evaluates to zero (false) or nonzero (true). Syntax: if (conditional expression){ statement; } condition statement TrueFalse SACS Programming Fundamentals Workshop 7
8
The ‘if-else’ Statement Perform if-action if a condition is true. Otherwise, perform else-action. Syntax: if (conditional expression){ statement 1 } else{ statement 2 } condition statement 1 True False SACS Programming Fundamentals Workshop 8
9
The ‘else-if’ Statement You can connect conditional constructs to form longer sequences of conditional tests: Syntax: if(conditional expression 1 ){ statement 1 } else if (expression 2 ){ statement 2 } else if (expression 3 ){ statement 3 } else{ statement 4 } SACS Programming Fundamentals Workshop 9
10
Points to remember… An else is associated with the closest unassociated if. if (expression 1 ){ if (expression 2 ) statement 2 else statement 3 } if (expression1) { if (expression2) statement2 else statement3 } if (expression1) { if (expression2) statement2 } else statement3 Correct Interpretation Just as parentheses modify the order of evaluation of expressions... braces modify how statements are executed. SACS Programming Fundamentals Workshop 10
11
The ‘Switch’ Statement Some of the confusions in the Switch keywords: – Switch – Case – Break – Default SACS Programming Fundamentals Workshop 11
12
The ‘switch’ Statement Performs actions based on a series of tests of the same variable. Form: switch ( conditional expression ) { case const-expr : statements default: statements } The break statement causes an immediate exit from the switch. Because cases serve only as labels, execution falls through to the next unless there is explicit action to escape. case 1...; break; TrueFalse...; break; TrueFalse...; break; TrueFalse case 2 case 3 SACS Programming Fundamentals Workshop 12
13
Any Questions??? SACS Programming Fundamentals Workshop 13
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.