SERVFORU

Tcl and OTcl Tutorial For NS2 -Loops and Conditional Statements


For loops

For loops are very useful in ns and can be used in conjunction with arrays to easily create larger network topologies. To generate 100 nodes, the following code can be used:
for {set i 0}{$i < 100}{incr i} {
    set n($i) [$ns node]
}

While loops

These are very similar to for loops. The syntax is
set i 0
while {$i < 10} {
    set n($i) [new Node]
    incr i
}

If statements

If statements are very simple
if {$i < 10} {
    puts "i is less than 10"
}
if {$var2 == "Tcl Variable 2"} {
    puts "var2 = Tcl Variable 2"
}
Share this article :
 

+ comments + 2 comments

Anonymous
February 7, 2013 at 9:12 AM

Appreciate this post. Will try it out.
my web page :: freebie trading scam

May 22, 2013 at 7:17 AM

In the first example of the for loop, there needs two be spaces. It doesn't compile as
for {set i 0}{$i < 100}{incr i} {
set n($i) [$ns node]
}

but it will compile as
for {set i 0} {$i < 100} {incr i} {
set n($i) [$ns node]
}

this includes the added two spaces on line 1 between the braces of the for conditions

Post a Comment

 
Support : Ebin EPhrem | Ebin Ephrem | #Gabbarism
Copyright © 2011. Services | Embedded Support | Reviews | Virtual Technologys - All Rights Reserved
Template Created by ebinephrem.com Published by Ebin Ephrem
Proudly powered by Blogger