MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Python, Ruby, Perl: “if” Statement Example

Xah Lee, ,

Python

#-*- coding: utf-8 -*-
# python

# Examples of if

x = 1
if x == 1:
    print "x yes"

y = 2
if y == 1:
    print "y yes"
else:
    print "y no"

z = 2
if z < 0:
    print "z neg"
elif z == 0:
    print "z zero"
elif z == 1:
    print "z one"
else:
    print "z other"

http://docs.python.org/reference/compound_stmts.html#if

Ruby

#-*- coding: utf-8 -*-
# ruby

# Examples of if

x = 1
if x == 1 then p 'x yes' end

y = 2
if y == 1 then p 'y yes' else p 'y no' end

z = 2
if z < 0 then 
  p 'z neg'
elsif z == 0 then
  p 'z zero'
elsif z == 1 then
  p 'z one'
else
  p 'z other'
end

Ruby if statement is also a expression. It returns the last value of the executed block. “everything” in Ruby are also expressions.

Perl

#-*- coding: utf-8 -*-
# perl

# Examples of if

$x = 1;
if ($x == 1) {
  print "x yes\n";
}

$y = 2;
if ($y == 1) {
  print "y yes\n";
} else {
  print "y no\n";
}

$z = 2;
if ($z < 0) {
  print 'z neg';
} elsif ($z == 0) {
  print 'z zero';
} elsif ($z == 1) {
  print 'z one';
} else {
  print 'z other';
}

perldoc perlsyn

blog comments powered by Disqus