Shell脚本之求1..100的和(三种方法)

x33g5p2x  于2021-10-21 转载在 Shell  
字(0.3k)|赞(0)|评价(0)|浏览(168)

1.for循环 

#!/bin/bash
sum=o
for ((i=0;i<=100;i++))
do 
sum=$[sum+i]
done
echo "100以内的求和:$sum"


 

2.while循环

#/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
  sum=$[sum +i]
  let i++
done
echo "{1..100}的求和结果为:$sum"


 

3.until循环

#!/bin/bash
i=0
sum=0
until [ $i -gt 100 ]
  do
   sum=$[sum+i]
  let  i++
done
echo "{1..100}求和结果:$sum"

相关文章