Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)

Slides:



Advertisements
Similar presentations
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Advertisements

Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
The Program Design Phases
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Chapter 5: Control Structures II (Repetition)
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
C++ Programming: From Problem Analysis to Program Design, Fourth 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.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120 Introduction to Computer Science Iteration (Looping)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CIS162AD - C# If Statements 04_decisions.ppt. CIS162AD2 Overview of Topic  Pseudocode  Flow Control Structures  Flowcharting  If, If-Else  Nested.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
REPETITION CONTROL STRUCTURE
CSC113: Computer Programming (Theory = 03, Lab = 01)
Structured Program
Repetition Control Structure
Chapter 8: More on the Repetition Structure
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Presentation transcript:

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt) CIS162AB - C++ Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)

Overview of Topics Pseudocode Control Structures Flowcharts Single and Compound Boolean Expressions Single and Compound Statements If, if-else, nested ifs While, do-while, nested loops CIS162AB

Pseudocode Pseudocode is a mixture of C++ code and English like statements. Used when designing algorithms. When designing, we don’t necessarily want to be concerned about where semi-colons go. We want to concentrate on the design. I’ll use pseudocode throughout the course, so don’t feel compelled to correct my syntax. However, if at anytime you are not sure about a command, please be sure to ask for clarification.  CIS162AB

Flow Control The order in which statements are executed. There are four control structures. Sequence Control Structure Selection Control Structure Also referred to as branching (if and if-else) Repetition Control Structure (loops) Case Control Structure (switch) CIS162AB

Flowcharting A flowchart is a pictorial representation of an algorithm or logical steps. Each step is represented by a symbol and the arrows indicate the flow and order of the steps. The shape of the symbol indicates the type of operation that is to occur. Flowcharts may help the move visual students learn and understand logic. CIS162AB

Flowchart Symbols Input or Output Begin or End Decision Processing Branch or Direction of Flow CIS162AB

1. Sequence Control Structure The order statements are placed (sequenced) cin >> price >> qty; subtotal = price * qty; cout << subtotal; The only way to display the subtotal, statements must be in this order. CIS162AB

Flowchart – Sequence Control Begin Input price, qty subtotal = price * qty; Output subtotal End CIS162AB

2. Selection Control ( if ) Simple if with single statement. discountRate = 1.00; if (qty > 25) discountRate = .98; subtotal = qty * (price * discountRate); CIS162AB

Flowchart – if statement discountRate = 1.00; If qty > 25 True discountRate = .98; False Subtotal = qty * (price * discountRate); CIS162AB

Selection Control (if-else) Simple if-else with single statements. DISCOUNT_RATE = .98; if (qty > 25) subtotal = qty * (price * DISCOUNT_RATE); else subtotal = qty * price; cout << subtotal; CIS162AB

Flowchart – if-else statement DISCOUNT_RATE = .98; If qty > 25 Subtotal = qty * (price * DISCOUNT_RATE); True False Subtotal = qty * price; Output subtotal CIS162AB

Boolean Expressions Boolean expressions evaluate to true or false. Must be enclosed in parenthesis if (Boolean expression) true statements (Yes) else false statements (No) CIS162AB

Compound Statements Use braces to create a block of statements. if (hours > 40) { regularPay = 40 * payRate; overtimePay = (hours – 40) * (payRate * OVERTIME_RATE); } else regularPay = hours * payRate; overtimePay = 0; grossPay = regularPay + overtimePay; CIS162AB

Incorrect if-else regularPay = 40 * payRate; if (hours > 40) { regularPay = 40 * payRate; overtimePay = (hours – 40) * (payRate * OVERTIME_RATE); } else regularPay = hours * payRate; overtimePay = 0; grossPay = regularPay + overtimePay; overtimePay would always be set to zero. CIS162AB

Boolean Expressions Simple Expressions Compound Expressions Have one comparison (hours > 40) Compound Expressions Have more than one comparison Created by using And and Or operators And - (qty > 0 && qty < 51) Or - (qty < 0 || qty > 51) CIS162AB

And Operator - && Both conditions must be true. if (qty > 0 && qty < 51) { subtotal = qty * price; } else cout << “Quantity must be 1 – 50.”; What happens if qty = 0, qty = 25, or qty = 60? A block with a single statement is valid. CIS162AB

Or Operator - | | Either condition must be true. if (qty < 1 || qty > 50) { cout << “Quantity must be 1 -50.”; } else subtotal = qty * price; What happens if qty = 0, qty = 25, or qty = 60? CIS162AB

