CONTROLLING PROGRAM FLOW

Slides:



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

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
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.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
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:
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
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.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
REPETITION CONTROL STRUCTURE
Chapter 3: Decisions and Loops
Programming Fundamentals
Repetition Statements
3 Control Statements:.
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

CONTROLLING PROGRAM FLOW CHAPTER 4 CONTROLLING PROGRAM FLOW

The if Statement Allow a program to make decisions. Syntax Example: if (Temp < 5) cout << “Wear a coat today.” << endl; Syntax if (condition) statement;

Boolean expression An expression that evaluates to either true or false. Use relational operators Operator Meaning == equal < less than <= less than or equal to > greater than >= greater than equal to != not equal to

Examples ch <= ‘Z’; Grade == 12; FruitName == “Banana”; Radius != 0; 2*pi*Radius >= Area;

/. Temperature program. / #include <iostream /*Temperature program */ #include <iostream.h> int main( ) { double Temp; cout << “Enter today’s temperature(Celsius): “; cin >> Temp; if (Temp < 5) cout << “Wear a coat today.” << endl; cout << “Have a gret day!” << endl; return (0); }

4.2 if Pitfalls Never make equality comparisons (== or !=) with values that may have fractional parts. if (480 == 4.8 * 100) cout << “These are equal!”; Compare values of only the same type Example: ‘2’ > 3; (page 7.12) = vs == Misplaced semicolon

4. 3 The if-else Statement Can include an else clause when condition is false if (condition) statement; else Indention is good programming style.

if (Temp < 5) cout << “Wear a coat today.” << endl; else cout << “Don’t wear a coat today.” << endl;

Modify the Circle Area program to display an error message when the value entered for Radius is negative. If Radius is a positive value or zero, only the resulting area in a message should be displayed. The error message should look similar to: Radius entered was -3.4 Negative radii are illegal.

#include <iostream.h> int main( ) { double Radius; cout << “Enter a radius: “; cin >> Radius; if (Radius < 0) cout << “Radius entered was “ << Radius; cout << “Negative radii are illegal.” << endl; } else cout << “Area = “ << (3.14 * Radius * Radius) << endl; return (0);

4.5 Nested if Statements An if statement that contains another if statement if (Votes1 == Votes2) cout << “It was a tie!” << endl; else if (Votes1 > Votes 2) cout << Candidate1 << “ is the winner”; cout << Candidate2 << “ is the winner.”;

4.6 The else-if Ladder Used to decide among three, four, or more actions. if (temp <= 0) cout << “Freezing”; else if (temp < 13) cout << “Cool”; else if (temp < 41) cout << “Hot”; else cout << “Very hot”; Does not contain an if statement. It is executed of all other conditions are false. DEFAULT.

4.8 Logical Operators (true) && (false) evaluates to false “and” represented by && (true) && (true) evaluates to true (true) && (false) evaluates to false (false) && (true) evaluates to false (false) && (false) evaluates to false if ((Temp < 5) && (Wind > 24)) cout << “Wear a coat today!”;

4.8 Logical Operators (true) || (false) evaluates to true “or” represented by || (true) || (true) evaluates to true (true) || (false) evaluates to true (false) || (true) evaluates to true (false) || (false) evaluates to false if ((Temp < 5) || (Wind > 24)) cout << “Wear a coat today!”;

4.8 Logical Operators !(false) evaluates to true “not” represented by ! !(true) evaluates to false !(false) evaluates to true if (!(Temp < 5)) cout << “DON’T wear a coat today!”;

4.9: Looping – The do-while Statement iteration Repeat one or more statements during program execution. Referred to as looping. Form: One or more C++ statements form the body of the loop do { statement; } while (condition); A Boolean expression used to determine if the loop is to be repeated.

4.9 Continued The do-while loop is executed at least once because the condition is not evaluated until AFTER the first iteration of the loop. If the condition is true, statement is executed again and then the condition reevaluated. Looping process is repeated until the condition is evaluated and found to be false.

#include <iostream.> int main() { char Answer; cout << “Circle Area Calculator” << endl; do { const double PI = 3.14159; double Radius; cout << “Enter the radius: “; cin >> Radius; cout << “Area is “ << (PI * Radius * Radius)<< endl; // Ask if another calculation is desired cout << “Do another circle (Y/N)?”; cin >> Answer; }while (Answer == ‘Y’); cout << “Thanks for using the Circle Calculator” << endl; Return (0); }

4.11 Looping: The while Statement Evaluates its condition BEFORE each iteration May execute zero or more times. Syntax (form) of a while loop body: while (condition) { statement; }

#include <iostream.h> int main() { int number; cout << “Enter a positive number: “; cin >> number; while (number < 0) { cout << “Number must be positive\n”; cout << “Please re-enter: “; }

4.16 Looping: The for Statement Used to execute a loop body a fixed number of times. Form: for (initialization, condition, increment) statement; Evaluated before each iteration Performed after each iteration - counter Performed only once

4.16 Looping: The for Statement Example: The following loop display the numbers 1 through 10. for (int i = 1; i <= 10; i++) cout << I << endl;

4.17 – and -= Decrement (decrease) Total--; Special form of assignment operator indicates the value after the operator is to be subtracted from the value of the variable on the left of the operator. Total = Total – Value; Total -=;

PROGRAMS Review 21 and 22 Exercises Quiz on Friday (for statements) 16a, p. 4-33 18a, b, c, p. 4-33/34 21 Quiz on Friday (for statements)