views:

157

answers:

8

I've been wondering about alternative ways to write control structures like you can write your own language constructs in Forth.

One that you learn early on for if statements is a replacement for this:

if ( x ) {
   // true
} else {
   // false
}

with this (sometimes this is more readable compared to lots of brackets):

x ? true : false

It got me thinking. Can we replace anything else incase it's more readable.

So those are the ones I can think of off the top of my head for the if statement and doing comparisons.

So I'm wondering what about how to replace looping constructs like for, while, etc.

How would you replace a while loop for example (without using a for loop). It's probable that it can't be done in these languages?

while (a < b) {

}
A: 

it is not about control structures. You take case where if produces bool value. This is actually boolean expression wrapped into unneeded control structure. You can't rewrite generic if this way. It is like saying that

int c = 0;
for (int i = 0; i < 10; i++)
   c++;

can be rewritten as c = 10; it has nothing about control structures.

this excessive structures are produced because sometimes it is easier to write it as structure, but not try to produce single expression. this comes out of laziness and lack of thinking of programmers. Here is example. You need to calculate sum of incrementing numbers. What first comes to your head? Right:

int sum = 0;
for (int i = 0; i < 10; i++)
   sum += i;

Now try to find single expression how to count this. It is less obvious.

Andrey
What's the better phrase to use if not control structure? While I see your point, it's being rather anal.
Brock Woolf
@Brock Woolf: i am telling that your examples are not making control structure readable, but first using control structure where not needed, that converting it back to single expression.
Andrey
@ Andrey. My secondary code examples obviously led you astray, you were stating the bleeding obvious which isn't what I was trying to say. I've updated the question.
Brock Woolf
@Brock Woolf your new example code can be replaced only by another loop, like for (;a < b;) { ... }. Is it what you meant? If your question is all control structures can be replaced by expressions then answer is no. forth implements completely different paradigm then c#.
Andrey
@Andrey. I just meant can a while loop, etc be replaced with another construct. Looks like the answer is no.
Brock Woolf
@Brock Woolf it can be replaced with `for` or may be `if` and `goto`. but it can't be replaced with expression.
Andrey
A: 

Tabular Programming ?

Klaim
A: 

In some cases, you can put all logic into the third parameter of the "for-loop statement" and create a loop that looks empty but does some job (I am not saying that this is a good style). For example,

int t =0;
for (int i = 0; i++ <10; t+=10); // warning : don't forget  semicolon
a1ex07
+1  A: 

You're raising an interesting topic in your question: what is "boolean" in C-like programming?

The answer, in my opinion, is that boolean values (actually, boolean operations) are implicit control structures in C.

It comes from the "short circuit" rule - when left of && is false or left of || is true, the right is not computed at all. There is no way to implement this except with a conditional jump - any bitwise OR and AND is not sufficient. Also, consider comparisons: a > b is translated to compare and conditionally jump on most modern processors. It's not "store 1 in a register when a>b".

What you've discovered is that we use implicit control structures in logical operations and comparisons instead of explicit "if"s. Some languages take it one level further - consider this standard idiom in Perl:

open("myfile.txt") or die "Could not open file";
Arkadiy
The perl idioms can to a certain point be used in C also, but it is bad style imho. As for the instructions generated by the tests it depends and even with short circuit evaluation it happens that the compiler removes completly the branches or uses cmov instructions.
tristopia
A: 

You can also replace by boolean expressions:

 expr1 && expr2; 

is the same thing as

 if(expr1)
   expr2;

and

 expr1 || expr2;

is equivalent to

 if(!expr1) expr2;

so you could replace

 if(expr1)
   expr2;
 else
   expr3;

by

 expr1 && expr2;
!expr1 || expr3;

but why anyone would want to do that is a whole another question. My collegue loves this kind of thing, as he thinks that makes his programs more 1ee7 .

tristopia
A: 

Not to do with the languages you ask about, but you might want to take a look at BYOB, which is a fork of Scratch that allows you to create your own flow control structures (and other structures) using a graphical interface.

anon
@Neil Butterworth: Nice troll
Brock Woolf
@Brock I don't see how it is a "troll" - you mentioned FORTH in your question and I thought you were interested in flow control design in general.
anon
@Neil Butterworth: I'm interested in replacing constructs in C like languages. Vague consensus so far seems to be no. I really don't see how a 'graphical' control structure GUI has anything to do with that. Pointing me to something that looks like "Fisher price my first if statement" (used to teach kids in Kindergarten) seems like a troll to me.
Brock Woolf
@Brock I use both BYOB and Scratch, and I'm quite a bit out of kindergarten.
anon
+2  A: 

How would you replace a while loop

Loops can be replaced by recursion.

void doWhile(a, b) {
    /* do something with a and b, hopefully changing them */
    if (a > b) doWhile(a, b);
}
FredOverflow
Exactly what the type of thing I am looking for :) +1
Brock Woolf
Trading infinite loop to a stack overflow is really sweet ;-)
Vanya
@Vanya: Haha good way to think about it
Brock Woolf
A: 

There's also always the Smalltalk way of doing things. In Smalltalk, EVERYTHING is an object, including blocks of code. There is NO conditional command structure.

The Boolean Class has two subclasses, True and False (each with one object, true and false respectively). The conditional is a method sent to the boolean object with two arguments, a then block of code and an else block of code. The True object executes the then branch, the False object executes the else branch.

While loops are similar, for loops are a message to the integer object...

I think it's kind of neat.

Brian Postow