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 |
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 The code will repeat this 'something' on every line from the file. Hope this helps! |
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? |
How about classic BASIC?
Code:
10 IF condition is false THEN GOTO 40
|
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 |
Or here is the simple script
X=0 while : do echo "X=$X" X=`expr ${X} + 1` sleep 1 done |
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. |
hmmm..so is it mandatory to break the loop explicitly when you add ":" in while loop?
|
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. |
Quote:
|
All times are GMT -5. The time now is 08:27 AM. |