A constant If statement

Jun 7, 2011 at 2:38am
Hi, I have always wondered if it is possible to have an if statement keep running without having to constantly write it. I have tried using loops but that didn't go so well. For example I have the using input numbers into variables many times throughout the program, but every time he is given multiple choices, and each time one of the choices always does the same thing. I don't want to always have to write that statment.
Jun 7, 2011 at 2:39am
I'm not sure I understand what you are saying...can you give an example? (At least an attempt to show what you mean?)
Jun 7, 2011 at 3:01am
Let's say I have a variable named x. Now, I have the user input a new value to it 100 times in the program, and whenever the variable is 4 I want it to say "you guessed rite!" But, that's a lot of if statements to write! I was asking if there was a way to have 1 if statement check itself constantly.
Jun 7, 2011 at 3:20am
...Like this?
1
2
3
4
5
6
7
while (/*...*/){
    int a=input();
    if (a>9000)
        //do something
    else
        //do something else
}
Last edited on Jun 7, 2011 at 3:20am
Jun 7, 2011 at 7:05am
closed account (zb0S216C)
An expression within a if statement cannot change, only the result.

jmlpor98 wrote:
Now, I have the user input a new value to it 100 times in the program, and whenever the variable is 4 I want it to say "you guessed rite!" But, that's a lot of if statements to write!

You only need one if statement, and that's to check if the user entered the value of 4.

1
2
3
4
5
6
7
int nInput;
for( int nLoop( 0 ); nLoop < 100; nLoop++ )
{
    cin >> nInput;
    if( nInput == 4 )
        cout << "you guessed rite!" << endl;
}


Wazzak
Topic archived. No new replies allowed.