Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
CS150 Introduction to Computer Science 1
Program Write a program that outputs the following: ***** 11/27/2018 CS150 Introduction to Computer Science 1
5
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
6
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
7
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
8
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.