Thursday, December 11, 2008

Week 1, Day 7

goto, while loops, for loops, do...while loops, and switch expressions. Pretty basic program flow parts of basically any programming language (ok, so the last two are not in all the languages I have learned so far... but still).

Today's "Class"
Today's chapter was all about controlling the flow of the program with loops and another condition branch tool.

goto
Basically... after showing how to do it, the book said to never use it. It makes code rather confusing. It has it's very rare uses, but apparently the author has only had to use it ONCE in his entire C++ career. If you feel you absolutely need to use it, you should be more capable at C++ than this blog will probably EVER teach you... so I won't bother going any further on it.

while loops
Very basic and straight forward. While the condition is true, the code is executed. Example:
int i = 0;
while( i < 10 )
{
i++;
cout << "i: " << i << endl;
}


That little code will just print "1", "2", "3", ... all the way down to "10". Nothing too complicated. Obviously, if the condition is false to begin with, the code is not ran at all. Be careful not to make a never ending loop unless you have a way out of it.
Speaking of a way out of a never ending loop... the break; command will exit a loop. The continue; command will restart the current loop at the beginning of the loop (skipping whatever is after the command). Apparently, those are suppose to be used sparingly... so as to make your code more easily read. Good to know that you can do that in your code if you come across something that needs it.

do...while loops
If you would like to create a while loop that for sure executes at least once, use a do...while. It is identical to a while loop, with the simple exception that it tests for while AFTER the code block is ran and not before it is ran. Example:
int tooLarge = 15;
do
{
tooLarge++;
cout << "tooLarge: " << tooLarge << endl;
} while(tooLarge < 10);


If this was a while loop, it would never print anything, but the do...while loop executes once and then figures out that it shouldn't loop because the condition is not met. Obviously has some usefulness in it... I can't think of a good example at the moment though. Sorry. Moving on then...

for loops
While loops are fantastic... but sometimes they are not really the right tool for the job. For instance, the code I gave as an example earlier could be done much better with a for loop. Like this:
for( i=0; i<10; i++ )
cout << "i: " << i << endl;


Nice and tidy eh? for loops have an expression, a condition, and another expression. Usually you initialize your variable in the first expression and the last expression does some sort of incrementing or decrementing. However, the chapter had me write several different programs that used all sorts of expressions in there. You can even just write a condition... but then it is just a differently labeled while loop. One of the last examples was a "forever" loop, which is a identical to a while loop that had a condition of "true". However, I love what some programmers put down to create a "forever" loop:
#define EVER ;;
....
for(EVER)
{
....
}


That has a degree of elegance and readability I love. #define EVER ;; tells the compiler that whenever it sees "EVER" in the code, replace it with the text ";;". Thus, the "forever" loop is correctly written for( ; ; ){ ... } as in a blank expression, a blank condition, and a blank expression... or a loop that can never end.

switch expressions
Instead of doing massive if...else branches, you could use switch... case.... default... expressions. You provide a variable, and then write down the different "cases" (if the value equals this... do this...). You can (and should) specify a default, if for no other reason than to debug. Here is a sample switch expression:
cin >> choice;
switch( choice )
{
case (1): cout << "you chose option one.";
break;
case (2): cout << "you chose option two.";
break;
default: cout << "that wasn't an option!";
break;
}


The break; commands are important. If you don't have them in there it will execute several commands (give it a try, it was interesting to see what happens). Obviously this is not as powerful as an if...else, but if you can use it, it is much tidier.

Summary and Workshop
Thus ended another chapter with the common summary. Exercises where ok, but I had the concept down WELL before I even finished the chapter. The chapter really seemed to drag on for me. But, that is simply because I have been using these sort of program flow loops and expressions since I was a sophmore in high school (used to program those graphic calculators for math class... surprisingly that became rather a useful primer as I went through life...).

No comments: