# No Branching Zone

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've really been wondering lately - imagine you are writing a game with C++ and you have no "if" or "?:" - or any other method of branching. What would be impossible, if anything? And what would be slower? I suspect some things will become faster and easier to understand... EDIT: Loops are allowed as long as you don't cheat and use it as an if, like: for( ; testCondition ; ) { if-branch } for( ; !testCondition ; ) {else-branch) EDIT part 2: Ok, let me simplify this - what if you can't use "if", "?:", "case", or any other kind of hackery that simulates those operations? EDIT part 3: My terminology was wrong - I mean "CONDITIONAL branch." (thanks doynax) From wikipedia: "A conditional branch is a basic logical structure. It resembles a fork in the road; there are at least two paths that may be taken and one must be chosen." A function call is not what I'm talking about because there is no logic to evalute in order to determine which function to call - even a virtual function is (I believe) just a lookup into the vtable, right? [Edited by - JoshM on August 11, 2005 7:59:05 PM]

##### Share on other sites
A game entirely without loops and (non-inline?) function calls would be kind of limited.
Basic boolean/arithmetic operators can often be used to synthesize conditional expressions though.

I suppose that it'd be theoretically possible develop anything like this for some kind of infinity machine.

##### Share on other sites
It would be impossible to do anything that requires a program to act on the outcome of a previous operation. It would be possible to implement certain things with number transformations rather than explicit branch instructions, but then you'd just be implementing branching on a more abstract level.

Quote:
 EDIT: Loops are allowed as long as you don't cheat and use it as an if, like:for( ; testCondition ; ) { if-branch }for( ; !testCondition ; ) {else-branch)

Loops with conditions are the equivalent of unconditional loops with an if(condition): break

##### Share on other sites
Quote:
 Original post by igni ferroqueIt would be impossible to do anything that requires a program to act on the outcome of a previous operation. It would be possible to implement certain things with number transformations rather than explicit branch instructions, but then you'd just be implementing branching on a more abstract level.

That's not exactly true. For example, here's a branch:

enum State{...} state;
switch( state )
{
case STATE_A: ProcessStateA(); break;
case STATE_B: ProcessStateB(); break;
etc...

And here's a non-branch:

State* state;
...
state->Process();

I wouldn't say that the second example is "a more abstract branch." It's a different paradigm. And if Process is a virtual function, then what happens when you call state->Process() depends entirely on the concrete class that state points to and thus depends on the result of a previous operation (ostensibly, assignment).

##### Share on other sites
Quote:
 Original post by JoshMThat's not exactly true. For example, here's a branch:enum State{...} state;switch( state ){ case STATE_A: ProcessStateA(); break; case STATE_B: ProcessStateB(); break;etc...And here's a non-branch:State* state;...state->Process();
I'd call them both a branches (including any kind of jump). It's just that the first is traditionally called a conditional branch.

##### Share on other sites
Updated original question to reflect clarifications based on feedback so far :)

##### Share on other sites
Quote:
 Original post by JoshMUpdated original question to reflect clarifications based on feedback so far :)
Hm.. I'm not so sure that you should listen to my definition of branches in this case.
Logically a straight jump isn't really a branch (as in a fork in the program flow), it's just that most CPU architectures tends to refer to both as branches anyway.

Quote:
 Original post by JoshMA function call is not what I'm talking about because there is no logic to evalute in order to determine which function to call - even a virtual function is (I believe) just a lookup into the vtable, right?
Yeah, I believe that you're technically correct ('the best kind of correct').
The true branch (if any) in this case isn't the invocation itself but rather the assignment of the function pointer.

##### Share on other sites
You could use arrays of function pointers:
typedef int (*func)(int);func[] fac_a = {fac, fac_0};int fac(int n) {    return fac_a[n==1](n-1) * n;}int fac_0(int n) {    return 1;}

##### Share on other sites
Quote:
 imagine you are writing a game with C++ and you have no "if" or "?:" - or any other method of branching.

Of course I missed the most obvious answer. The word "game" generally implies success and failure conditions.

Of course, this game (hehe) is pointless without a more formal definition of what counts as a branch. Pretty much any computation boils down to conditional branches on some level.

##### Share on other sites
Quote:
 Of course I missed the most obvious answer. The word "game" generally implies success and failure conditions.

Hmmm, I don't think about it that way - to me game implies only "conditions". Not necessarily only success and failure. There may be degrees of success and failure. Either way, let's say I have a guessing game where you have to guess my number. I could do it like this:

const char* responseCorrect = "Correct!";const char* responseWrong = "Wrong!";int i;const char* responseTable[10];for( i = 0; i < 10; ++i ) { responseTable = responseWrong; }responseTable[ randomNumberFrom0To9 ] = responseCorrect;int guessedNumber = getGuessedNumber();cout << responseTable[ guessedNumber ];

Admittedly it's a lame and contrived example, but it's possible at least :)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 12
• 17
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633358
• Total Posts
3011511
• ### Who's Online (See full list)

There are no registered users currently online

×