© 2017 Morgan C. Benton code4your.life
One of the most basic elements of programming is the ability to control the order in which instructions are executed, how many times they are executed, or if they are executed at all.
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
if (/* conditional expression */) {
// code to execute IF the
// conditional expression
// is TRUE
} else {
// code to execute otherwise
}
switch (/* variable */) {
case X:
// code to execute if the
// variable == X
break;
case Y:
// code to execute if the
// variable == Y
break;
default:
// code to execute if the
// variable did NOT equal
// X or Y
}
Expressions that evaluate to TRUE or FALSE
© 2017 Morgan C. Benton code4your.life
Values that are generally evaluated as "true"
Values that are generally evaluated as "false"
© 2017 Morgan C. Benton code4your.life
if (/* conditional statement */) {
// do this...
}
if (/* conditional statement */) {
// do this...
} else {
// do this...
}
if (/* conditional statement */) {
// do this...
} else if (/* another conditional */) {
// do this...
} else {
// do this...
}
switch (var1) {
case val1:
// do this...
break;
case val2:
case val3:
// do this...
break;
default:
// do this...
}
if (var1 == val1) {
// do this...
} else if (var1 == val2 || var1 == val3) {
// do this...
} else {
// do this...
}
© 2017 Morgan C. Benton code4your.life
if (/* conditional statement */) {
// do this...
}
if (/* conditional statement */) {
// do this...
} else {
// do this...
}
if (/* conditional statement */) {
// do this...
} elseif (/* another conditional */) {
// do this...
} else {
// do this...
}
switch ($var1) {
case $val1:
// do this...
break;
case $val2:
case $val3:
// do this...
break;
default:
// do this...
}
if ($var1 == $val1) {
// do this...
} elseif ($var1 == $val2 || $var1 == $val3) {
// do this...
} else {
// do this...
}
© 2017 Morgan C. Benton code4your.life
if /* conditional statement */ {
// do this...
}
if /* conditional statement */ {
// do this...
} else {
// do this...
}
if /* conditional statement */ {
// do this...
} else if /* another conditional */ {
// do this...
} else {
// do this...
}
switch var1 {
case val1:
// do this...
case val2:
case val3:
// do this...
default:
// do this...
}
if var1 == val1 {
// do this...
} else if var1 == val2 || var1 == val3 {
// do this...
} else {
// do this...
}
© 2017 Morgan C. Benton code4your.life
if conditional:
# do this...
if conditional:
# do this...
else:
# do this...
if conditional1:
# do this...
elif conditional2:
# do this...
else:
# do this...
# Python does not have a
# switch/case statement
© 2017 Morgan C. Benton code4your.life
if conditional then
# do this...
end
if conditional then
# do this...
else
# do this...
end
if conditional1 then
# do this...
elsif conditional2 then
# do this...
else
# do this...
end
unless conditional then
# do this...
else
# do this...
end
case var
when val1
# do this...
when val2, val3
# do this...
else
# do this...
end
if var == val1
# do this...
elsif var == val2 or var == val3
# do this...
else
# do this...
end
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
for (/* initializer(s) */; /* end condition */; /* increment */) {
// do this...
}
// typical example
for (i = 0; i < 10; i += 1) {
// do this 10 times
}
// this type of loop is common in Java, JavaScript, PHP, and Go
// BE CAREFUL with the syntax, which varies slightly across languages
© 2017 Morgan C. Benton code4your.life
// PHP
foreach($items as $key => $item) {
// loops over each item in the
// array of items; $key is set
// to the array index or object
// property name
echo $item;
echo $items[$key];
}
// JavaScript
for(var i in items) {
// loops over each item in the
// array of items; i is set
// to the array index or object
// property name
console.log(items[i]);
}
for(var item of items) {
// loops over each item in the
// array of items; item is set
// to the value of each of the
// items in the collection
console.log(item);
}
# Ruby
items.each do |item|
puts item
end
# Python
for item in items:
print(item)
© 2017 Morgan C. Benton code4your.life
# Python
for i in range(1, 6):
# iterates 5 times over
# values 1 to 5
print(i)
# Ruby
for i in 1..5
# iterates over
# values 1 to 5
puts i
end
5.times do |i|
# iterates over
# values 0 to 4
puts i
end
1.upto(5) do |i|
# iterates over
# values 1 to 5
puts i
end
# R
for (i in 1:5) {
# iterates over
# values 1 to 5
print(i)
}
© 2017 Morgan C. Benton code4your.life
i = 0;
while (i < 10) {
// do something
// incrementer inside
// the loop block
i += i;
}
// NOTE: do..while ALWAYS executes
// at least once, since condition is
// not checked until the end
i = 10;
do {
// do something
// incrementer inside
// the loop block
i += i;
} while (i < 10);
© 2017 Morgan C. Benton code4your.life
// JavaScript
// .map() applies a function to
// all elements of an array
var cm2in = x => x * 0.3937;
var cms = [10, 15, 20];
var ins = cms.map(cm2in);
// ins == [3.937, 5.905, 7.874]
# R
cms <- c(10, 15, 20)
ins <- sapply(cms, function(x) x * 0.3937)
[1] 3.9370 5.9055 7.8740
© 2017 Morgan C. Benton code4your.life
// off-by-one error example
for (i = 0; i <= 5; i += 1) {
// how many times does this
// code get executed?
console.log(i);
}
// output
0
1
2
3
4
5
// 6 times!
// infinite loop example
for (i = 1; i != 10; i += 2) {
// how many times does this
// code get executed?
console.log(i);
}
// output
1
3
5
7
9
11
...
// infinite times!
© 2017 Morgan C. Benton code4your.life
// in JavaScript (very similar in Java and PHP)
try {
// do something risky that might cause
// an error at runtime
} catch(err) {
// handle the error if something goes
// wrong so your program doesn't just
// unceremoniously quit on you
}
© 2017 Morgan C. Benton code4your.life