0

Convert the for-loop in the following script to a while-loop.

echo "Enter a number"
read n
count=0
for((i=1; i <= n; i++))
do
    echo "Enter a number"
    read a
    if((a > 0))
       then
       count=$((count+1))
    fi
done
echo "You entered $count positive number(s)"

my try:

while (i<=n)
echo "Enter a number"
read a
if ((a>0))
    then
    count=$((count+1))
i++

i dont know if i fully understand while and for loops. Thanks for your help ^_^

relativeLee
  • 39
  • 2
  • 7

1 Answers1

2

Your loop is almost right; however you forgot to initialize i=1 before entering the while loop, and the do/done keywords

aaa
  • 489
  • 5
  • 13
  • hmm, do goes right after the while (i<=n), and the done goes after the i++? – relativeLee Oct 28 '13 at 01:52
  • thanks aaa, trying to learn csh!!! maybe you can help me with my other question i posted a minute ago :P – relativeLee Oct 28 '13 at 02:03
  • http://stackoverflow.com/questions/19626144/assistance-with-cshell-arithmetic-function – relativeLee Oct 28 '13 at 02:03
  • @LeeHurlbert: You're not learning csh; your script is a bash script. Which is a good thing (see [here](http://www.perl.com/doc/FMTEYEWTK/versus/csh.whynot)), but if you actually want to learn csh programming that's not what you're doing. – Keith Thompson Oct 28 '13 at 02:37