School of Computing. Dublin City University.
Online coding site: Ancient Brain
coders JavaScript worlds
#!/usr/bin/bash for (( i=1 ; i <= 10 ; i++ )) do echo $i done j=5 echo $j (( j++ )) echo $j (( j = $j + 1 )) echo $j
#!/usr/bin/ksh integer i i=1 echo $i i=i+10 echo $i i=i-4 echo $i i=i*4 echo $i i=i/2 echo $i
flag=0 if condition then flag=1 fiYou can test if equality and inequality conditions are true.
if test $1 -lt 50Using the "expr" command, you can do arithmetic:
# x = $1 + $2 x=`expr $1 + $2`So despite the fact that the Shell environment variables here are only text strings, and have no types, we can use other programs (test, expr) that interpret their text string arguments in certain ways, and so we can use them as numeric types after all.
flag=0 echo $flag while test $flag -lt 30 do flag=`expr $flag + 1` echo $flag donewhich is equivalent to the following in C++:
int flag = 0; cout << flag << "\n"; while ( flag < 30 ) { flag++; cout << flag << "\n"; }The C++ program will run a lot faster of course!
Question - Why does the C++ program run a lot faster?
Hint: $ which expr /bin/expr $ ls -l /bin/expr -r-xr-xr-x 1 bin bin 20988 May 3 1996 /bin/exprflag++; = say 3 machine instructions (what 3?) 30 times this = 90 machine instructions.
flag=`expr $flag + 1` |
# powers echo "2^32" | bc # floating point operations # scale defines no. of decimal places echo "scale=2; 2347 / 19" | bc echo "scale=3; sqrt(50)" | bc # bc -l loads extra library: =========================== # e to the power echo "e(5)" | bc -l # sine echo "s(0)" | bc -l # calculate pi using the fact that: # arctan(1) = pi/4 echo "scale=100; 4*a(1)" | bc -l |
factor n