© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Chapter 4: Control Structures: Selection
Visual C++ Programming: Concepts and Projects
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Operator Precedence Operators Precedence Parentheses () unary
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
3 Control Statements:.
Chapter 4: Control Structures I (Selection)
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Structural Program Development: If, If-Else
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures

© Janice Regan, CMPT 102, Sept Flowcharts  Flowcharts use some basic symbols  To start or end a function  To contain calculations  To make decisions  To connect different parts of an algorithm

© Janice Regan, CMPT 102, Sept Control Structures  Three methods of processing a program  In sequence  Branching  Looping  Branch: Altering the flow of program execution by making a selection or choice  Loop: Altering the flow of program execution by repetition of a particular block of statement(s)

© Janice Regan, CMPT 102, Sept Selection Based on Branching  One-Way Selection  One alternative: “Things to do” if a condition is true  C implementation: if statement  Two-Way Selection  Two alternatives: “Things to do” if the condition is true, a different “things to do” if the condition is false  C implementation: if-else statement  Compound Statements  Allows a group of statements to be considered as a block  That block can replace a single statement in any selection structure

© Janice Regan, CMPT 102, Sept Selection Based on Branching  Multiple Selections  A series of different “things to do” if a series of conditions hold  C implementation: if-elseif-else structure  Alternate C implementation: Nested if statements  Switch Structures  A series of different “things to do” depending on the value of a particular variable

© Janice Regan, CMPT 102, Sept Decisions  Use a decision statement when an action is to be taken only if a particular condition holds  The condition which must hold may be logical or relational expression or a Boolean variable. The value of the condition must be True or False  Each possible path through a condition statement will contain a sequence of steps to be executed  The condition and the sequences of steps that are executed for each outcome of the condition statement form a selection structure.  A selection structure is a type of control structure

© Janice Regan, CMPT 102, Sept Flowchart: one way selection  A simple decision uses a decision box to hold the condition (Boolean value: relational or logical expression)  The sequence of statements is held in a sequence box or boxes condition Statement 1; Statement n; T F ⋮

© Janice Regan, CMPT 102, Sept Example of one-way selection  if statements  An action is taken only if a particular condition is true. The action is described by a single C statement.  Example: if (examScore < 50) fprintf(“Printing address of failing student so they can be contacted to arrange tutoring: is %s”, Address);

© Janice Regan, CMPT 102, Sept Compound/Block Statement  Only one statement following the if statement is part of the if control structure  Must use a compound statement, implemented in C as { }, to include more than one statement in a list of things to do if the condition is true  A compond statement can also be called a "block" statement  You should always use a block statement { }  Even if the block contains just one statement  Using the { } if there is only one statement is a style decision, not a requirement of C

© Janice Regan, CMPT 102, Sept One way selection  Do a series of actions only if a given condition holds  If the condition does not hold skip the actions if ( condition ) { /* Series of actions to be taken */ /* when the condition is TRUE */ action 1; ⋮ action n; }

© Janice Regan, CMPT 102, Sept Compound Statement in Action  Note indenting in this example: if (myScore > yourScore) { printf(“My score was higher than yours “); difference = myScore – yourScore; printf(“I got %d more points than you did”, difference) }

© Janice Regan, CMPT 102, Sept Flowchart: two way selection  A selection structure uses a decision box, and sequence boxes. There may be multiple sequence boxes along each path condition Statement 1; Statement n; T F Statement 1; Statement n; ⋮ ⋮

© Janice Regan, CMPT 102, Sept Example of two-way selection  if-else statements  Choice of two alternate statements based on condition expression  Example: if (examScore > 50) myCourseGrade = “PASS”; else myCourseGrade = “FAIL”;

© Janice Regan, CMPT 102, Sept Compound/Block Statement  Only one statement following the if statement is part of the if control structure  Only one statement following the else statement is part of the control structure  Must use a compound statement { } to include more than one statement  Style Pointer: Each block (after the if and after the else) should have block statement even if the block contains just one statement

