In this article, we will cover the overview of TCL script to perform appropriate arithmetic operations using the switch statement and will implement with the help of an example. Let’s discuss it one by one.

**Pre-requisite –**

If you want to know more about TCL, then kindly go through this article, https://www.geeksforgeeks.org/basics-of-ns2-and-otcltcl-script/.

**Overview :**

We will understand the basic syntax of a switch statement in Tool Command Language, by a simple example. In this example, our objective is to perform an arithmetic operation based on the symbol we specify. For example, if we specify ‘+’ then addition must be performed and like-wise.

**Steps to perform appropriate arithmetic operations using the switch statement :**

Here, we will implement TCL script to perform appropriate arithmetic operations using the switch statement step by step as follows.

**Step-1: Input numbers :**

Firstly, we need to read two numbers a and b.

puts "Enter the first number " gets stdin a puts "Enter the second number " gets stdin b

**Step-2: Reading input :**

Secondly, we need to prompt to enter the desired character and have to read it.

puts "Enter \n '+' for addition\n '-' for subtraction\n '*' for multiplication\n '/' for division" gets stdin ch

**Step-3: Writing Switch Statement :**

Now, we need to write a switch statement, to map to the appropriate symbol.

switch $ch { "+" { set result [expr $a+$b] puts "Addition of $a and $b is $result" } "-" { set result [expr $a-$b] puts "Subtraction of $a and $b is $result" } "*" { set result [expr $a*$b] puts "Multiplication of $a and $b is $result" } "/" { set result [expr $a*$b] puts "Division of $a and $b is $result" } "%" { set result [expr $a%$b] puts "Modulo of $a and $b is $result" } default { puts "Enter the appropriate input" } }

**Note – **

The syntax for the switch statement should be exactly as shown above. If you neglect the spaces or type the opening curly brace in a new line, the result will be an error. And of course, we only use double quotes for characters in TCL.

**Step-4: Understanding the syntax :**

Now to understand the syntax better, the C programming version of the addition (+) case is shown below.

case '+': result=a+b; printf("Addition of %d and %d is %d",a,b,result); break;

As you can see, we don’t use the keyword **case** followed by a colon ‘**:**‘ in TCL. Instead, we directly specify the required action statement, followed by the body in the curly braces. The break statement is not necessary for the TCL script.

**Step-5: TCL script to perform appropriate arithmetic operations using the** **switch statement :**

Now, let us look at how the complete code using TCL would like as follows.

**Code –**

puts "Enter the first number " gets stdin a puts "Enter the second number " gets stdin b puts "Enter\n '+' for addition\n '-' for subtraction\n '*' for multiplication\n '/' for division" gets stdin ch switch $ch { "+" { set result [expr $a+$b] puts "Addition of $a and $b is $result" } "-" { set result [expr $a-$b] puts "Subtraction of $a and $b is $result" } "*" { set result [expr $a*$b] puts "Multiplication of $a and $b is $result" } "/" { set result [expr $a*$b] puts "Division of $a and $b is $result" } "%" { set result [expr $a%$b] puts "Modulo of $a and $b is $result" } default { puts "Enter the appropriate input" } }

**Output :**

The outputs for a set of inputs are as follows.

**For addition –**

**For Multiplication –**

**For modulus –**

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the **CS Theory Course** at a student-friendly price and become industry ready.