Pipe Character - | Where is the pipe character on the keyboard? On most keyboards It is right above the Enter key Shares the key with the back slash - \ Must hold the shift key go get it Instead of a solid line, it is shown as a broken line For the Or operator, 2 pipe characters must be entered - | |. For the And operator, 2 ampersands characters must be entered - &&. CIS162AB

Notes on Boolean Expressions Short-circuit evaluation – if the evaluation of the entire expression can be determined by the result of the first expression, the second expression is not evaluated. int y =1, x =0; if (y > 0 | | x > 0) // T or F Not operator (!): ( y != 0) //This statement is OK, but the not ( !(y= =0)) //outside the parentheses can be confusing. CIS162AB

Booleans Only True or False C++ does the math to get to a True or False. true = 1, false = 0 Non-zeros converted to true, which is a one.   if ( (5 && 7) + (!6) ) T && T + (!T) T + F 1 + 0 = 1 = T CIS162AB

Nested if Statements - Indented if (qty > 0) if (qty < 51) subtotal = qty * price; else cout << “Quantity must be < 51”; cout << “Quantity must be > 0”; CIS162AB

Matching else to if How are else statements matched with an if? Compiler works it’s way back. When an else is encounter, it looks back to find an if that has not been matched to an else. Why do we indent each level? We indent to make programs easier to read. Indenting has no effect on how compiler matches an else to an if. CIS162AB

Multiway Branching Indenting not always practical if (qty < 26) discount = 0 else if (qty < 51) discount = .01 if (qty < 76) discount = .02 if (qty < 101) discount = .03 … more CIS162AB

Multiway Branching if (qty < 26) discount = 0 else if (qty < 51) CIS162AB

Conditional Operator Expression if (qty > 25) discountRate = 0.98; else discountRate = 1.00; discountRate = (qty > 25) ? .98 : 1.00; CIS162AB

Assignment (=) vs Comparison (==) if (x = 12) // always true if (x == 12) // comparison To prevent accidental assignment, state constant first. if (12 = x) Compiler will report this as a syntax error. CIS162AB

3. Repetition Control (loops) loop – a group of statements that are repeated until a certain conditions occurs to stop it. while loop do-while loop for-loop (covered later) CIS162AB

While Loop Example count = 3; //initialize controlling variable while (count > 0) { cout << count; count--; } Output: 3 2 1  CIS162AB

While Loop Controlling Boolean expression evaluated before executing loop body. Controlling variable must be initialized. Boolean expression must be true to enter loop body. No semicolon after Boolean expression. It is possible that body is not executed at all. Boolean expression must be false to get out of loop. Controlling variable must be modified inside loop. Execution continues with next statement after loop. CIS162AB

Flowchart – While Loop Initialization important for While Loop count = 3 while count > 0 False True Output count Represents Loop Skip or Exit Loop count-- Next statement CIS162AB

Do-while Loop Example count = 3; do { cout << count; count--; }while (count > 0); Output: 3 2 1 CIS162AB

Do-while Loop Controlling Boolean expression evaluated after executing body. So, body is always executed at least one time. Initialization of controlling variable not necessarily required if it will be initialized inside the loop. Boolean expression must be false to get out of loop. Controlling variable must be modified inside loop. Semicolon required after Boolean expression. Execution continues with next statement after loop. CIS162AB

Flowchart – Do-while Loop count = 3 Output count Loop will be executed at least one time, because the condition is at the bottom. count-- Represents Loop while count > 0 True False - Exit Loop Next statement CIS162AB

Loop Summary Incrementor: count++ Decrementor: count-- Infinite loop – expression that always evaluates to true Avoid using = = as the operator. Controlling variable must be altered within the loop. Use Control-C to terminate an infinite loop. When to use a while or do-while will become evident as we continue to use and learn each loop. CIS162AB

Nested Loops count = 3; while (count > 0) { cout << endl << count; count--; count2 = 1; do cout << count2; count2++; }while (count2 < 4); } 3 1 2 3 2 1 2 3 1 1 2 3 CIS162AB

While Loop Error Semi-colon after expression creates an infinite loop with a single statement that does nothing. DO NOT DO THIS. while (count > 0); { cout << count; count--; } CIS162AB

4. Case Control Structure Switch statement covered later. CIS162AB

Summary Control Structures Flowcharts Single and Compound Boolean Expressions Single and Compound Statements If, if-else, and nested ifs While, do-while, and nested loops CIS162AB