Inverted syntax: Difference between revisions

From Rosetta Code
Content added Content deleted
(initial content)
 
(Tcl doesn't do this)
Line 15: Line 15:
print 'Wow! Lucky Guess!' if ($guess == 6); # Inverted syntax (note missing braces)
print 'Wow! Lucky Guess!' if ($guess == 6); # Inverted syntax (note missing braces)
unless ($guess = 6) { print "Sorry, your guess was wrong!"; } # Traditional syntax
unless ($guess = 6) { print "Sorry, your guess was wrong!"; } # Traditional syntax
print 'Huh! You Guessed Wrong!' unless ($guess == 6); # Inverted syntax
print 'Huh! You Guessed Wrong!' unless ($guess == 6); # Inverted syntax</lang>
</lang>


{{omit from|Tcl|The language syntax really doesn't allow this.}}
[[Category:Inverted syntax]]

Revision as of 13:23, 31 May 2011

Task
Inverted syntax
You are encouraged to solve this task according to the task description, using any language you may know.

In traditional syntax conditional expressions are usually shown before the action within a statement or code block:

<lang psuedocode> IF raining=true THEN needumbrella=true </lang>

In inverted syntax, the action is listed before the expression in the statement or code block:

<lang psuedocode> needumbrella=true IF raining=true </lang>

The task is to demonstrate support for inverted syntax forms within the language by showing both the traditional and inverted forms.

Perl

<lang perl>if ($guess == 6) { print "Wow! Lucky Guess!"; }; # Traditional syntax print 'Wow! Lucky Guess!' if ($guess == 6); # Inverted syntax (note missing braces) unless ($guess = 6) { print "Sorry, your guess was wrong!"; } # Traditional syntax print 'Huh! You Guessed Wrong!' unless ($guess == 6); # Inverted syntax</lang>