Wondering about if statements

Pages: 12
Hello Guys,

I am making a text adventure game, that uses a lot of classes, and definitely if statements. So mu question is this: Is there a way to 'break' out of an if statement, like the continue keyword for loops? I want this so I can go up the evaluation order for like a player going up to another room.

- Thanks Guys, RUNNER PRO AGARIO
I mean. If you need to break out of an if statement, then you're design is probably poor and you should rethink it. I found some info on some "hacks" that you can use, see if you find something useful - https://goo.gl/ObdomP

Edit:

I'd perhaps do it this way if I absolutely had to -

1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < 1; i++) // Put them in a for-loop that runs once, so you are allowed to use the keyword break
	{
		if (x == 5)
		{
			cout << "X is equal to: ";
			break;
			cout << x;
		}
// Not that it breaks out of the entire for-loop aswell.
	}
Last edited on
It's for a text adventure, exactly what I'm trying to do is this.

There is an integer name count.

Depending on count's value, you go into the specific if statement which gives a situation and then changes count.
Now I want to reevaluate count after each if statement.
Last edited on
It doesn't neccessarily have to be an if statement too, could it be something else?
The way to break out of an if statement is to write the } character.

You may also want to consider using an else clause in more complex scenarios.
Last edited on
@LB,

I want to exit an if statement before its real end.
Sounds like a typical loop scenario.

Are you aware of the fact that we have forums in which it is topical to ask C++ programming questions (as opposed to here, where it is not?)

@cire

Are you suggesting having a loop that checks the value of my variable count each iteration, and then evaluates each iteration? Then, yes, that's what I want.
I'm probably going to use that.

PS: This isn't really a programming question, because I am asking for debugging or anything, just my curiosity. It was my mistake to include parts about my adventure game though.
Last edited on
Wait, I implemented a solution, so I guess I'm done. Time to mark this solved.
PS: This isn't really a programming question, because I am asking for debugging or anything, just my curiosity. It was my mistake to include parts about my adventure game though.

The motivation for your question doesn't have anything to do with its subject matter.
Ok, Fine :+)
RUNNER PRO AGARIO wrote:
I want to exit an if statement before its real end.
There is never a reason to need this. It is nonsensical.
1
2
3
4
5
6
if(a) {
    execute_starting_code();
    if(!want_break) {
        execute_rest_of_code();
    }
}
let me tell you this in pseudocode

1. check variable count for value
2. Based on value of count, do corresponding if statement.
3. In that same if statement, change the value of count, such as incrementing or decrementing it.
4. Go to step 1.

I want to achieve this, but my initial question, NOW SOLVED, had me stuck at the start of step 4.
Last edited on
You can also just put this "step 1" in a function, and just call the function ¯\_(ツ)_/¯
Last edited on
No, but then won't count reset there?
Sounds like you want a for loop, not an if statement.
1
2
3
// note: does not handle conditions where an initialized declaration is contextually converted to bool
#define IFB( expression_evaluated_in_bool_context ) \
for( bool done = false ; !done && (expression_evaluated_in_bool_context) ; done = true ) 

http://coliru.stacked-crooked.com/a/e91c6738a7fa2530
thanks
I might be reading this wrong, but wouldn't a switch statement be most efficient?
Pages: 12