Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
syntax:ccmd:c_for [2012/11/30 18:46]
ormaaj More revision. Also Bash bug was fixed.
syntax:ccmd:c_for [2013/07/15 03:56] (current)
ormaaj [Bugs]
Line 39: Line 39:
  
 The equivalent construct using a [[syntax/​ccmd/​while_loop | while loop]] and the [[syntax:​ccmd:​arithmetic_eval | arithmetic expression compound command]] would be structured as: The equivalent construct using a [[syntax/​ccmd/​while_loop | while loop]] and the [[syntax:​ccmd:​arithmetic_eval | arithmetic expression compound command]] would be structured as:
- 
 <​code>​ <​code>​
 (( <​EXPR1>​ )) (( <​EXPR1>​ ))
Line 47: Line 46:
 done done
 </​code>​ </​code>​
 +The equivalent ''​while''​ construct isn't exactly the same, because both, the ''​for''​ and the ''​while''​ loop behave differently in case you use the [[commands/​builtin/​continuebreak | continue]] command.
 +
  
 ==== Alternate syntax ==== ==== Alternate syntax ====
Line 95: Line 96:
  
 This example loops through the bit-values of a Byte, beginning from 128, ending at 1. If that bit is set in the ''​testbyte'',​ it prints "''​1''",​ else "''​0''"​ => it prints the binary representation of the ''​testbyte''​ value (8 bits). This example loops through the bit-values of a Byte, beginning from 128, ending at 1. If that bit is set in the ''​testbyte'',​ it prints "''​1''",​ else "''​0''"​ => it prints the binary representation of the ''​testbyte''​ value (8 bits).
- 
 <​code>​ <​code>​
 +#​!/​usr/​bin/​env bash
 +# Example written for http://​wiki.bash-hackers.org/​syntax/​ccmd/​c_for#​bits_analyzer
 +# Based on TheBonsai'​s original.
 +
 function toBin { function toBin {
-    ​typeset n=2 x=x=x[\(n*=2\)\>$1+ typeset ​m=$1 n=2 x='x[(n*=2)>​m]' 
-    for (( x ; n /= 2;)); do + for ((x = x; n /= 2;)); do 
-        printf %d $(( $1 & n && 1)) + printf %d $(( & n && 1)) 
-    done+ done
 } }
  
-testbyte=123 +function main { 
-if (( $(ksh -c '​printf %..2d $1' _ "$testbyte") == result = $(toBin "$testbyte") )); then + [[ $1 == +([0-9]) ]] || return 
-    printf '%s is %s in base 2!\n' "$testbyte" "​$result"​ + typeset result 
-else + if (( $(ksh -c '​printf %..2d $1' _ "$1") == result = $(toBin "$1") )); then 
-    echo 'Oops, something went wrong with our calculation.'​ >&​2 + printf '%s is %s in base 2!\n' "$1" "​$result"​ 
-    exit 1 + else 
-fi+ echo 'Oops, something went wrong with our calculation.'​ >&​2 
 + exit 1 
 + fi 
 +
 + 
 +main "​${1:​-123}"​ 
 + 
 +# vim: set fenc=utf-8 ff=unix ft=sh :
 </​code>​ </​code>​
 +
 <div hide> ​ <div hide> ​
 <​code>​ <​code>​
Line 125: Line 137:
 </​code>​ </​code>​
 </​div>​ </​div>​
 +
 Why that one begins at 128 (highest value, on the left) and not 1 (lowest value, on the right)? It's easier to print from left to right... Why that one begins at 128 (highest value, on the left) and not 1 (lowest value, on the right)? It's easier to print from left to right...
  
-//Challenge question//: how did we arrive at 128 for ''​n''​?+We arrive at 128 for ''​n'' ​through the recursive arithmetic expression stored in ''​x'',​ which calculates the next-greatest power of 2 after ''​m''​. To show that it works, we use ksh93 to double-check the answer, because it has a built-in feature for ''​printf''​ to print a representation of any number in an arbitrary base (up to 64). Very few languages have that ability built-in, even things like Python.
  
 ==== Up, down, up, down... ==== ==== Up, down, up, down... ====
Line 167: Line 180:
 ===== Bugs ===== ===== Bugs =====
  
-  * //Fixed in git for >4.2//. <​del>​There appears to be a bug as of Bash 4.2p10 in which command lists can't be distinguished from the for loop's arithmetic argument delimiter (both semicolons),​ so command substitutions within the C-style for loop expression can't contain more than one command.</​del>​+  * //Fixed in 4.3//. <​del>​There appears to be a bug as of Bash 4.2p10 in which command lists can't be distinguished from the for loop's arithmetic argument delimiter (both semicolons),​ so command substitutions within the C-style for loop expression can't contain more than one command.</​del>​
  
 ===== See also ===== ===== See also =====
  • syntax/ccmd/c_for.1354301173.txt
  • Last modified: 2012/11/30 18:46
  • by ormaaj