Unary Operators ++ and --

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS150 Introduction to Computer Science 1
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Compound Operators 03/07/11. More Operators, First Section 4.4.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
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.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 CS161 Introduction to Computer Science Topic #6.
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.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
for Repetition Structures
Chapter 2 Assignment and Interactive Input
Chapter 5: Loops and Files.
Repetition Structures (Loops)
Computer Science 101 While Statement.
Learning About the Loop Structure (continued)
Introduction to pseudocode
Counting Loops.
Repetition Control Structure
Chapter 6: Repetition Statements
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
CS150 Introduction to Computer Science 1
2.6 The if/else Selection Structure
Understanding Conditions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Repetition Statements (Loops) - 2
Life is Full of Alternatives Part 3
Presentation transcript:

Unary Operators ++ and -- ++ is increment operator x++; is the same as x = x + 1; -- is decrement operator x--; is the same as x = x - 1; 11/27/2018 CS150 Introduction to Computer Science 1

Prefix and Postfix Unary ++ and -- Prefix Postfix k = --x; k =x--; k = ++x; k = x++; Increment/ Assign value of x to decrement x k, then increment then assign or decrement x value of x to k 11/27/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example cout << “Value of i is” << i; cout << “Value of i++ is” << i++; cout << “Value of ++i is” << ++i; cout << “Value of --i is” << --i; cout << “Value of i-- is” << i--; 11/27/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a program that outputs the following: ***** 11/27/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Loops Initialize LCV Loop Control Variable count = 0; while (count < 5) { cout << “*****” << endl; count++; } How many times (iterations) does loop run? Change the value of count 11/27/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 While loops while (logical expression is true) statement; { statement1; statement2; … } 11/27/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Key ingredients Initialize MUST initialize loop control variable Test The value of loop control variable is tested during each iteration of loop Update Loop control variable is changed during each loop iteration If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely. 11/27/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples Write a while loop that outputs each integer from 1 to 5 Write a program that inputs the following data for 5 students: name, id# and grade Write a program that inputs the following data for a user specified number of students: name, id# and grade 11/27/2018 CS150 Introduction to Computer Science 1