Selection ... choices
Things to debate - Remember.
Comments
- C - //
- Python - #
- PHP - // or #
- Bash - #
C
#include
<stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[]) {
int cats =
atoi(argv[1]);
if (cats >
3) {
printf ("%d is too many cats\n", cats);
} else {
printf ("%d cats is OK\n", cats);
}
}
Python
import
sys
cats = int(sys.argv[1])
if cats > 3:
print("%d is
too many cats" %(cats))
else:
print("%d
cats is OK" %(cats))
Bash
if [ $1
-gt 3 ];
then
echo $1 is
too many cats
else
echo $1 cats
is OK
fi
PHP
<?php
$cats =
$_SERVER['QUERY_STRING'];
if ($cats > 3) {
echo "$cats
is too many cats\n";
} else {
echo "$cats
cats is OK\n";
}
?>
Hands On
Copy the above code into the respective files:
- selection.c
- selection.py
- selection.sh
- selection.php
Run the programs
- Do the same as for sequence:
- Compile and run selection.c
- Add a shebang to the python and bash programs
- change the permissions to 755 and run with ./selection.py
<number> etc.
- Put seq1.php into your public_html directory and run
it from a browser with <yourmachine>/<yourlogon/~
Homework
Choose 2 of the 4 languages and write some simple selection code
that does something useful.
- Perhaps a message if the CPU temperature is above a
given value. CPU temp can be found with the following
simple script:
cpu=$(</sys/class/thermal/thermal_zone0/temp)
echo "$((cpu/1000))
c"
On the Pi running Raspbian the temperature can be found at at
the same place
- Or displays the time of day and says good morning, afternoon
or evening in Spanish, or Swahili, or whatever depending
on your preference. See appendix for info on date and
time. Let's assume sunset is 19:00 as actually finding
sunset is beyond the scope ...
Appendix
Date and time
- C -
- Python -
- Bash -ps date +%H:%M
- PHP -