-3

I'm new at C++ programming, so I have a newbie question.

If the value of int b is the user input, how would I create a loop that would run only "b" amount of times?

Example:

  1. User enters "5".
  2. The loop will run 5 times.
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3390915
  • 45
  • 1
  • 1
  • 3

4 Answers4

3

You may eventually wish to change things a little here (e.g. ++i, etc.) and there to suit your various needs as you get more familiar and experienced with coding.

For now, in typical usage...

You can use a for loop:

for ( int i = 0; i < b; i++ ) {
    // do something here
}

Take note that you start from i = 0 in your first loop. In your second loop, i = 1, and so on so forth. In your last b-th loop, i = b-1. The i++ in the for loop means that i will be incremented by one at the end of each loop, automatically; there is no need for you to write another statement (such as i = i + 1) to increment i inside the loop.

Or, you can use a while loop.

while (i < b) {
    // do something here
    i++;
}

In the while loop, you have to manually and explicitly increment i yourself at the end of the loop.

If you need more help, you can refer to tutorials online for more examples, such as: http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

If your interested, you can also take a look at the do-while loop: http://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htm

Tacocat
  • 1,134
  • 8
  • 23
1
int b;
cin>>b;
#taking user input for value of b
for(int i=0;i<b;i++){
    #do whatever you want
}

This loop runs for b number of times.

the_unknown_spirit
  • 2,518
  • 7
  • 34
  • 56
  • 1
    If the OP can't perform a simple loop, they aren't likely to know what `cin` is. – Tas Feb 15 '16 at 05:04
  • SO is not a platform to ask such primitive and basic questions. You are supposed to do your own part of research and then ask questions on SO. Even if the OP doesn't understand why `cin` has been used , its the way I have taken user input in the code in C++ just as the user input he said in the question. – the_unknown_spirit Feb 15 '16 at 05:14
  • 2
    _SO is not a platform to ask such primitive and basic questions._ The question is literally "how do I loop _n_ times". If you believe that SO is not the place to ask, why are you encouraging these questions by answering? – Tas Feb 15 '16 at 05:17
  • I am not encouraging this type of question. I just answered the query. Answering a question and helping someone struggling with that doesn't mean I am encouraging asking such questions here. Please don't get offended. – the_unknown_spirit Feb 15 '16 at 05:19
  • 2
    IMO, answering IS encouraging, but you are entitled to do what you will. – Tas Feb 15 '16 at 05:23
0
for( int a = 0; a < **b**; a = a + 1 )
   {
       // your code goes here
   }
Stanley Kirdey
  • 602
  • 5
  • 20
0

Use An variable which we can increase and after specific target loop will stop executing. ex- In following code I used variable int times, which increases +1 every time when my condition fails. when this variable reaches specified target it stops executing

'''

        int times = 0;
         while (times < 2000 && other condition)
         {
             //Your Code

             if (An Condition){
                 //Your Code
                 break;
             }
             else{
                 times += 1;
                 continue;
             }
          }
    

'''