:TITLE: Looping 101 - While loop
;#
;# RCSID: $Header: /home/cvs/TclTutor3/lesson/Tcl_English/Tcl_English9.lsn,v 1.1 2008/08/02 12:37:17 clif Exp $
;# Copyright (c) 1995 Clif Flynt
;# 9300 Fleming Rd.
;# Dexter, MI  48130
;# clif@cflynt.com
;# See file "NOTICE" for licensing terms.
;#
:LESSON_TEXT_START_LEVEL 0:
<B>Syntax: </B> <CODE>while</CODE> <I>test body</I>
<P>
While evaluates <CODE>test</CODE> as an expression. If <CODE>test</CODE> is
true, the code in <CODE>body</CODE> is executed.  After the code in <CODE>body</CODE>
has been executed, <CODE>test</CODE>is evaluated again.
<P>
A <CODE>continue</CODE> statement within <CODE>body</CODE> will stop the
execution of the code and the test will be re-evaluated.  A <CODE>break</CODE> within
<CODE>body</CODE> will break out of the while loop, and execution will continue with
the next line of code after <CODE>body</CODE>.
<P>
:TEXT_END:
<P>
:LESSON_TEXT_START_LEVEL 1:
TCL includes two commands for looping, the <CODE>while</CODE> and <CODE>for</CODE> 
commands.  Like the <CODE>if</CODE> statement, they evaluate their test the same
way that the <CODE>expr</CODE> does.  In this lesson we discuss the <CODE>while</CODE> command,
and in the next lesson, the <CODE>for</CODE> command.  In most circumstances where 
one of these commands can be used, the other can be used as well.
<P>
<B>Syntax: </B> <CODE>while</CODE> <I>test body</I>
<P>
The <CODE>while</CODE> command evaluates <CODE>test</CODE> as an expression. If <CODE>test</CODE> is
true, the code in <CODE>body</CODE> is executed.  After the code in <CODE>body</CODE>
has been executed, <CODE>test</CODE>is evaluated again.
<P>
A <CODE>continue</CODE> statement within <CODE>body</CODE> will stop the
execution of the code and the test will be re-evaluated.  A
<CODE>break</CODE> within <CODE>body</CODE> will break out of the while
loop, and execution will continue with the next line of code after
<CODE>body</CODE>
<P>
In TCL *everything* is a procedure, and everything goes through the same substitution
phase.  For this reason, the <CODE>test</CODE> must be placed within braces.  If
<CODE>test</CODE> is placed within quotes, the substitution phase will replace any
variables with their current value, and will pass that test to the <CODE>while</CODE>
procedure to evaluate, and since the test has only numbers, it will always 
evaluate the same.
<P>
Look at the two loops in the example.  If it weren't for the break command
in the second loop, it would loop forever.
:TEXT_END:
<P>
:LESSON_TEXT_START_LEVEL 2:
Another common feature of computer languages is a looping construct.  This
is a way for the program to do the same set of operations over and over
with different values and not make the programmer type all the lines of code.
<P>
TCL includes two commands for looping, the <CODE>while</CODE> and <CODE>for</CODE> 
commands.  Like the <CODE>if</CODE> statement, they evaluate their test the same
way that the <CODE>expr</CODE> does.  In this lesson we discuss the <CODE>while</CODE> command,
and in the next lesson, the <CODE>for</CODE> command.  In most circumstances where 
one of these commands can be used, the other can be used as well.
<P>
The syntax for the while command is:
<B>Syntax: </B> <CODE>while</CODE> <I>test body</I>
<P>
When the Tcl interpreter encounters a while command, it evaluates the <CODE>test</CODE>
immediately.  If the <CODE>test</CODE> evaluates to true, then the code within
<CODE>body</CODE> is executed.  After the code is <CODE>body</CODE> has been executed,
<CODE>test</CODE> is evaluated again.  If <CODE>test</CODE> is still true, then <CODE>body</CODE>
is evaluated yet again.  This process continues until <CODE>test</CODE> evaluates
to false, or a <CODE>break</CODE> command is encountered.
<P>
<CODE>Break</CODE> and <CODE>continue</CODE> are two commands that modify the behavior
of a loop.  When a <CODE>break</CODE> is encountered the interpreter exits the loop
and continues with the next line of code after the loop.  When a <CODE>continue</CODE> is
encountered, the interpreter goes back to the <CODE>test</CODE>, and does not execute
the rest of the code within <CODE>body</CODE>.
<P>
In TCL *everything* is a procedure, and everything goes through the same
substitution phase.  For this reason, the <CODE>test</CODE> must be
placed within braces.  If <CODE>test</CODE> is placed within quotes,
the substitution phase will replace any variables with their current
value, and will pass that test to the <CODE>while</CODE> procedure to
evaluate, and since the test has only numbers, it will always evaluate
the same. 
<P>
For example - <CODE> while {$x < 5} </CODE> is passed to the <CODE>while</CODE> command as "$x < 5",
but <CODE>while "$x < 5"</CODE> would have the current value of x substituted for $x,
and (if x == 1) <CODE>"1 < 5"</CODE>  would be passed to the <CODE>while</CODE> procedure.
Since 1 is always less than 5, this code would loop forever.
<P>
Look at the two loops in the example.  If it weren't for the break command
in the second loop, it would loop forever.
:TEXT_END:
<P>
:CODE_START:
set x 1;

# This is a normal way to write a Tcl
# while loop.

while {$x < 5} {
    puts "x is $x"; 
    set x [expr $x + 1]
    }

puts "exited first loop with X equal to $x\n"

# The next example shows the difference between ".." and {...}
# How many times does the following loop run?
# Why does it not print on each pass?

set x 0;
while "$x < 5" {
	set x [expr $x + 1]
	if {$x > 7} break;
 	if "$x > 3" continue;
	puts "x is $x"; 
	}

puts "exited second loop with X equal to $x"

:TEXT_END:


