Script Languages Synopsis/if

From Wikibooks, open books for an open world
< Script Languages Synopsis
Jump to: navigation, search

if/unless control structure[edit]

Bash:

if list; then list; [ elif list; then list; ] ... [ else list; ] fi

Perl:

if (expr) {block} 
[elsif (expr) {block}]
[else {block}]
 unless (expr) {block}
[else {block}]

Ruby:

In Ruby, an if block is an expression, i. e., it returns the last evaluated value. The then keyword can be omitted, if a new line starts at this point.

if expr [then]
    expr...
[elsif expr [then]
    expr...]...
[else
    expr...]
end

Example:

 points = if outcome=='gewonnen' then 3 elsif outcome=='unentschieden' then 1 else 0 end

Python:

if expr:
    block
[ elif expr:
    block ]
[ else:
    block ]

if modifier[edit]

Perl:

statement if expr

statement unless expr

Ruby:

expr if expr