将for循环转换为while循环

时间:2013-10-28 01:32:08

标签: csh

将以下脚本中的for循环转换为while循环。

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)"

我的尝试:

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

我不知道我是否完全理解while和for循环。谢谢你的帮助^ _ ^

1 个答案:

答案 0 :(得分:2)

你的循环几乎是正确的;但是你忘了在输入while循环之前初始化i = 1,以及do / done关键字

相关问题