Increment and the While. Class 15

Similar documents
*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

REPETITION CONTROL STRUCTURE LOGO

Chapter 5: Loops and Files

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 4 - Notes Control Structures I (Selection)

Why Is Repetition Needed?

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Study recommendations for Quiz 5 - Chapter 5

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Looping. Arizona State University 1

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Control Structures of C++ Programming (2)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming Fundamentals

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 5: Control Structures II (Repetition)

Unit 7. 'while' Loops

A First Program - Greeting.cpp

Add Subtract Multiply Divide

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Introduction to Programming

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

C++ Programming: From Problem Analysis to Program Design, Third Edition

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Lecture 7 Tao Wang 1

C/C++ Programming Lecture 7 Name:

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Introduction. C provides two styles of flow control:

Study recommendations for the Mid-Term Exam - Chapters 1-5

C++ Final Exam 2017/2018

CSCE 206: Structured Programming in C++

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

CSCI 123 Introduction to Programming Concepts in C++

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

CS2141 Software Development using C/C++ C++ Basics

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

In this chapter you will learn:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Chapter 7. Additional Control Structures

Chapter 3. More Flow of Control

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Scientific Computing

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Programming Language. Functions. Eng. Anis Nazer First Semester

Control Structures II. Repetition (Loops)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CHAPTER 3 BASIC INSTRUCTION OF C++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Object-Oriented Programming in Java

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

CSI33 Data Structures

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Relational Operators and if. Class 10

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Introduction to C ++

Computer Programming : C++

Programming for Engineers Iteration

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lecture # 6. Repetition. Review. If Else Statements Comparison Operators Boolean Expressions Nested Ifs. Switch Statements.

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Computer Programming I - Unit 5 Lecture page 1 of 14

More Complex Versions of the if Statement. Class 13

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

LECTURE 02 INTRODUCTION TO C++

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Object oriented programming

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Week 1: Hello World! Muhao Chen

Transcription:

Increment and the While Class 15

Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment or decrement an unsigned integer: x += 1; and x -= 1; so common that there are operators just for this: the increment operator ++ and the decrement operator -- they are unary operators assuming is a variable x with a value, x++ causes x to have the next larger value unsigned int x = 5; x++; // now x is 6 x--; // now x is 5 again

Prefix and Postfix each operator comes in two forms, a prefix form and a postfix form unsigned int x = 5; x++; // now x is 6 ++x; // now x is 7 in the example above, both prefix and postfix forms do the same thing but there is a huge difference in how they actually operate

Prefix and Postfix unsigned int number = 5; cout << number++ << endl; cout << ++number << endl; the difference is in when they operate in each case, number is being used in an expression and also being incremented

Prefix and Postfix unsigned int number = 5; cout << number++ << endl; cout << ++number << endl; the difference is in when they operate in each case, number is being used in an expression and also being incremented in the prefix form, the increment happens first in the postfix form, the increment happens last thus the output from the above lines of code is:

Prefix and Postfix unsigned int number = 5; cout << number++ << endl; cout << ++number << endl; the difference is in when they operate in each case, number is being used in an expression and also being incremented in the prefix form, the increment happens first in the postfix form, the increment happens last thus the output from the above lines of code is: 5 7

Prefix and Postfix another example double foo = 6.25; double bar; bar = foo++; double quux = --foo; after this code runs bar has the value quux has the value

Prefix and Postfix another example double foo = 6.25; double bar; bar = foo++; double quux = --foo; after this code runs bar has the value 6.25 quux has the value

Prefix and Postfix another example double foo = 6.25; double bar; bar = foo++; double quux = --foo; after this code runs bar has the value 6.25 quux has the value 6.25

Using Increment and Decrement the following code snippets are completely legal: x *= y / ++z; if (a++ > 10) but they are very dangerous they are confusing and hard to figure out never do this! a best practice of programming is that each statement or expression should do only one thing these are doing two things at once they should be split into separate statements

Non-Linear Control Flow II the focus of chapter 4 was non-linear program flow using the concept of the if statement technically called branching the focus of chapter 5 is a different form of non-linear program flow: looping

Non-Linear Control Flow II the focus of chapter 4 was non-linear program flow using the concept of the if statement technically called branching the focus of chapter 5 is a different form of non-linear program flow: looping C++ has three looping constructs 1. the while loop 2. the do-while loop 3. two versions of the for loop

The while Loop the while loop is extremely similar in both structure and action to the simple if statement the only difference in structure is one word while (expression) { statement; statement;... } all the rules of style and the common mistakes are identical to those for the if statement

Flowchart the flowchart of while is almost identical to simple if but there is one crucial difference simple if sends the program forward around a detour while sends the program around a detour and then backwards this allows for the program to repeat a block of statements more than once indeed, another name for looping is repetition

1 // A simple while loop from 2 // Gaddis Program 5-3 page 238 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 unsigned int number = 0; 9 10 while (number < 5) 11 { 12 cout << "Hello" << endl; 13 number++; 14 } 15 cout << "Done" << endl; 16 return 0; 17 } line 8: the loop control variable is initialized line 10: the loop control variable is tested line 13: the loop control variable is incremented lines 12 and 13 repeat while the Boolean expression is true when the Boolean expression becomes false, control jumps to line 15 Hello is printed exactly 5 times

Pretest the while loop is a pretest loop the Boolean expression is tested each time before the loop body is executed the loop body may be executed zero times the loop body may be executed exactly one time the loop body may be executed many times

Pretest the while loop is a pretest loop the Boolean expression is tested each time before the loop body is executed the loop body may be executed zero times the loop body may be executed exactly one time the loop body may be executed many times in fact, the loop body may be executed infinitely many times this is a bad thing an infinite loop usually caused by incorrect logic failure to properly initialize the loop control variable failure to increment or decrement the loop control variable

Input Validation a common use for a while loop is input validation cout << "Enter an ACT score: "; unsigned int act_score; cin >> act_score; while (act_score < 1 act_score > 36) { cout << "Invalid score. Again: "; cin >> act_score; }... do stuff with act_score

Counters and Accumulators look at program count accumulate.cpp the variable count of scores is a counter it keeps track of how many times the loop body executes it is incremented each time the body executes

Counters and Accumulators look at program count accumulate.cpp the variable count of scores is a counter it keeps track of how many times the loop body executes it is incremented each time the body executes the variable sum of scores is an accumulator it keeps a running sum of the non-zero scores that were entered it is added to each time the body executes

Counters and Accumulators look at program count accumulate.cpp the variable count of scores is a counter it keeps track of how many times the loop body executes it is incremented each time the body executes the variable sum of scores is an accumulator it keeps a running sum of the non-zero scores that were entered it is added to each time the body executes both variables must be initialized before the loop begins the counter variable may or may not be the loop control variable (here it is not)

The do-while Loop the second loop construct of C++ is the do-while loop its form is below note the semicolon! do { statement; statement;... } while (expression); this is a posttest loop its Boolean expression is tested after the loop body executes it is guaranteed that the loop body will execute at least once look at program count accumulate do while.cpp, which is the previous program converted to use a do-while loop

Controlling a Loop With a Flag 1 bool done = false; 2 3 while (!done) 4 { 5 cout << "Enter a plan: "; 6 char plan; 7 cin >> plan; 8 9 if (plan == 'A') 10 // stuff for plan A... 11 else if (plan == 'B') 12 // stuff for plan B... 13 else if (plan == 'C') 14 // stuff for plan C... 15 else 16 { 17 done = true; 18 } 19 } it is extremely common to control a while loop with a flag the flag is initialized to false when the loop exit condition is recognized, the flag is set to true