Search code examples
while-looptcl

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


Solution

  • 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.