Jump to content

  • Log In with Google      Sign In   
  • Create Account

for loop that will repeat the number of times the user wants.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 SchurtonStudios   Members   -  Reputation: 110

Like
0Likes
Like

Posted 11 January 2013 - 11:31 PM

How do i write a for loop that will repeat the number of times the user wants it too when asked by the console? 



Sponsor:

#2 Zaoshi Kaba   Crossbones+   -  Reputation: 4655

Like
3Likes
Like

Posted 11 January 2013 - 11:52 PM

It would be nice to know what language you're using.

 

I'll assume C++:

#include <iostream>

void main() {
    int repeat;
    std::cin >> repeat;
    std::cout << std::endl;

    for(int i = 0; i < repeat; i++)
        std::cout << "repeat\n";
}


#3 SchurtonStudios   Members   -  Reputation: 110

Like
0Likes
Like

Posted 12 January 2013 - 12:07 AM

sorry java thanks that helped figured it out 


Edited by SmallTallGiant, 12 January 2013 - 02:24 AM.


#4 Crusable   Members   -  Reputation: 594

Like
4Likes
Like

Posted 12 January 2013 - 02:23 AM

The code for the for loop will be the same but here:

 

 

 
//imported crap and other code
 
Scanner scan = new Scanner(System.in);
 
int numLoops;
 
System.out.print("How many loops?");
numLoops = scan.nextInt();
 
for(int i = 0; i < numLoops; ++i){
       
//do whatever you need to and loop as many times as the user entered for numLoops
}
 

 

 

you can do the same thing with a while loop (or do while), but the for loop has everything working in one line, where the while loops will take a couple.

 

Hope this helps smile.png


Edited by Mathew Bergen, 12 January 2013 - 02:24 AM.


#5 shadowomf   Members   -  Reputation: 323

Like
-1Likes
Like

Posted 12 January 2013 - 12:46 PM

where the while loops will take a couple

Remember C/C++ and as far as I know Java too, don't use line endings a seperators.

You can happily write your whole application in one line, as long as the compiler supports it and you don't miss any semicolons.



#6 Crusable   Members   -  Reputation: 594

Like
1Likes
Like

Posted 12 January 2013 - 02:44 PM


where the while loops will take a couple

 

Remember C/C++ and as far as I know Java too, don't use line endings a seperators.

You can happily write your whole application in one line, as long as the compiler supports it and you don't miss any semicolons.

 I understand his, however i said this because the only time i put more than one  "line" of code on one line, is in simple switch statements,

 

 
switch(choice){
 
     case 1: /*goto function*/ break;
 
     ...
}
 

 

and in the code that other people write, i have never seen more that one "line" of code on a  single line. And if you where to write a whole application on one line, it would be very confusing, unless it was a hello world program.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS