


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Dr. Masroor Kumar gave this lab manual at Baddi University of Emerging Sciences and Technologies for Computer Networking course. It includes: TCL, Cygwin, Basic, Commands, User, Input, Logical, Operators, Commands, Loops
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Learning TCL Convention: TCL commands are shown in italic
Warm Up If you have practiced the last lab sufficiently you may skip the warm up
Open working console Start cygwin (for installation problem read and re-install the ns2 using the installation guide) Start xserver by typing startxwin.bat Minimize the bash console and work in the new window opened by startxwin.bat
Basic Commands Examples puts string puts HelloWorld puts “sentence or word or letter or variable or expression ” puts “Hello World” puts {sentence or word or letter or variable or expression} puts {Hello world }
set variable_name variable_value set num 10 set variable_name [expression] set sum [ expr 10+20] set variable_name $variable_value set num2 $num
New Lessons
Lesson – 01 Writing TCL commands in a file and executing
Command: source filename
TCL commands can be written in a file and the file can be executed using the interpreter through source command, the source command takes a file name as argument, if the file path is not already set using environment variable PATH, one has to provide full path name, if path is already defined one has to provide only the file name with extension.
Example: 1- Create a file temp.txt in your /home/DCIS directory 2- iin the file write puts “Hello World” and save the file 3- go to tcl interpreter prompt and write source temp.txt 4- result : command prompt should display “Hello World”
Exercise- Write tcl code that displays your bio data as student of BS CIS i.e. your name, batch, subject etc.
Exercise-01 ( use file ) Write a program that adds two numeric values by using a procedure, for that you have to define the following at least
1- A procedure (choose a suitable for that) 2- Create three variables, assign values one of them should have a value = 0 3- Display the values of the variables 4- Display a sentence “Calling Procedure 5- Use the procedure and store the value in third variable with value = 0 6- Display the value of the third variable
The output should look like
The variables are A = 10 , B = 20 Result = 0 Calling the procedure “sum” A + B = 30
Lesson – 02 User Input
Command: gets stdin
gets reads data from a given input stream , an input stream is any source of data , it can be a file or can be direct user input from command line, stdin is standard input stream i.e. read from console, so to read from console the command will be gets stdin, the gets command is used like expr comamnd
Exmple
puts “Enter A value” set num [gets stdin] puts $num
The above example will display a sentence “Enter a value” and will allow user to enter any data (can be a word of sentence), user input will be stored in variable num, as gets require parameter so in set command one has to write gets command in [ ] (square brackets).
Lesson – 03 if then else
In TCL the if then else command is used to perform conditional execution just like as in C/C++, all logical comparisons operators are same as in C/C++, the syntax is as follows
Logical Operators
$A > $B A Greater than B $A < $B A Less than B $A >= $B A Greater than or equal to B $A <= $B A Less than or equal to B $A == $B A Equal to B $A != $B A Not Equal to B
Simple if Example
if [command] body
OR
if [command] { body }
set num 10 if [$num] puts “The variable num is greater than 0 “
if [ $num> 20] { puts “The variable num is greater than 0” puts “The variable num is greater than 20” }
The { } (curly brackets) are necessary to be used as shown, i.e. start of if body right after logical comparison, though [ ] are used with if it is better to use { } with if so that we can use spaces as we discussed in last lab.
if else
if [ command ] {
body
} else { body }
if elseif
if [ command ] {
body
} elseif { body
} else { body }
Note: elseif (no space between else and if )
Example: 1 set num1 10 set num2 20 if [$num1>$num2] { puts “$num1 is greater than $num2” } else { puts “$num2 is greater than $num1” }
Example: 2 set num1 10 set num2 20 if [$num1>$num2] { puts “$num1 is greater than $num2” } elseif [$num2>$num1] { puts “$num2 is greater than $num1” } else { puts “The two numbers are equal”
Example
#Multiplication table...
set n 4 ;# We want the multiplication table of 4
set table ""
for { set i 1 } { $i <= 10 } { incr i } {
set table "$table $i x $n = [expr $i * $n]\n"
}
Exercices
1- Write a program that compares three integers and print the smallest of the three
2- Write a program that ask users to input three numbers and print how many of them are even, how many of them are
odd and print the numbers in ascending order
3- Write a program that takes a number N as input and prints the even number between 1 and N using for loop
4- Extend 1 to print even numbers between two given numbers A & B