LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   while loop, need some explanation (https://www.linuxquestions.org/questions/programming-9/while-loop-need-some-explanation-761817/)

manya 10-14-2009 07:39 AM

while loop, need some explanation
 
Hey Guys,

I am bit confused about while loop and not sure what it does. I need to know what while : does and how can you use that effectively.

Thnks,
Manya

rizhun 10-14-2009 07:46 AM

Hi manya,

You didn't specify what language you are talking about, so I'll have to be general...

A while loop is a flow control statement that allows code to be executed repeatedly based on a given condition.

I'll give you an example in Perl...

Say you wanted to cat a file and pipe it into a Perl program you've made, you'd process each line of the file by using a while loop:

Code:

#!/bin/perl

while ( <STDIN> ) {
  do something with the text line...
}

This code says "while data is coming from standard-input (STDIN) then do something with the text line"

The code will repeat this 'something' on every line from the file.

Hope this helps!

pixellany 10-14-2009 08:19 AM

http://tldp.org ..Get a copy of the Bash Guide for Beginners

Did you mean to ask literally what this does?:
"while :"
If so, whre did you see that?

sundialsvcs 10-14-2009 08:24 AM

How about classic BASIC?

Code:

10 IF condition is false THEN GOTO 40
20 do something
30 GOTO 10
40 ... the while-loop is now finished ...

Key points:
  • The condition is tested at the start of the loop. (It is not "continually tested during the loop.")
  • The body of the loop executes only so long as the condiion is true.
  • If the condition is initially false, the body of the loop is not executed at all.

manya 10-14-2009 09:12 AM

Oops !! sorry about providing half info. I was talking about bash scripting and here are some codes but not sure what they do or rather I need to know for what and how that while loop can be used

while : ; do
echo -n "Enter path to compressed image on cdrom [$cdpath or 'exit' to exit]: "
read P
if [ "$P" = "exit" ] ; then
cleanup 1
fi

IMGPATH=${P:=$cdpath}
if [ ! -z "$IMGPATH" ] ; then
break;
fi
done

manya 10-14-2009 09:48 AM

Or here is the simple script

X=0
while :
do
echo "X=$X"
X=`expr ${X} + 1`
sleep 1
done

Guttorm 10-14-2009 10:04 AM

Hi

while :
do
...
done

This means do what's between the "do" and the "done" forever. Well actually until someone kills the program, or there is a "break" or an "exit" inside the loop.

manya 10-14-2009 10:08 AM

hmmm..so is it mandatory to break the loop explicitly when you add ":" in while loop?

Guttorm 10-14-2009 10:20 AM

Yes : means do nothing, evaluate to true and set return code to 0.

"while :" is the same as "while true" but just a bit shorter and more difficult to read IMO.

pixellany 10-14-2009 01:42 PM

Quote:

Originally Posted by sundialsvcs (Post 3718857)
How about classic BASIC?

Code:

10 IF condition is false THEN GOTO 40
20 do something
30 GOTO 10
40 ... the while-loop is now finished ...


Yuck!!!---I thought GOTO went out with Fortran.....;)


All times are GMT -5. The time now is 08:27 AM.