How's this look? loop?

How does this look before I submit? Thanks for reviewing.

Question
A for loop header that uses a counter variable has the form

for (int i = 0; i < n; i++)

Declare a counter variable i and rewrite the for loop header using the operators for the counter class.


1
2
3
4
5
6
7
8
9
10
 int i=0;

while(i<n)
{
int i = 0
int n = 10 
}

i++;
}
Your incrementer i++
Need to be inside the while loop or it will never end.

1
2
3
4
5
6
7
int i=0;
int n = 10

while(i<n)
{
i++;
}


Each time it goes through the loop it make i bigger, like 0<10 first time then 1<10 all the way until it hits 10<10 then it will stop and continue to the rest of the program.

Too add, don't re declare i. The i and n are not needed in the loops.
Last edited on
That isn't correct. There is three parts in a for statement. They are, in order, declaration, condition, repetition. In simpler terms, the first part is something that is declared before entering the loop. The second part is something that is check before the loop runs. And the third part is something that is done at the end of each loop.

Looking at your example, your for statement would really look like this:
for (int i = 0; i < 10; i = 1)

That doesn't quite look like what you were told to replicate.

Essentially, this is how it should look:
1
2
3
4
5
6
7
/* Declaration part goes here */

while (/* Condition part goes here */) {
  // Do some stuff

  /* Repetition part goes here */
}


Should be pretty easy to figure it out now.
Last edited on
Thank you for the suggestions, please forgive my ignorance with this. Can I have a for loop and a while loop in the same function? This is what I'm looking at now.

1
2
3
4
5
6
7
8
9
int i=0;
for (int i = 0; i < 10; i = 1)
while(i<n)
{
	int i = 0
	int n = 10 

	i++
}
Technically, you can nest as many loops as you want:
1
2
3
4
5
for (...;...;...)
   for (...;...;...)
      for (...;...;...)
         for (...;...;...)
            // Do stuff 


But I misunderstood the question. Why are you using a for loop when the instructions tell you specifically what to use?

Now that you know the parts of a for loop, modify the sections using the operators of the counter class.

I'm guessing that you have to initialize a counter somehow, but since I don't know what your class looks like, I'm just going to use a basic example:
1
2
3
4
5
6
for (myCounterClass myCounterObject(0); // Initializes MCO to 0
     myCounterObject.value() != myCounterObject.Max(); // Checks the current value against the maximum
     myCounterObject.increment()) // Increases the counter on each loop
{
   // Do some stuff here x amount of times
}


I hope that helps you figure out how to fix your problem. I'm hoping your teacher taught you how to use the counter class that you were given.
There is no additional information given for this problem. I posted it verbatim with what it says from the question. My interpretation of it was the counter variable was for (int i = 0; i < n; i++) and I had to create another variable (i) that will loop, with i starting at zero, adding 1, and not getting larger than (n).

But I could definitely be way off on that, I'm nowhere close to being fluent with programming.


Does that make sense to you? Creating the counter class as just n = 10
or is that something that probably wouldn't be in a introductory course.

Declare a counter variable i and rewrite the for loop header using the operators for the counter class.


According to that, there is a counter class, which, according to you, you have no other code. So, I'm going to just assume your teacher want's you to use an integer, or int.

However, if that's the case, your teacher has essentially already given you how to write a proper for loop. for (int i = 0; i < n; i++)

It says to rewrite it, but doesn't say how. If they want a for loop version of it, just use the same code, but change n to some value. If they want you to break apart the for loop into something that works the same way, refer to my code I pasted earlier.

Essentially, a for loop is a sophisticated while loop. Observe:
for ( /* Declaration */ ; /* Condition */ ; /* Repetition */ )

Translates to:
1
2
3
4
5
 /* Declaration */ ;

while ( /* Condition */ ) {
   /* Repetition */ ;
}


I can't just write your answer for you, but if you closely at where each part fits from the for loop structure to the while loop structure, I'm sure you'll get the right answer.

Edit: Presuming this is related to your other question, I assume counter is either a pseudo class, or one that you have been studying about but haven't shared the information, possibly because you don't know what to share. However, much like your other post, I'll take a stab in the dark about how to create a for loop with this unknown Counter class.

for (Counter myCounter = 0; !myCounter.atMax(); myCounter ++)

Either this, the first one I posted above, or some slight modification of either should be the right answer based on the context of what I've been reading from your posts. I assume these are homework questions for end of term that you either didn't do or are trying to cram in at the last minute. Either way, you should try to complete these kinds of problems when you're actually learning about them, either in class or out of the book.
Last edited on
This question doesn't make sense.
It looks like it wants you to copy and paste the example but throws in that "counter class" which doesn't make sense

The alternatives I can think of are rewriting the loop using a 'while' mimicking the 'for' behaviour or rewriting the loop declaring 'i' outside of it

A note: 'for' loop parts are "initialization; condition; increment". The first is not really a declaration because you don't HAVE to declare a counter there (I know I'm being annoying, sorry)
You're right on the initialization part, the term was just slipping my mind, however, the increment part isn't right. Take this for loop for example:
1
2
3
4
for (std::string myString; myVector.size() < 50; myVector.push_back(myString)) {
   std::cout << "Enter a student's name: ";
   std::cin >> myString;
}


Repetition, in my opinion is better since you don't always increment.

Another example:
1
2
3
4
 for (int i = 10; i > 0; i --)
   std::cout << i << "\n";

std::cout << "Blast off!\n";
Last edited on
You're right, repetition is more accurate. The way I said it is the one usually used
Topic archived. No new replies allowed.