Kumar
Kumar

Reputation: 717

TCL:: How to print numbers from 10 to 1 using while loop in TCL (How to decrement the variiable value)

TCL Script:

set a 10
while {$a < 1} {
    puts $a
    incr a
}

Expected output:

10
9
8
7
6
5
4
3
2
1

I am trying to print numbers from 10 to 1. But its not working (It prints nothing).

Is there a way to "decrement" the variable value (decr a)?

Thanks,

Kumar

Upvotes: 1

Views: 19807

Answers (4)

G&#225;bor
G&#225;bor

Reputation: 451

set a 10
while {$a > 0} {
    puts  $a
    incr a -1
}

Upvotes: 0

Johannes Schacht
Johannes Schacht

Reputation: 1344

I think your loop body is never executed because the condition yields false the very first time. You probably wanted to write ">" instead of "<".

Upvotes: 0

JigarGandhi
JigarGandhi

Reputation: 243

same can be done by for loop also

for {set i 10} {$i > 1} {incr i -1} {
puts $i

}

Upvotes: 1

Dinesh
Dinesh

Reputation: 16428

Change the condition to $a > 1 and to decrement the value, you have to use incr a -1. Here we gave the step as -1.

Upvotes: 4

Related Questions