2

Just out of curiosity, does anyone know of a language that uses a "reverse" if/then statement?

For example,

if(a == b) {
    doC();
}

in most languages would be

{
    doC();
} if(a == b)

in this language.

I know it won't work in most languages (like C#, for instance) that execute up to down, left to right, but are there any languages that use or support such a syntax?

MatthewSot
  • 3,516
  • 5
  • 39
  • 58
  • 2
    Vax Basic used to allow it. There were a few others, back in the day. – RBarryYoung Apr 28 '14 at 03:48
  • Just to clarify, there is nothing more "up to down, left to right" about C#. C syntax isn't special compared to Perl, ruby, or python, which all have this feature. This would work just fine with C# syntax. It's syntax which is separate from the execution. The implementers just didn't add it. – mmachenry Apr 28 '14 at 23:10
  • good to know, thanks! probably shouldn't make assumptions about languages based on a quick skim of some docs ;) – MatthewSot Apr 29 '14 at 00:10

4 Answers4

5

Sure, you can do this in Perl. This:

if (a == b) {
    doC();
}

Can be written this way:

do {
  doC();
} if (a == b);

It’s often used to make preconditions easier to read:

while (<>) {
  last if /<END>/;  # Exit loop if current line contains "<END>".
  print;
}

And Ruby borrows this feature from Perl.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
1

There's a A if cond else B statement in Python

For example

print('kid') if age < 18 else print('adult')
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • This is just an ordinary expression statement that happens to use the `… if … else …` conditional operator. This example works in Python 3 where `print` is a function, but not in Python 2. – Jon Purdy Apr 28 '14 at 04:09
  • 1
    @JonPurdy then what's the problem with this? The OP asked about a language that has reversed if condition – phuclv Apr 28 '14 at 04:10
1

Ruby, Python (as Lưu Vĩnh Phúc said) support that kind of style.
This is Ruby's way:

puts :hello if true

In the Moonscript there are "line decorators" :

print "hello world" if name == "Rob"

They differ a little from the Ruby's ifs so I put them into another category.

Another group are concatenative or stack based programming languages. You can read more about it here.
They do not exactly do what you described but they are close.
In the Factor you can write:

10 3 <
[ "yes" ]
[ "no" ]
if

I believe that syntax where the condition is just before if is possible. After all 10 3 < is evaluated to the f (Factor's false value) so it does not matter where f is on the stack.

Darek Nędza
  • 1,420
  • 1
  • 12
  • 19
0

How about the do while loop in php? Of course, the code would always get executed at least once within the do loop but it partially does what you might be looking for.

<?php
$i = 0;
do {
    echo $i;
} while ($i > 0);
?>
phuclv
  • 37,963
  • 15
  • 156
  • 475
ProfileTwist
  • 1,524
  • 1
  • 13
  • 18
  • this is pretty much what I was thinking about, and do/while works in most languages (javascript & c# off the top of my head), but I was more looking for something that (as you pointed out) will only get executed if the statement is true. do/while still works in C# and makes sense because it's still reading up-down, left-right. I'm looking for a language that doesn't follow that pattern – MatthewSot Apr 28 '14 at 03:53
  • Any particular reason why you are looking for a language that digresses from the traditional top/down parsing methodology? – ProfileTwist Apr 28 '14 at 03:58