31
Control Structures

Lecture 3 control_structures_i

Embed Size (px)

Citation preview

  1. 1. Control Structures
  2. 2. OBJECTIVES In this Lecture you will learn how to: Make decisions Using : simple if Ifelse statement Ifelse if statement Use logical operators 2 Intro to Programming with C++ Sir Noble
  3. 3. Program Control and Control Structures An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which the actions execute Program control Specifying the order in which statements (actions) execute. Control Structures Simple control statements 3 Intro to Programming with C++ Sir Noble
  4. 4. The Simple if Statement Allows statements to be conditionally executed or skipped over Models the way we mentally evaluate situations: "If I do not have the minimum CGPA required, I will not graduate." "If I have codeblocks installed on my machine, I will be able to write and debug simple c++ programs." If condition is true, body of the if statement executes If condition is false, body of the if statement does not execute 4 Intro to Programming with C++ Sir Noble
  5. 5. The Logical flow of Simple If Decision 5 Intro to Programming with C++ Sir Noble
  6. 6. Decision Making: Equality and Relational Operators The if Statement Syntax Boolean expression is any C++ expression that can be evaluated Statement is any C++ statement or block of statements that you want to execute when the boolean expression evaluates as true. 6 if ( boolean expression ) Statement; Intro to Programming with C++ Sir Noble
  7. 7. 7 Simpe If Statement Intro to Programming with C++ Sir Noble
  8. 8. 8 Example of Simpe If Statement Intro to Programming with C++ Sir Noble
  9. 9. 9 Example of Simpe If Statement Intro to Programming with C++ Sir Noble
  10. 10. Expanding the if Statement To execute more than one statement as part of an if statement, enclose them in curly braces{ }: if (gpa >= 3.75) { cout