954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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.

m16jim
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Do you know what a loop is? Read this page:

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

cscgal
The Queen of DaniWeb
Administrator
19,423 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

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?

m16jim
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

yes

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>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
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

m16jim
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You