Flow-control structures: Difference between revisions

m
m (→‎{{header|Perl}}: lang tag)
m (→‎{{header|Tcl}}: lang tag)
Line 508:
=== exception ===
Tcl's catch command can be used to provide a basic exception-handling mechanism:
<lang tcl> if {[catch { ''... code that might give error ...'' } result]} {
puts "Error was $result"
} else {
''... process $result ...''
}</lang>
 
=== custom control structures ===
A novel aspect of Tcl is that it's relatively easy to create new control structures (more detail at http://wiki.tcl.tk/685).
Eg. defining a command to perform some operation for each line of an input file:
<lang tcl> proc forfilelines {linevar filename code} {
upvar $linevar line ; # connect local variable line to caller's variable
set filechan [open $filename]
Line 524:
}
close $filechan
}</lang>
Now use it to print the length of each line of file "mydata.txt":
<lang tcl> forfilelines myline mydata.txt {
puts [string length $myline]
}</lang>
 
=={{header|Visual Basic .NET}}==