# Using while loop for if statement.

• 10-02-2008
Galens
Using while loop for if statement.
I'm having a bit of trouble here trying to make this if structure loop if an unknown entry is made. When I enter the unknown in the gui (else statement) it will loop the else statement, but not the beginning (if statement) My variable is predeclared local
Code:

`int g2le = 0;`
Much appreciated.

Code:

```while ( g2le == 0 ){     if ( g2s == 1 ){         cout<< g;         g2le++;         cin.get();         }     else if ( g2s == 2) {         cout<< h;         g2le++;         cin.get();         }     else if ( g2s == 3){         cout<< i;         g2le++;         cin.get();         }     else {         cout<< j;         cin.get();         }         }         cin.get(); }```
Quote:

Program initiated successfully

This is the start of the program. We are using cout to display this message. Ple
ase press enter to continue after each code block has been executed.

This part of the program will use 'int' to declare variables with user input. Pl
ease, enter a number.
3
The number you entered will be displayed right now: 3 See?

So now we seen an example of how the code will enter a value through cin, and re
place the value of the variable with your input. This is a local variable assign
ment. It can constantly change as I will show you in a few moments.

Please enter another value for a.
4
The variable 'a' is now: 4 instead the previous number you entered

Now we will move onto assigning two variables at once for an 'int'. Please enter
two numbers into the prompt. Place a space between each number please.
2 3
The numbers you entered are: 2 and 3.

Now we will take these numbers and begin using math to solve different types of
problems.

2 x 3 equals: 6
2 + 3 equals: 5
2 - 3 equals: -1
2 / 3 equals: 0.666667

Now I will demonstrate how the 'if' statement works well with passwords or other
various program functions.

Lets pay a little game here, lets imagine you are in a role playing game and you
have several options to choose from. Lets call them:

1.Attack
2.Defense
3.Magic

Go on, choose one by using numerical values
4
!Error! Unknown Command!!Error! Unknown Command!
!Error! Unknown Command!
!Error! Unknown Command!
!Error! Unknown Command!
!Error! Unknown Command!
The program is not to describe anything to anyone but to help myself become more involved into coding.
• 10-02-2008
tabstop
Well, if g2s is not 1, 2, or 3, how does g2le change (to get out of the loop) or g2s change (to pick a different choice in the if-elseif-elseif-else chain)?
• 10-02-2008
Galens
I want the last else if to repeat the external loop, not internal. The other if, else if I want it to break the loop and continue to the next code block.

I'm a little confused here. I tried this and I don't understand why its not working. On top of the else if repeating its own code block, the 1,2,3 values won't pause even though I'm using the cin.get();

Code:

```while ( g2le < 1 ){     if ( g2s == 1 ){         cout<< g;         g2le++;         cin.get();         }     else if ( g2s == 2) {         cout<< h;         g2le++;         cin.get();         }     else if ( g2s == 3){         cout<< i;         g2le++;         cin.get();         }     else if ( g2s != 1,2,3){         cout<< j;         g2le--;         cin.get();         }         }         cin.get(); }```
• 10-03-2008
Galens
So far I got it fixed where it doesn't exit the gui upon any value entered. I'm using break statements after the cin.get(); in each internal block to prevent this. I tried continue inside the else block thinking it would continue the loop...guess not. I had to switch back from:

Code:

`while ( g2le < 1 )`
To:

Code:

`while (g2le == 1)`
The transition back prevented the program from exiting on all value inputs. I also changed back from the last block (else if) back to (else) I had previously tried

Code:

`else if (g2s != 1,2,3)`
Code:

`else if (g2s != 1 || 2 || 3)`
Here is where I am so far:

Code:

```while ( g2le == 1 ){           int g2le = 1;     if ( g2s == 1 ){         cout<< g;         g2le = g2le++;         cin.get();         break;         }     else if ( g2s == 2) {         cout<< h;         g2le = g2le++;         cin.get();         break;         }     else if ( g2s == 3){         cout<< i;         g2le = g2le++;         cin.get();         break;         }     else {         cout<< j;         g2le = g2le--;         cin.get();         continue;         }         }         cin.get(); }```
Maybe someone can shed some light for me here. I'm stumped and still cannot resolve this issue. Much appreciated
• 10-03-2008
iMalc
Quote:

Originally Posted by Galens
I'm having a bit of trouble here trying to make this if structure loop if an unknown entry is made

'if' statements don't loop. 'while', 'do..while' and 'for' statements can loop.
Quote:

So far I got it fixed where it doesn't exit the gui upon any value entered. I'm using break statements after the cin.get(); in each internal block to prevent this. I tried continue inside the else block thinking it would continue the loop...guess not.
A 'continue' statement does exactly what it implies. It jumps immediately to the next iteration of the loop.
Quote:

The transition back prevented the program from exiting on all value inputs. I also changed back from the last block (else if) back to (else) I had previously tried

Code:

`else if (g2s != 1,2,3)`
Code:

`else if (g2s != 1 || 2 || 3)`

The syntax you are after is:
Code:

`else if (g2s != 1 && g2s != 2 && g2s != 3)`
Or to put it a shorter way:
Code:

`else if (g2s < 1 || g2s > 3)`
Quote:

Here is where I am so far:
Code:

```...         g2le = g2le++; ...```

That line is undefined behaviour. You can't modify g2le twice in the same statement. You want just:
Code:

`g2le++;`
• 10-03-2008
Galens
I did it!!!!!

I noticed a common sense error I made before the while. I wasn't asking for input inside the while, which was preventing any new values to be entered after the cin>> g2s; It was retaining the value 4(unknown command) and repeating the while with the that value. I hope I explained that correctly....

Fixed:

Code:

```while ( g2le == 1 ){     cout<<"1.Attack\n2.Defense\n3.Magic\n";     cin>> g2s;        if ( g2s == 1 ){         cout<< g;         g2le++;         cin.get();         break;         }     else if ( g2s == 2) {         cout<< h;         g2le++;         cin.get();         break;         }     else if ( g2s == 3){         cout<< i;         g2le++;         cin.get();         break;         }     else {         continue;         }         }         cin.get(); }```
Thanks all who helped!