Throughout your programming experience you've probably been doing things like adding and subtracting 1 to a variable. C++, in its infinite wisdom, has shortcuts that make it easier to do things like this. Let's look at them. Consider this statement (you've probably seen it a thousand times, in one form or another) : burrito = burrito + 1; You've used that every time you needed to add 1 to a variable. Now, behold the easier way! burrito += 1; Isn't that easy? It doesn't stop there, though. You can do the same thing with subtraction, multiplication, division, and almost any other operator! Here's a list of commonly used ones:
Those should help you out. Let's try using them in a small program: #include <iostream.h> void main(void) { int fajita; double taco; fajita = 0; fajita = fajita + 4; / Old Way fajita += 4; / New Way! fajita = fajita - 4; / Old Way fajita -= 4; / New Way taco = 5; taco = taco / 2; / Old Way taco /= 2; / New Way! taco = taco * 2; / Old Way taco *= 2; / New Way! } In that example, we execute the new way right after each old way. Next time you write a program, try using a couple of these. Once you get the hang of them, they'll become very useful, and you'll appreciate whoever was lazy enough to ask for them in the first place.
One of the neatest shortcuts allows you to take the " i++; / Add 1 to i Or i--; / Remove 1 from i
You'll have to play with those two -- they're really useful, especially
inside for (i=0;i<10;i++) { cout << "I is " << i << "\n"; } |