Quartus® Prime Pro Edition User Guide: Scripting

ID 683432
Date 7/08/2024
Public
Document Table of Contents

2.9.7. Control Structures

Tcl supports common control structures, including if-then-else conditions and for, foreach, and while loops. The position of the curly braces as shown in the following examples ensures the control structure commands are executed efficiently and correctly. The following example prints whether the value of variable a positive, negative, or zero.

 If-Then-Else Structure

if { $a > 0 } { puts "The value is positive"
} elseif { $a < 0 } {
	puts "The value is negative"
} else {
	puts "The value is zero"
}

The following example uses a for loop to print each element in a list.

For Loop

set a { 1 2 3 }
for { set i 0 } { $i < [llength $a] } { incr i } {
	puts "The list element at index $i is [lindex $a $i]"
}

The following example uses a foreach loop to print each element in a list.

 foreach Loop

set a { 1 2 3 }
foreach element $a {
	puts "The list element is $element"
}

The following example uses a while loop to print each element in a list.

while Loop

set a { 1 2 3 }
set i 0
while { $i < [llength $a] } { puts "The list element at index $i is [lindex $a $i]"
	incr i
}

You do not have to use the expr command in boolean expressions in control structure commands because they invoke the expr command automatically.