130 likes | 300 Views
C onditional C ontrol F low. By Muhammad Ahsan Q adar. 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. Flow of Control.
E N D
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop
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
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
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
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
Truth Tables for the Conditional Operators SACS Programming Fundamentals Workshop
True False condition statement 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; } SACS Programming Fundamentals Workshop
False True condition statement1 statement1 The ‘if-else’ Statement • Perform if-action if a condition is true. Otherwise, perform else-action. • Syntax: if (conditional expression){ statement1 } else{ statement2 } SACS Programming Fundamentals Workshop
The ‘else-if’ Statement • You can connect conditional constructs to form longer sequences of conditional tests: • Syntax: if(conditional expression1){ statement1 } else if (expression2){ statement2 } else if (expression3){ statement3 } else{ statement4 } SACS Programming Fundamentals Workshop
Points to remember… • An else is associated with the closest unassociated if. if (expression1){ if (expression2) statement2 else statement3 } Correct Interpretation if (expression1) { if (expression2) statement2 else statement3 } if (expression1) { if (expression2) statement2 } else statement3 Just as parentheses modify the order of evaluation of expressions...braces modify how statements are executed. SACS Programming Fundamentals Workshop
The ‘Switch’ Statement • Some of the confusions in the Switch keywords: • Switch • Case • Break • Default SACS Programming Fundamentals Workshop
True False case 1 ...; break; True False ...; break; True False ...; break; The ‘switch’ Statement • Performs actions based on a series of tests of the same variable. • Form: switch (conditionalexpression) { case const-expr: statements case const-expr: statements case const-expr: statements default: statements } • The breakstatement 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 2 case 3 SACS Programming Fundamentals Workshop
Any Questions??? SACS Programming Fundamentals Workshop