© Janice Regan, CMPT 102, Sept Two way selection  Complete one of two possible series of actions  First series of actions is complete if condition is true  Second series of actions is completed if condition is false if (condition) {//Series of actions to be taken when the condition is TRUE action 1; ⋮ action n; } else { // Series of actions to be taken when the condition is FALSE action 1; ⋮ action n; }

© Janice Regan, CMPT 102, Sept if-else Statement Syntax Selection structure for C  Formal syntax:  if ( ) else  Note each alternative is only ONE statement!  To have multiple statements execute in either branch  use compound statement

© Janice Regan, CMPT 102, Sept Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T ⋮ ⋮ ⋮

© Janice Regan, CMPT 102, Sept Example of Multiple selection  if-else statements  Choice of two alternate statements based on condition expression  Example: if (examScore > 80) myCourseGrade = “A”; else if (examScore > 60) myCourseGrade = “C”; else myCourseGrade = “FAIL”;

© Janice Regan, CMPT 102, Sept Compound/Block Statement  Only one statement following the if statement is part of the if control structure  Only one statement following the else statement is part of the control structure  Only one statement following the else if statement of the control structrue  Must use a compound statement { } to include more than one statement  Style Pointer: Each block should have block statement even if it contains one statement

© Janice Regan, CMPT 102, Sept Multiple Selections (else if) if (condition1) {// Series of actions to be taken when condition 1 is TRUE action 1; action n; } else if (condition 2) { // actions to be taken when condition 1 is FALSE and condition2 is TRUE action 1; action n; } else { // Series of actions to be taken when condition 1and condition 2 are FALSE action 1; action n; }

© Janice Regan, CMPT 102, Sept Multiway if-else: Example  Not new, just different indenting  Avoids "excessive" indenting  Syntax:

© Janice Regan, CMPT 102, Sept CMPT 102 Programming Style  When using decision structures (if statements) you should always use the { } regardless of whether you use a single or multiple statements in each branch of the if statement.  The opening and closing brackets for the block should each be written on their own line. (examples follow)  WHY?  As your code evolves it is common to add statements (functionality) within a decision statement.  When you add statements and forget to add the {} to indicate the extent of the block unexpected things happen. The resulting problems can be difficult to find.

© Janice Regan, CMPT 102, Sept Defining the condition  Each decision statement is based upon a condition  The condition is an expression with a logical value (true or false)  The condition is a boolean expression and may be  A relational expression (a type of logical expression) Two numerical values combined using a binary relational operator (a simple relational expression) A more complex relational expression  Another type of logical expression Two logical values combined with a binary logical operator One logical value One logical value operated on by a unary logical operator A more complex logical expression

© Janice Regan, CMPT 102, Sept Binary Relational Operators in C  <less than  <=less than or equal to  > greater than  >= greater than or equal to  == equal to  !=not equal to  Evaluated left to right Binary Equality Operators in C

© Janice Regan, CMPT 102, Sept Binary Logical Operators  &&Logical AND  ||Logical OR  !Not  Evaluated left to right  Arguments of logical operators have values of true or false Unary Logical Operators

© Janice Regan, CMPT 102, Sept Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 && EXPRESSION2 T T T T F F F T F F F F

© Janice Regan, CMPT 102, Sept Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 || EXPRESSION2 T TT T FT F TT F F F

© Janice Regan, CMPT 102, Sept Truth Tables ^ The ^ (Exclusive or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 ^ EXPRESSION2 T T F T F T F TT F FF

© Janice Regan, CMPT 102, Sept Truth Tables ! The ! (Not) operator EXPRESSION1 ! EXPRESSION1 T F F T

© Janice Regan, CMPT 102, Sept Precedence of operators in C  ( ) []. innermost first  (pre) + - ! ~(unary) (right to left)  * / %  + -  >=  == !=  &&  ||  = += -= *= /= %=(right to left)

© Janice Regan, CMPT 102, Sept Precedence Examples  Arithmetic before logical  x + 1 > 2 || x || ( x + 1) < -3  Short-circuit evaluation  (x >= 0) && (y > 1)  Be careful with increment operators! (x > 1) && x<y++  In C Boolean values (true and false) are represented as integers  All non-zero integer values  true  Zero value  false

© Janice Regan, CMPT 102, Sept Expressions with relational operators  Value of a relational expression (expression including a relational or binary equality operator) is true or false.  Arguments of a relational operator are numerical (or character)  A < C  (A + B) >= CLet A=9, B=5, C=2  A * B <= C  A % C == A % B  A != -C  A > B

© Janice Regan, CMPT 102, Sept Expressions: logical operators  Value of a logical expression (expression including a logical operator) is true or false.  Arguments of the logical operator are also true or false Let A=9, B=5, C=2 Then  (C = C  (A C  !(A < B) || B<A  A < B && B < C++

© Janice Regan, CMPT 102, Sept Common Pitfalls in C  Operator "=" vs. operator "=="  One means "assignment" (=)  One means "equality" (==)  These two operators are VERY different  Example of common error: if (x = 12)  Note operator used! Do_Something else Do_Something_Else