nested loop problem 
Author Message
 nested loop problem

I'm having troubles using nested loops in tcl.  If I try something like:

while {$i < 5} {
    while {$j < 5} {
        puts "$i $j"
        incr j
    }
    incr i

Quote:
}

I get as output:
1 1
1 2
1 3
1 4

In other words, the outer loop only executes once, but the inner loop
works correctly.  Is there a good reason for this?

Thanks,
Cavendish

--
****************************************************************************

*                                                                          *
*      You're in trouble when you find it's hard for you to smile          *
*           A simple song might make it better for a little while.         *
*                                            ---Sly and the Family Stone   *
****************************************************************************



Sun, 20 Jul 2003 03:24:34 GMT  
 nested loop problem
You need to reset the value of j after
each inner loop.
Quote:

> I'm having troubles using nested loops in tcl.  If I try something like:

> while {$i < 5} {
>     while {$j < 5} {
>         puts "$i $j"
>         incr j
>     }
>     incr i
> }

> I get as output:
> 1 1
> 1 2
> 1 3
> 1 4

> In other words, the outer loop only executes once, but the inner loop
> works correctly.  Is there a good reason for this?

> Thanks,
> Cavendish

> --
> ****************************************************************************

> *                                                                          *
> *      You're in trouble when you find it's hard for you to smile          *
> *           A simple song might make it better for a little while.         *
> *                                            ---Sly and the Family Stone   *
> ****************************************************************************



Sun, 20 Jul 2003 03:38:44 GMT  
 nested loop problem

Quote:

> while {$i < 5} {
>     while {$j < 5} {
>         puts "$i $j"
>         incr j
>     }
>     incr i
> }

> I get as output:
> 1 1
> 1 2
> 1 3
> 1 4

> In other words, the outer loop only executes once, but the inner loop
> works correctly.  Is there a good reason for this?

Would you accept "j equals 5 at any time after the first completion of the
inner loop" as a good reason?

Andre'

--



Sun, 20 Jul 2003 03:39:01 GMT  
 nested loop problem

Quote:

> I'm having troubles using nested loops in tcl.  If I try something like:
> while {$i < 5} {
>     while {$j < 5} {
>         puts "$i $j"
>         incr j
>     }
>     incr i
> }
> I get as output:
> 1 1
> 1 2
> 1 3
> 1 4
> In other words, the outer loop only executes once, but the inner loop works >correctly.  Is there a good reason  for this?

Yes ;-) What really happens is the outer loop is executed until $i==4,
but the inner loop is done only the first time round. Since you don't
reset j, the inner condition stays false after the first failure.
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
RC DT2, Siemens Electrocom, Buecklestr. 1-5, D-78467 Konstanz,Germany
-------------- http://purl.org/thecliff/tcl/wiki//Richard*Suchenwirth
Bug: Divided by zero! Fix: Multiply by zero.  (Babbage, JUST-IN-CASE)


Sun, 20 Jul 2003 04:10:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. nested while loops, inside loop not stopping correctly

2. problem with nested while-loops

3. Problem with file input within nested loops

4. That nested nested nested if statement...

5. how to nest while loops

6. A puzzle - nested for-loops

7. Smalltalk rocks, and a newbie question ( nested loops ).

8. Nested Awk loops?

9. Nested ACCEPT loops with ROUTINE

10. Smalltalk rocks, and a newbie question ( nested loops ).

11. Help with unique attributes and nested loops

12. FP vs assembly programming for super-scalar CPUs [Was: Nested Loops in Clean]

 

 
Powered by phpBB® Forum Software