C++ if statements

Status
Not open for further replies.
Now my for loops have issues. The machine just skips right over it. Before it I had a goto thing for demonstration but it just exploded in an infinite madhouse.
Code:
    int iii=0;
    for (;iii>10;){
    cout<<"HI"<<endl;
    iii++;
    }

You've probably got your iii variable because you have two other loops already taking i and ii. If you create your variable as Soul has you will be able to re-use i. C++ allows this syntax where C does not so you should confirm that you are using the correct compiler.

For a start you've set it to continue while iii is greater than 10. Seeing as it starts at 0, that'll never happen.
Code:
for(int iii=0;iii<10;i++){
cout<<"HI"<<endl;
}

Make sure you're incrementing the correct variable.
 
AHH RAGE!!!
Code:
    int iii=0;
    for (;iii<10;){
    if (iii==7){
        continue; //goes to the top of the loop
    }
    cout<<"HI"<<endl;
    iii++;
    if (iii==9){
    break;       //ends the loop at nine
    }
    }

The break is not only skipping the loop but is exiting the main function somehow. Please help.
 
Also how can you control the the scope of the random number generation. Instead of being this huge number. I know this is possible by putting two number inside math.random(1,13) for Lua but I dunno if it is possible in C++.
 
AHH RAGE!!!
Code:
    int iii=0;
    for (;iii<10;){
    if (iii==7){
        continue; //goes to the top of the loop
    }
    cout<<"HI"<<endl;
    iii++;
    if (iii==9){
    break;       //ends the loop at nine
    }
    }

The break is not only skipping the loop but is exiting the main function somehow. Please help.
If I read this correctly, it should be an endless loop since once you hit 7 iii will not be incremented again because the continue statement will bypass the iii++ line and iii will always be 7.

Why do you want to have the iii++ within the loop as opposed to in the For statement such as: for(int iii=0; iii<10; iii++)?
 
Also how can you control the the scope of the random number generation. Instead of being this huge number. I know this is possible by putting two number inside math.random(1,13) for Lua but I dunno if it is possible in C++.

You would use modulo to do something like this:

random_integer = (rand()%10)+1;

Which would produce a random number between 1 and 10. If you leave the +1 off the end it would be 0 to 9.
 
It's failing because of how you've written your for loop.
Strollin is right, once iii==7 it will bypass everything and just keep looping over and over. The 'continue' function, when used inside a for loop, skips to the end. Because you're not using the for loop to increment it'll just keep looping. If you change your code just slightly, like below, it'll work.
Code:
   int iii=0;
    for (;iii<10;iii++){
    if (iii==7){
        continue; //goes to the top of the loop
    }
    cout<<"HI"<<endl;
    if (iii==9){
    break;       //ends the loop at nine
    }
    }

All the first if statement there is doing is stopping "HI" from being sent when iii==7
Also, you don't need the last if statement (if iii==9). The for loop already has a stop condition (iii<10). The below code does exactly the same thing as the above
Code:
   int iii=0;
    for (;iii<10;iii++){
    if (iii==7){
        continue; //goes to the top of the loop
    }
    cout<<"HI"<<endl;
   }
 
Status
Not open for further replies.
Back
Top Bottom