# Using the for loop

• 01-30-2006
Kenny77
Using the for loop
I am programming this test commentor and I am trying to get it to restart again but I can't figure it out.

Code:

```#include <iostream> using namespace std; int main() {     int percentage, i;         cout<<"Input your test percentage: ";     cin>>percentage;         cin.ignore();         if (percentage<50){                       cout<<"You failed. Study harder.\n";                       }     else if (percentage<60){                             cout<<"You just passed. Study harder.\n";                             }     else if (percentage<70){                             cout<<"You didn't do very well.\n";                             }     else if (percentage<80){                             cout<<"You did quite well.\n";                             }     else if (percentage<90){                             cout<<"You did excellent.\n";                             }     else if (percentage<100){                             cout<<"You did very well.\n";                             }     else if (percentage>100){                             cout<<"Who marked your test?\n";                             }     else if (percentage==100){                             cout<<"You did perfect.\n";                             }     cin.get(); }```
What do I put in and where?
• 01-30-2006
Salem
Code:

```while ( someCondition ) {   // the stuff you want to repeat }```
• 01-30-2006
taku
A for loop is generaly used when you have a set number of times the loop should be executed. (There are "tricks" you can use on them but that's another discusion)

A while loop is generaly used to execute as long as a condtion is true, a do while loop is the same as a while loop however it will execute at least once.

The syntex for a for loop is

for (variable initialization; conditional; variable increment) // the varable initialization is normaly an int.

Code:

```for example for ( int i =0; i<Max_Percents; i++) {   the code you want to repeat }```

if you want to the user to enter data, indefinitly you have two options.

1. You can have a Sentinel- Controlled Loop, which has the user enter a unique value to end the loop
Code:

```do {  your code }while (input != sentinal);```
2. You can put your input into a while statment, to end the loop you would press cntl+d. This is more benificial for reading from files, then user input though

ex:
while (cin>>percentage)
• 01-31-2006
74466
I just had to, i modified the code to the only way i know how to repeat a program,
Code:

```#include <iostream> using namespace std; int main() {     int percentage, i, x;     x=1;         while(x==1)     {             cout<<endl;         cout<<"Input your test percentage: ";     cin>>percentage;         cin.ignore();         if (percentage<50){                       cout<<"You failed. Study harder.\n";                       }     else if (percentage<60){                             cout<<"You just passed. Study harder.\n";                             }     else if (percentage<70){                             cout<<"You didn't do very well.\n";                             }     else if (percentage<80){                             cout<<"You did quite well.\n";                             }     else if (percentage<90){                             cout<<"You did excellent.\n";                             }     else if (percentage<100){                             cout<<"You did very well.\n";                             }     else if (percentage>100){                             cout<<"Who marked your test?\n";                             }     else if (percentage==100){                             cout<<"You did perfect.\n";                             }     cout<<"\n \n \n To try again press 1, or press any key to exit!\n";     cin>>x;     cin.ignore();         }     }```
now i am sure there is another more "Clean" way of doing it, if so could anybody please explain?

And would it not be better to use something like
Code:

`else if(percentage<90&&percentage>79){`
Just askin as thats how i would do it
• 01-31-2006
7stud
First things first: you have to learn to indent things properly. Do a google search, and examine some sample code.
• 01-31-2006
dwks
Quote:

And would it not be better to use something like
Code:

`else if(percentage<90&&percentage>79){`

You don't need to, because it's an else if, and the previous ifs have made sure that percentage is more than 79.

Look at this program.
Code:

```#include <iostream> #include <cctype> using std::cout; using std::cin; using std::tolower; int main(void) {     char again;     do {         do_something();         cout << "Do that again? ";         cin >> again;     } while(tolower(again) == 'y'); }```
tolower() converts a letter to lowercase (there's also a toupper). These functions are in <cctype>.