Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Control Structures.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Control Flow Statements: Repetition/Looping
Control Structures Selections Repetitions/iterations
Decisions If statements in C.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
More on Algorithms and Problem Solving
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Week 4: Control Structures - Repetition
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
true (any other value but zero) false (zero) expression Statement 2
Visual C++ Programming: Concepts and Projects
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter#3 Part1 Structured Program Development in C++
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Branching statements.
CNG 140 C Programming (Lecture set 3)
The if…else Selection Statement
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
EGR 2261 Unit 4 Control Structures I: Selection
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
JavaScript: Control Statements I
Operator Precedence Operators Precedence Parentheses () unary
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 4: Making Decisions.
Control Structures – Selection
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Relational and Logical Operators
MSIS 655 Advanced Business Applications Programming
Structured Program
Quiz 1 96/07/23.
3 Control Statements:.
Computer Science Core Concepts
Relational and Logical Operators
Week 3 – Program Control Structure
Relational Operators.
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Relational and Logical Operators
Structural Program Development: If, If-Else
Presentation transcript:

Lecture 3: Control Structures - Selection BJ Furman 10SEP2012

The Plan for Today Review from last two weeks  Flowcharts  Pseudocode  Data types  Variables and Constants  Structure of a C program  Formatted output: printf( ) Operators and their precedence Review control structures  Sequence  Selection  Repetition Selection structures  If  If/else  Switch Relational operators Selection structure example

Learning Objectives Apply concepts for developing algorithms, using variables, and structuring a C program Explain what is meant by a control structure Explain the three basic types of control structures Determine the result of relational comparisons Apply the if and if/else control structures

Control Structures - Review All programs can be written in terms of three control structures (like building blocks) SSequence ‘Built-in’ to C UUnless otherwise directed, one statement after the next is executed SSelection (three types) Depending on a condition, select between one statement or another IIf var1 is greater than 10, do this…, else do that… RRepetition (three types) Depending on a condition, execute one or more statements repeatedly

Selection Structure Overview Three kinds of selections structures iif (also called, ‘single-selection’) if condition is true Perform action if condition is false, action is skipped, program continues iif/else (also called, ‘double-selection’) if condition is true Perform action else ( if condition is false ) Perform a different action (this will be skipped if condition is true) sswitch (also called ‘multiple-selection’) Allows selection among many actions depending on the integral value of a variable or expression

Single Selection IF - Flowchart TRUE FALSE Speed > 65 connector flow line decision symbol action symbol Print “You’re speeding”

Relational Operators Important for constructing the decision expression 5 < 7 result is ____ 5 > 7 result is _____ 7 <= 7 result is ____ 8 >= 7 result is ____ 5 == 5 result is ____ 5 == 7 result is ____ var1 = 7 result is____ 5.0 == 5 result is ___ 6 != 5 result is ____ Adapted from H. Cheng chap04.ppt, slide 5 Practice

Double-Selection IF - Flowchart TRUE Speed > 65 FALSE Print “Over speed limit” Print “Within speed limit”

Adapted from Deitel & Deitel, C How to Program, 6 th ed., p. 111 SWITCH - Flowchart

IF statement (single-selection) Syntax if(expression) /* if expression is TRUE (i.e., NOT EQUAL to zero) */ statement1; /* then execute this statement */ statement2; /* execute this statement next*/ Notes  Indent the statements for clarity  Can have multiple statements Enclose a ‘block’ of statements using { } (curly braces) if( x <= 2 ) { statement1; statement2; } statement3;

IF statement example Pseudocode (notice indentation!) If speed is greater than 65 mph print “You’re speeding!” C code if(speed > 65) printf(“You’re speeding!\n”); C code with multiple statement block if(speed > 65) /* statements below executed only if speed > 65 is true */ { printf(“You’re speeding!\n”); printf(“Slow down!\n”); printf(“Keep speed below 65 MPH\n”); }

IF-ELSE statement - Double Selection Syntax if(expression) /* if expression is TRUE (i.e., NOT EQUAL to zero) */ statement1; /* execute this statement */ else /* else execute the following statement */ statement2; Notes:  If expression is non-zero, statement1 is executed, then the program continues with the statement after statement2, i.e., statement2 is skipped  If expression is equal to zero, statement1 is skipped and statement2 is executed, then the program continues with the statement after statement2

IF-ELSE statement example Pseudocode (notice indentation!) If speed is greater than 65 mph print “Over speed limit!” else print “Within speed limit” C code if(speed > 65) printf(“Over speed limit!\n”); else printf(“Within limit\n”);

Compound Condition - && Logical operators for more complex decisions  Logical AND operator && (double ampersand) if(switch1 = = 0 && switch2 = = 1) turn on the motor The condition evaluates to TRUE if and only if BOTH expressions on either side of && evaluate to TRUE  Note operator precedence Otherwise condition evaluates to FALSE  Beware of ‘short circuit evaluation’  Make the condition most likely to be FALSE the left- most condition

Compound Condition - | | Logical operators for more complex decisions, cont.  Logical OR operator | | (double vertical bar) if(switch1 = = 0 || switch2 = = 1) turn on the motor The condition evaluates to TRUE if one or the other or both expressions on either side of && evaluate to TRUE  Note operator precedence Otherwise condition evaluates to FALSE  Beware of ‘short circuit evaluation’  Make the condition most likely to be TRUE the left-most condition

Grade Determination for Overall Percentage (OP) OP >= 90‘A’ 80 <= OP < 90‘B’ 70 <= OP < 80‘C’ 60 <= OP < 70‘D’ OP < 60‘F’ Nesting selection structures Selection structures can be stacked and nested to handle more sophisticated decision/action functionality  Ex. Figuring grades Pseudocode  Notes:  “ an else is always associated with the nearest previous if” (Darnell & Margolis, 1996)  Use braces ({ })to clarify the association of the else for other situations where the decision structure is more complicated Adapted from Deitel & Deitel, C How to Program, 3 rd ed., p. 64

Nesting If/else – C Code – Two Ways Or Adapted from Deitel & Deitel, C How to Program, 3 rd ed., p. 64

SWITCH Good when faced with testing multiple alternatives that depend on a single variable  The test is done once Must be an integral expression  int or char  NOT float, double  case items must be constant integral expressions No variables  The structure is very organized and readable

Adapted from Deitel & Deitel, C How to Program, 6 th ed., p. 111 SWITCH - Flowchart

Practice - 1 Pair up with someone next to you that you do not know Develop an algorithm for:  Finding and printing out the largest of two numbers (3 min) One person work on the pseudocode, the other on a flowchart (1 min) Compare pseudocode and flowchart (3 min) Write out the algorithm in C

Practice - 2 Develop an algorithm for the ignition control in a car: Requirements:  The starter will only start when: Key must be in the ignition slot Transmission selector must be in ‘Park’ Key must be turned to ‘Start’ position  The starter is energized with the statement starter_on();

Review

References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3rd ed., Springer, New York. Cheng, H. H. (2010). C for Engineers and Scientists: An Interpretive Approach, McGraw-Hill, New York. Deitel, H. M. & Deitel, P. J. (2001). C How to Program, 3rd ed., Prentice-Hall, New Jersey.

Nesting selection structures Selection structures can be stacked and nested to handle more sophisticated decision/action functionality /* File: ifc.c */ #include int main () { int i; i = 10; if(i==2 || i == 4) { printf("i = 2 or 4\n"); } else if(i == 10) { printf("i = 10\n"); } else { printf("i = %d\n", i); } return 0; } Adapted from H. Cheng chap05.ppt, slide 12

Operators Adapted from H. Cheng chap04.ppt, slide 5