1 / 13

C onditional C ontrol F low

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.

kita
Télécharger la présentation

C onditional C ontrol F low

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop

  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

  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

  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

  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

  6. Truth Tables for the Conditional Operators SACS Programming Fundamentals Workshop

  7. 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

  8. 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

  9. 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

  10. 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

  11. The ‘Switch’ Statement • Some of the confusions in the Switch keywords: • Switch • Case • Break • Default SACS Programming Fundamentals Workshop

  12. 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

  13. Any Questions??? SACS Programming Fundamentals Workshop

More Related