Gary
Gary

Reputation: 2859

Is variable assignment within if conditions possible?

You know how in C and JavaScript variable assignments can take place within conditions, such as

if ( boolean || (val = func(args)) == case1 ) {
  /* Perform A */
} else if ( val == case2) {
  /* Perform B */
} ...

such that don't need to write another elseif to perform code A.

Can this be done in Tcl; and, if so, how?

It's not very important but I'd like to know just the same.

Thank you.

Upvotes: 0

Views: 392

Answers (1)

Shawn
Shawn

Reputation: 52439

You can use set and other commands in an expr-ression, and set returns the newly assigned value, so...

set boolean true
proc func {} { return case2 }

if {$boolean && [set val [func]] eq "case1"} {
    puts "case 1 found"
} elseif {$val eq "case2"} {
    puts "case 2 found"
}

Upvotes: 2

Related Questions