Chapter 3  Conditions and Loops
Captain the ship is out of control!

Controlling the flow of the program is very important.  Without the ability to  control the flow, programming would be extremely difficult.  This ability to have control of the logic of a program is common ground in programming, and this leads to some common types of control.  Some of the specific flow control will differ a bit in form from one programming language to the next; however, the conceptual ideas are common.  The conditionals, loops and control options for flow control that we will examine and put to use are as follows:

if / elseif / else
switch
for
foreach
while
continue / break

Let's take a look at each of them one at a time.

if {expr1} {
       expr1TrueBody1
}

if {expr1} {
       expr1TrueBody1
} else {
       expr1FalseBody
}

if {expr1} {
       expr1TrueBody1
} elseif {expr2} {
       expr2TrueBody
} else {
       expr2FalseBody
}
 

switch [options] string {
 pattern { pattern body }
 pattern { pattern body }
 pattern { pattern body }
 default { default for all non matched patterns }
 }
 options:
  -exact  Selects exact comparison
  -glob  Selects pattern matching (Also the default option)
  -regexp  Selects regulate expression matching

for {start} {test} {next} {body}
 {start}  Command executed once at the beginning
 {test}  Boolean evaluation for loop flow control (loop runs while true)
 {next}  Command executed at the end of each iteration
 {body}  Commands to be executed with each iteration

foreach variableName list {body}
 variableName  The variable that will represent each given value from the
    provided list that can be used for substitution in the body
    section.

continue / break

Assignment 3.7
Below is an example of each form.  Try to determine the output that each program will produce.  Upon determination, type in the example and follow the hints offered in the program notation.  Finally, evaluate the difference between what your predicted output and what the actual program output.

######################################
# Written by Nelson
# Example ch3p7a.tcl
# This program offers an example of
# the if / elseif / else composition.
#
# Unremark the different values for
# x (only one at a time) so that you
# can examine the output for each value.
######################################
if [winfo exists .msg1] {destroy .msg1}
set x 3
#set x 5
#set x 7
if {$x == 5} {
      set cow "$x was equal to 5"
   } elseif {$x < 5} {
          set cow "$x is less than 5"
     } else {
         set cow "$x is greater than 5"
        }
message .msg1 -width 15c -bg yellow  -text "$cow"
pack .msg1


######################################
# Written by Nelson
# Example ch3p7b.tcl
# This program offers an example of
# the switch composition.
#
# Unremark the different values for
# x (only one at a time) so that you
# can examine the output for each value.
######################################
if [winfo exists .msg1] {destroy .msg1}
#set x 1
#set x 3
#set x 5
set x 7
switch $x {
  3 { set cow $x}
  5 { set cow $x}
  7 { set cow $x}
  default {set cow "$x was unlisted"}
}
message .msg1 -width 15c -bg yellow  -text "$cow"
pack .msg1


######################################
# Written by Nelson
# Example ch3p7c.tcl
# This program offers an example of
# the for composition.  I managed to
# sneak in a bit of substitution.
#
# Unremark the different values for
# x (only one at a time) so that you
# can examine the output for each value.
######################################
set x 1
#set x 3
#set x 5
#set x 7
# Let's do a little substitution.
# This first for loop will try to destroy the max number of
# msg widgets the other for loop could possible create based
# on the numbers I have coded in.

for {set i 1} {$i<=7} {incr i} {
 if [winfo exists .msg$i] {destroy .msg$i}
 }

for {set counter 1} {$counter<=$x} {incr counter} {
 message .msg$counter -width 15c -bg yellow  -text "Message $counter"
 pack .msg$counter
 }


######################################
# Written by Nelson
# Example ch3p7d.tcl
# This program offers an example of
# the foreach composition.  With a bit of substitution.
#
######################################
# Set var x to contain some information.

set x "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"

# Again substitution will be used.
#
# This first for loop will try to destroy the max number of
# msg widgets the other for loop could possible create based
# on the definition of the variable x I have coded in.

foreach item $x {
     if [winfo exists .msg$item] {destroy .msg$item}
 }

foreach thing $x {
     message .msg$thing -width 15c -bg yellow  -text "Message $thing"
     pack .msg$thing
 }


######################################
# Written by Nelson
# Example ch3p7e.tcl
# This program offers an example of
# the while composition.
#
# Again try the variables one at a time.
######################################
if [winfo exists .msg1] {destroy .mgs1}
set x 1
#set x 3
#set x 5
#set x 7
# set output to be empty.
set output ""
while {$x<10} {
     set output "$output $x"
     set x [expr $x+1]
 }
message .msg1 -width 30c -bg red -text $output
pack .msg1


######################################
# Written by Nelson
# Example ch3p7f.tcl
# This program offers an example of
# the use of break.
#
# Check out the substitution is being used in this code.
#
# Try different values for
# x so that you can examine the output
# for each value you try.
#
# You can as well change the value of breakNum.
######################################
set x 7

# Set a number condition for the break command
set breakNum 3

# Let's do a little substitution.
# This first for loop will try to destroy the max number of
# msg widgets the other for loop could possible create based
# on the numbers I have coded in.

for {set i 1} {$i<=7} {incr i} {
     if [winfo exists .msg$i] {destroy .msg$i}
 }
# Destroy the break notification message
if [winfo exists .msgBreak] {destroy .msgBreak}

# Do the work we want.
for {set counter 1} {$counter<=$x} {incr counter} {
     message .msg$counter -width 15c -bg yellow  -text "Message $counter"
     pack .msg$counter
     if {$counter == $breakNum} {
          # When the counter is equal to the condition then break!
          break
          message .msgBreak -width 15c -text "Condition met, calling break!"
          pack .msgBreak
         }
 }


######################################
# Written by Nelson
# Example ch3p7g.tcl
# This program offers an example of
# the use of continue.
#
# Check out the substitution is being used in this code.
#
# Try different values for
# x so that you can examine the output
# for each value you try.
#
# You can as well change the value of continueNum.
######################################
set x 7

# Set a number condition for the continue command
set continueNum 3

# Let's do a little substitution.
# This first for loop will try to destroy the max number of
# msg widgets the other for loop could possible create based
# on the numbers I have coded in.

for {set i 1} {$i<=7} {incr i} {
     if [winfo exists .msg$i] {destroy .msg$i}
 }

# Destroy the break notification message
if [winfo exists .msgContinue] {destroy .msgContinue}

# Do the work we want.
for {set counter 1} {$counter<=$x} {incr counter} {
     if {$counter == 3} {
          # When the counter is equal to the condition then continue!
          continue
          message .msgContinue -width 15c -text "Condition met continue!"
          pack .msgContinue
         }
     message .msg$counter -width 15c -bg yellow  -text "Message $counter"
     pack .msg$counter
 }


Next Section

Table of Contents