Difference between revisions of "Block Syntax"
(Created page with "==Block Syntaxis== C syntax with starting bracket on same line as the conditional statement: if (x==3) { echo "three!"; return; } C syntax with the starting brac...") |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | Most programming languages use curly brackets <tt>{ }</tt> to group statements, forming a block. However, this has the inherent problem that it is often a fight between style fanatics to either save space and write it on the same line as the condition, or style fanatics to write it on a seperate line, aligning the opening and closing brackets. There are better alternatives for curly brackets (most language designers feel that curly brackets where a terribly idea in the first place). Bellow are some alternatives. | |
C syntax with starting bracket on same line as the conditional statement: | C syntax with starting bracket on same line as the conditional statement: | ||
if (x==3) { | if (x==3) { | ||
− | + | printf("three!"); | |
return; | return; | ||
} | } | ||
Line 10: | Line 10: | ||
if (x==3) | if (x==3) | ||
{ | { | ||
− | + | printf("three!"); | |
return; | return; | ||
} | } | ||
Line 17: | Line 17: | ||
if (x==3) | if (x==3) | ||
{ | { | ||
− | + | printf("three!"); | |
return; | return; | ||
} | } | ||
− | Pascal | + | Pascal syntax: begin and end instead of { }: |
if (x=3) then | if (x=3) then | ||
begin | begin | ||
− | + | writeln("three!"); | |
return | return | ||
end; | end; | ||
− | + | Shell syntax: as Pascal, but with if/then/fi instead of if/begin/end: | |
− | if (x===3) : | + | if [ $x -eq 3 ] |
+ | then | ||
+ | echo "three!"; | ||
+ | return 0; | ||
+ | fi | ||
+ | |||
+ | Shell syntax: with if/then on the same line: | ||
+ | if [ $x -eq 3 ]; then | ||
+ | echo "three!"; | ||
+ | return 0; | ||
+ | fi | ||
+ | |||
+ | Ruby: remove the spurious then from the condition/then/end: | ||
+ | if x == 3 | ||
+ | print "three!" | ||
+ | return | ||
+ | end | ||
+ | |||
+ | [http://php.net/manual/en/control-structures.alternative-syntax.php PHP alternative control structure syntax], similar to Ruby, but using endif using specific end-keyword per condition: | ||
+ | if (x === 3): | ||
echo "three!"; | echo "three!"; | ||
return; | return; | ||
Line 35: | Line 54: | ||
Python syntax: indentation implies nesting, no need for end or } syntax: | Python syntax: indentation implies nesting, no need for end or } syntax: | ||
− | if (x==3) | + | if (x==3): |
− | + | print("three!") | |
return | return | ||
Latest revision as of 12:23, 22 March 2012
Most programming languages use curly brackets { } to group statements, forming a block. However, this has the inherent problem that it is often a fight between style fanatics to either save space and write it on the same line as the condition, or style fanatics to write it on a seperate line, aligning the opening and closing brackets. There are better alternatives for curly brackets (most language designers feel that curly brackets where a terribly idea in the first place). Bellow are some alternatives.
C syntax with starting bracket on same line as the conditional statement:
if (x==3) { printf("three!"); return; }
C syntax with the starting bracket aligned with the closing bracket:
if (x==3) { printf("three!"); return; }
C syntax, with Whitesmiths style C indentation, arguing that { and } are not part of the if statement:
if (x==3) { printf("three!"); return; }
Pascal syntax: begin and end instead of { }:
if (x=3) then begin writeln("three!"); return end;
Shell syntax: as Pascal, but with if/then/fi instead of if/begin/end:
if [ $x -eq 3 ] then echo "three!"; return 0; fi
Shell syntax: with if/then on the same line:
if [ $x -eq 3 ]; then echo "three!"; return 0; fi
Ruby: remove the spurious then from the condition/then/end:
if x == 3 print "three!" return end
PHP alternative control structure syntax, similar to Ruby, but using endif using specific end-keyword per condition:
if (x === 3): echo "three!"; return; endif;
Python syntax: indentation implies nesting, no need for end or } syntax:
if (x==3): print("three!") return