Basic C Question

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Basic C Question

Postposted on Mon Feb 03, 2003 3:19 pm

I have to write a program that is (counter controlled while loop). My question is what is that?
malebolgia
Gerbil Elite
 
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: New Mexico, USA

Re: Basic C Question

Postposted on Mon Feb 03, 2003 3:28 pm

malebolgia wrote:I have to write a program that is (counter controlled while loop). My question is what is that?
It's a for loop where you write the control logic into the loop yourself, instead of having the compiler do it based on the loop statement. I don't personally think there's any reason to favor it over a for loop (which makes the intent more explicit), but if that's your assignment, so be it. (heck, the purpose might be to demonstrate the superiority of a for loop)
Craig P.
Gerbil Team Leader
 
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Postposted on Mon Feb 03, 2003 8:47 pm

****whooshing of air******

Allow me to dust off my coding skills.

Ok, they way I explain loops to people is this (now I'm banking on the fact that you know how an 'IF' statement works). Loops are like 'IF' statements only a little different. As you should know 'IF' work as such:

if (statement = true)
do this
else
do that

Well this is all well and good, but what if you wanted to repeat that IF statement a number of times to perform some type of work. Ahhh, this is where the loop comes in.

Now the three basic loops in C are FOR, WHILE and DO WHILE. I'll leave out the FOR since you didn't ask about it. However I highly recommend you learn how to use a FOR loop. It is probably the most commonly used looping statement.

Ok, now the WHILE loop works like this:

while (statement = True)
do this

Let's say you wanted to count to ten on the screen it would look like this

int x = 0;

while (x <= 10)
{
printf("%x\n",x);
x++;
}

Now the idea here is that you can determine how long you want the loop to run. The number '10' can be replaced with another variable that you determined earlier. So this loop would run until x was greater then or equal to 10.

Now the difference between that and a DO WHILE loop is this:

In the above example 'x' would be equal to 0 when you get to the while statement. The first thing done is the expression is compared in the WHILE statement and it is determined if not the loop should run. x is 0 and less then 10 so it is. The first number printed is '0' If 'x' was equal to 11. The loop would never run.

A DO WHILE look like this:

int x = 0;
do
{
printf("%x\n",x);
x++;
}
while (x <= 100);

Now the loop would run the same if 'x' is equal to zero. However if x was equal to '11' you would get the print out of 11 then your program would stop. Why? The do part is always executed at least once before the WHILE condition is reached. One of my Profs told me why a DO WHILE loop was created that way, but I can't remember now. It's been a while.

Let me know if I can clear any thing else up.
"I used to think the brain was the most amazing organ in the entire body. Then I realized who was telling me this."
If ignorance were painful, half the posters here would be on morphine drips.
zgirl
Grand Gerbil Poohbah
 
Posts: 3939
Joined: Tue Jan 01, 2002 7:00 pm
Location: The dark side of the moon

Postposted on Tue Feb 04, 2003 2:26 am

My question is: is this homework?

And if so, why are you guys helping do his homework? :-) If you don't already know this, you should be asking your instructor.

If it's something else, then disregard.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4214
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Postposted on Tue Feb 04, 2003 4:35 am

woha this is really basic :) the post by z-man is perfectly accurate
remember (this is very important):
a)while(...){...} checks the condition BEFORE entering each time
b)do{...}while(...); alwas enters at least once. The check is after every loop.

-------> now if you're serious about "counters" :D
The most standard loop structure you will find for counters is this particular "for" (assuming 10 times and being "i" a counter variable or "index")

------------------------
int main()
{
int i;

[...]

for(i=0;i<10;i++)
{
[...]
}

[...]
}
------------------------
remember:
a)variable declarations have to be at the beginning of the block OR it won't qualify as ANSI C, but Objective C or C++ (or some other extensions) (usefull note mostly for exams :) )
b)inside the for, you can use multiple sentences SEPARATED BY COMMAS (yeah this is special)
c)in the typical case stated above, we are counting from 0 to limit-1(useful for vectors and matrices). If you want to count from 1, use this instead: for(i=1 ; i<=limit ; i++)

