DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   for loop question (http://www.daniweb.com/forums/thread95936.html)

m16jim Nov 8th, 2007 1:53 am
for loop question
 
I have a question on an assignment that has me confused. The question is how many times does the following loop execute?

For I = 1 to 10
PRINT I
End For (I)

Now I am not sure if this is a trick question or not. I do not see a loop in the above statement, I = 1 to 10, print I would display 1,2,3,4,5,6,7,8,9,10 and then end. I do not see a loop.

Am I out in left field?? Any assistance would be greatly appreciated.

cscgal Nov 8th, 2007 1:57 am
Re: for loop question
 
Do you know what a loop is? Read this page:

http://en.wikipedia.org/wiki/For_loop

m16jim Nov 8th, 2007 2:02 am
Re: for loop question
 
I thought I knew what a loop was, I must have really mixed a few of these lessons up.

So then the

For I = 1 to 10
PRINT I
End For (I)

Does in fact state an actual loop and the 1 to 10 is the times the loop will execute? So it will loop 10 times?

WaltP Nov 8th, 2007 4:28 am
Re: for loop question
 
yes

Narue Nov 8th, 2007 10:43 am
Re: for loop question
 
>I have a question on an assignment that has me confused.
And confused you should be, because the question is ambiguous.

>The question is how many times does the following loop execute?
The real question is what language is that? If it's pseudocode that follows C++ rules, the loop will execute 9 times because 1 TO 10 probably denotes a half-open range [1,10) where the 10 is excluded (as is the convention in C++). So the output would be:
1 2 3 4 5 6 7 8 9
But the loop could also follow classic BASIC-style looping rules where the range is fully inclusive [1,10], and the 10 isn't excluded. In that case the loop will execute 10 times, and the output would be:
1 2 3 4 5 6 7 8 9 10

m16jim Nov 8th, 2007 7:20 pm
Re: for loop question
 
Narue, well I am really confused now, the code is in C++ pseudocode, so with that being said then the loop would execute 9 times?

Narue Nov 8th, 2007 8:00 pm
Re: for loop question
 
>so with that being said then the loop would execute 9 times?
That would be my guess, but this is definitely something you should ask your teacher about. Pesudocode is arbitrary; there aren't any hard rules, so for something like this you need to find out what's expected.

WaltP Nov 8th, 2007 8:50 pm
Re: for loop question
 
IMAO, the word to is inclusive of the two numbers, so it would execute 10 times. But that's just my opinion of that FOR command

Narue Nov 9th, 2007 9:57 am
Re: for loop question
 
>But that's just my opinion of that FOR command
Well, your opinion doesn't define how the loop works. Neither does mine. Only the teacher teaching that pseudocode can tell us what he expects the semantics to be.


All times are GMT -4. The time now is 9:50 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC