LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2009, 07:39 AM   #1
manya
Member
 
Registered: Apr 2004
Posts: 194

Rep: Reputation: 15
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
 
Old 10-14-2009, 07:46 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
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!
 
Old 10-14-2009, 08:19 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 10-14-2009, 08:24 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 10-14-2009, 09:12 AM   #5
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
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
 
Old 10-14-2009, 09:48 AM   #6
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
Or here is the simple script

X=0
while :
do
echo "X=$X"
X=`expr ${X} + 1`
sleep 1
done
 
Old 10-14-2009, 10:04 AM   #7
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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.

Last edited by Guttorm; 10-14-2009 at 10:07 AM.
 
Old 10-14-2009, 10:08 AM   #8
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
hmmm..so is it mandatory to break the loop explicitly when you add ":" in while loop?
 
Old 10-14-2009, 10:20 AM   #9
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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.
 
Old 10-14-2009, 01:42 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by sundialsvcs View Post
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.....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Explanation of the "loop" option when mounting CD image files uncle-c Linux - Newbie 5 12-15-2007 11:16 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration