Grayrigel
Grayrigel

Reputation: 3594

Loop condition with operators

I am not experienced in Tcl language. I am trying to run a loop and trying to write output to an output file. My loop condition is not generating any output. I have double checked my inputs they seem to be correct. Is my syntax for end condition is correct ? Instead of && if I use just $j >83 the it's works fine. ( I have attached a snipped on my code below, I have issue in the first line of my code).

for {set j 1} { $j > 83 && $j < 104 } {incr j}
 {

    set sel [atomselect top "resid $j and name CA"]
    mol reanalyze top
    set structs [$sel get structure]
    if {[lindex $structs ] == "H"} 
    {
        incr count
    }
    puts -nonewline $output $j
    puts  -nonewline $output "   "
    puts $output $structs
 }

Upvotes: 0

Views: 68

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137687

Assuming that the obvious syntax errors are corrected…

The for command stops when the condition expression clause evaluates to false. On the first time through, you first run the setup clause (set j 1) and then checks the condition ($j > 83 && $j < 104) which indeed evaluates to false because 1 is not between 83 and 104, so the for stops before running the body of the loop.

You might be happier with:

for {set j 84} {$j < 104} {incr j} {
    # The rest of your loop body here...
}

In more complex cases, you defining a basic loop and then put a separate skip rule within the body:

for {set j 1} {$j <= 200} {incr j} {
    if {!($j > 83 && $j < 104)} {
        # Start the next loop round immediately
        continue
    }

    # The rest of your loop body here...
}

But in this case… just loop over the right range to start out with.

Upvotes: 1

Related Questions