Hello everyone,

I have this little for loop that I want to convert to a while loop. It accepts unknown number from the command line parameters and then add them all up and display the sum, here is the for loop:

sum=0

for x in $*
do 
sum=`expr $sum + $argument`
done

echo $sum

it runs perfectly, any suggestions how to convert it to a while loop?

Thank you

Regards

Recommended Answers

Why must you convert it? If it is for "homework"/"classes", then good luck.

Jump to Post

All 2 Replies

Why must you convert it? If it is for "homework"/"classes", then good luck.

Here are a couple of hints to get you going:

  • ${#} reports the number of arguments currently available
  • shift decrements the number of available arguments

Combining those two should give you ample ammunition for a while loop.

The construct, by the way, is

while [[ ${CONDITION} ]]; do
   # stuff here
done
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.