how to rewrite

(int i = 1; i <= 7; i++)

how do i write the function above differently?
What you have written above is not a function

But it strongly looks like something which follows for

(int i=1;i<8;++i)
right i have

void readTestScores(double & exam, double &tavge)
	double score, total = 0;
	cout << "ENTER EXAM SCORE\t:  ";
	cin >> exam;
	cout << "ENTER ALL TEST SCORES\t:  ";
	for (int i = 1; i <= 7; i++)
		cin >> score;
		total += score;
	tavge = (total / 7);

but is their another way i can write the for(int...)?
i dont know what the i's stand for and i was hoping i could put something else in their place
i is just a counter for the loop. The for statement means the loop will execute 7 times. It's a perfectly good way of performing a repeated action. Why do you want to change it?

for statements are one of the absolutely fundamental building blocks of C/C++ coding. If you're going be working with code at all, I strongly recommend you go back to your tutorial sources and learn about them.
Last edited on
thanks you
You're welcome.

I'm still curious as to why you want to change it.
maybe a homework question to use a while or do/while loop instead?
my teacher didnt go over i's so i replaced the i's with count and it worked
my teacher didnt go over i's so i replaced the i's with count and it worked

Is this a delayed April Fool's joke? Is it still April 1 somewhere in the world?

i is just the name of the variable. It's not a thing you have to learn about.
Last edited on
Count down instead of up (this can also be slightly quicker too)
that is not a function. If its a for loop you just write the enclosed statement 7 times. I'm not going to do it since it looks like a homework problem.
for (int i = 1; i <= 7; i++)

Here is what it does. For a better description you should be able to google any part you dont understand.

int acounter = 1; // a counter starting with 1
while ( acounter < = 7) // while loop, while counter is less than some value
Run below code
acounter++; // add 1 to acounter
Zaita wrote:
Count down instead of up (this can also be slightly quicker too)
What is your reasoning for this? I don't see how it could be any quicker since addition and subtraction take the same amount of time and you are still going through the same iterations, just in a different order. Grated, there might be rare cases where it is faster due to branch prediction, but the same is just as likely for increasing.
On particularly non-optimizing compilers on early x86 platforms, counting down was 1-2 CPU instructions shorter than counting up. It's irrelevant today, of course.

:) I actually found loops counting down will still be slightly faster even with modern compilers, but I work on high speed computing where 1 second of time saved per iteration equates to about 11 days saved real time.
Topic archived. No new replies allowed.