general for:
for([beginning];[condition];[afterBlock])
{
[block]
}

Hope this helps ;)
no sig
muyuubyou
Grand Gerbil Poohbah
 
Posts: 3231
Joined: Wed Aug 28, 2002 6:19 am
Location: London, UK or Tokyo/Yokohama, Japan or Madrid, Spain

Postposted on Tue Feb 04, 2003 8:31 am

Woah, I didn't give him his homework. He never told me what the assignment has to do. He just wanted to know what a WHILE loop was. I basically gave him a text book definition of a simple loop. Any C/C++ book would have this and his instructor should have lectured him on this.

BTW - Is this for a high school or college course?
"I used to think the brain was the most amazing organ in the entire body. Then I realized who was telling me this."
If ignorance were painful, half the posters here would be on morphine drips.
zgirl
Grand Gerbil Poohbah
 
Posts: 3939
Joined: Tue Jan 01, 2002 7:00 pm
Location: The dark side of the moon

Postposted on Tue Feb 04, 2003 9:25 am

There's nothing wrong with helping some with their homework.

Besides, students that do research outside of class are more likely to excel.
boobyne
Gerbil XP
 
Posts: 354
Joined: Wed Jun 12, 2002 3:58 pm
Location: Mission Viejo, California

Postposted on Tue Feb 04, 2003 10:04 am

Just remeber that a counter controlled while loop is easier to read if it is large than a for loop. Also it makes more sense if you have a fancy loop where you may need to adjust the counter outside of a simple increment/decrement. Granted this isn't done often.

Primarily you can always replace a for() loop with a while() loop and acheive the same thing. It is just a matter of readability.
liquidsquid
Minister of Gerbil Affairs
 
Posts: 2452
Joined: Wed May 29, 2002 10:49 am
Location: New York

Postposted on Tue Feb 04, 2003 10:39 am

Also, your most common error will be forgetting to increment the counter, resulting in an endless loop that will require you to kill the process.
boobyne
Gerbil XP
 
Posts: 354
Joined: Wed Jun 12, 2002 3:58 pm
Location: Mission Viejo, California

Postposted on Tue Feb 04, 2003 11:25 am

The problem is doing someone's homework for them, not helping them with it. If we just help (and carefully) then we will help them learn, which is after all the point of schoolwork. The problem when we do it for them is that they just copy it and don't learn anything.
Craig P.
Gerbil Team Leader
 
Posts: 285
Joined: Tue Dec 31, 2002 3:12 am
Location: South Bend, IN

Postposted on Tue Feb 04, 2003 1:48 pm

Craig P. wrote:The problem is doing someone's homework for them, not helping them with it. If we just help (and carefully) then we will help them learn, which is after all the point of schoolwork. The problem when we do it for them is that they just copy it and don't learn anything.


True, but he gave me no details on the exact assignment. Only that it had something to do with a WHILE loop. So I gave him a basic WHILE loop example. He could find similar examples in almost any C book and type it in. Even if he had stated "I need a while loop to do thisFill in blank I still would have just given the basic info that I did. If he really want to learn it he applies it to the assignment and/or plays with the code a little himself.
"I used to think the brain was the most amazing organ in the entire body. Then I realized who was telling me this."
If ignorance were painful, half the posters here would be on morphine drips.
zgirl
Grand Gerbil Poohbah
 
Posts: 3939
Joined: Tue Jan 01, 2002 7:00 pm
Location: The dark side of the moon

Postposted on Tue Feb 04, 2003 7:11 pm

To put to rest if this was a homework question the answer is no. I?m trying to re-learn C before I start taking my CS classes in college next year. I?ve always had problems when I had to use counters. I never really picked them up. So I?m trying to find out ways for me to become as familiar with them as I am with pointers.
malebolgia
Gerbil Elite
 
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: New Mexico, USA


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 2 guests