Friday, May 9, 2008

C++ Loops...

Well, the more people i showed this blog to on the C++ lessons, the more they want me to keep an up-to-date blog on the lessons made.... lol. okay, i can only do that if i understand. And i guess if i do a write up the more i'll understand better, and in a way it's my notes too. okay, so this week the discussions was on loops... so let's have an indepth look at loops.


So, how do i begin.... okay... picking off where we left off from switch,

cin : Ask User What sex is he or she
switch(userinput)
{
Case 'Male': Tell him he is a man

Case 'Female': Tell her she is a woman

default: user did not input what he is
}

So, let's say we want to keep on asking the user to input something that is either male or female. so here we can use the loop statement.

while(expression)
{
...
}

which in this case we can do,

-Declare what is USERINPUT.
-Declare what is TRY.

set TRY to FALSE

while(if try is FALSE loop again)
{
cin : Ask User what sex is he or she
switch(USERINPUT)
{
Case 'Male': Tell him he is a man
Set TRY to TRUE

Case 'Female': Tell her she is a woman
Set TRY to TRUE

default: Tell user the entry is invalid and to try again
Set TRY to FALSE(Or not since it is FALSE by default)
}
}

So, that would work in that form. So what happens is that when the user starts this program, it will ask the user to key in their sexual state. lol. And have to type in either MALE or FEMALE. If the user keys in anything else, it will just repeat the same question over again.

One of the dangers of using the loop statement is the eternal loop that can follow if it is missused. What happens is that, it will continue the loop as no one defined where it should stop. so let me show you an example.

Define User

While(if user is human loop again)
{
ask user what user is
}

That is an obvious thing. Since only humans can use the internet. It will keep on asking the user what he is. Also, since it's in this state, the program is still running so it will keep on asking this question over and over again. an endless loop that the user can only terminate by an ALT-F4 or by restarting the computer. In extreme cases it's the latter choice.

So the important thing is always to keep the expression in the while statement up to date, unless you want it to keep on annoying the user on something. There should be a point in which it can break the loop. That is the most important in coding.

Okay, now that we have looked at the simple way of coding, now let's look at the more streamlined way to do it. This way is better as firstly it's alot more cleaner, makes you look pro and beats writing alot more code... which i will show you in a while.

for (initial statement;loop statement;update statement)
{
...
}

...hmm... this part i'm not exactly sure right now, so i'll leave it down here for now, i'll be back when i properly understand this part, and then i'll continue this lesson.

Edit: Okay so after some deliberation....

basically what the above means is:


for (set ask to 0; if cheese is yes or if "ASK" is 3, exit;increment "ASK" by 1)
{
Ask if user likes cheese
}
switch(cheese)
{
case 'Yes' :
Output YAY CHEESE!~
break loop;

case 'No' :
Boo to you.....you suck
}

See? That would work. what basically is happening is that, at the start by default the program will set ASK to 0 since it needs to be defined. Then what happens is, if the user tried the program and typed in no, it will loop again, for 3 times before displaying you suck. on the other hand, if the user types in yes, it will print yay cheese.

Basically that's all there is to it in loops. i hope you took back something from this. =)

No comments: