Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Loop question

loop

  • Please log in to reply
6 replies to this topic

#1 trojansc82

trojansc82

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 26 October 2008 - 05:17 PM

I had a question from a quiz that I got incorrect, but unfortunately it didn't show the correct answer at the end. Here is the question and my answer:

Which one of the following correctly defines a loop to execute exactly 10 times?

for (int a = 1, a == 10, a++) {}


Anybody know what a correct answer in this form would be?
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 October 2008 - 06:44 PM

The reason your loop fails is because when a==1, it will fail the test a==10 and exit. There are several possible correct answers, such as using a<=10 or a<11 for your test.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 gammaman

gammaman

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 70 posts

Posted 27 October 2008 - 01:20 PM

In order for it to do that the easiest you would start at int i = 0
  • 0

#4 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 27 October 2008 - 01:35 PM

I had a question from a quiz that I got incorrect, but unfortunately it didn't show the correct answer at the end. Here is the question and my answer:


Anybody know what a correct answer in this form would be?



Your code
for(int a = 1, a == 10, a++) {}
Which doesn't seem to be logical nor be in "java" way structured.
As for the loop, how can a be 1 from one point and then 10 in another?
I can see you have given little time with it.

My code
for(int a=1; a<10; a++)
It will begin from start 1 and end it when it reaches 10. This is logical correct and "java" correct. Start point and the "end" point(lol "end point").

Anyways see you later alligator.
  • 0

#5 niaz0099

niaz0099

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 02 November 2008 - 11:31 PM

I got incorrect, but unfortunately it didn't show the correct answer at the end. Here is the question and my.
  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 November 2008 - 06:28 AM

You didn't include anything, niaz.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 Steve.L

Steve.L

    CC Addict

  • Member
  • PipPipPipPipPip
  • 382 posts
  • Location:Kingston, Canada
  • Programming Language:C, Java, C++, PHP, Python, Ruby, PL/SQL, Delphi/Object Pascal, Lisp, Pascal, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 03 November 2008 - 10:23 AM

Your code

for(int a = 1, a == 10, a++) {}
Which doesn't seem to be logical nor be in "java" way structured.
As for the loop, how can a be 1 from one point and then 10 in another?
I can see you have given little time with it.


There is no proper "Java" structure, you idiot. Don't go planting misinformation in new programmers' heads.

My code

for(int a=1; a<10; a++)
It will begin from start 1 and end it when it reaches 10. This is logical correct and "java" correct. Start point and the "end" point(lol "end point").

Anyways see you later alligator.


His question specifically stated that the loop is to execute ten times, and yours executes nine.

Epic Logic Fail!

Oh, and you fail at English, too.

@OP:
Listen to Panther, who actually knows what he's talking about.
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download