Wondering about if statements

Pages: 12
Feb 6, 2016 at 4:34pm
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
Feb 6, 2016 at 4:45pm
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 Feb 6, 2016 at 4:52pm
Feb 6, 2016 at 5:04pm
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 Feb 6, 2016 at 5:04pm
Feb 6, 2016 at 5:14pm
It doesn't neccessarily have to be an if statement too, could it be something else?
Feb 6, 2016 at 5:40pm
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 Feb 6, 2016 at 5:41pm
Feb 6, 2016 at 5:56pm
@LB,

I want to exit an if statement before its real end.
Feb 6, 2016 at 5:56pm
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?)

Feb 6, 2016 at 5:59pm
@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 Feb 6, 2016 at 5:59pm
Feb 6, 2016 at 6:00pm
Wait, I implemented a solution, so I guess I'm done. Time to mark this solved.
Feb 6, 2016 at 6:05pm
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.
Feb 6, 2016 at 6:21pm
Ok, Fine :+)
Feb 6, 2016 at 8:42pm
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.
Feb 6, 2016 at 9:19pm
1
2
3
4
5
6
if(a) {
    execute_starting_code();
    if(!want_break) {
        execute_rest_of_code();
    }
}
Feb 6, 2016 at 10:41pm
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 Feb 6, 2016 at 10:42pm
Feb 6, 2016 at 10:43pm
You can also just put this "step 1" in a function, and just call the function ¯\_(ツ)_/¯
Last edited on Feb 6, 2016 at 10:44pm
Feb 6, 2016 at 10:46pm
No, but then won't count reset there?
Feb 7, 2016 at 2:53am
Sounds like you want a for loop, not an if statement.
Feb 7, 2016 at 3:55am
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
Feb 7, 2016 at 5:04pm
thanks
Feb 8, 2016 at 9:42am
I might be reading this wrong, but wouldn't a switch statement be most efficient?
Pages: 12