开发者

Alternative Control Structures

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开发者_StackOverflow's probable that it can't be done in these languages?

while (a < b) {

}


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);
}


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";


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.


Tabular Programming ?


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


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 .


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