Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:06:38+00:00 2026-06-07T21:06:38+00:00

I have a for loop in a while loop. I have a condition to

  • 0

I have a for loop in a while loop. I have a condition to break the while in the for loop.

Here is the code :

while {[gets $thefile line] >= 0} {
   for {set i 1} {$i<$count_table} {incr i} {
   if { [regexp "pattern_$i" $line] } {
      for {set break_lines 1} {$break_lines<$nb_lines} {incr break_lines} {
         if {[gets $thefile line_$break_lines] < 0} break
      }
   }
   #some other process to do
}

I want to skip $nb_lines in the file parsed to do other thing further. Here the break, breaks the for loop, so it doesn’t work.

Can the for loop make the while loop broken ?
But the break is just for 1 (or more) lines, i want to continue to parse the file after the break to process line further

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:06:39+00:00Added an answer on June 7, 2026 at 9:06 pm

    The break command (and continue too) doesn’t do multi-level loop exit. IMO, the simplest workaround is to just refactor the code so you can return to exit the outer loop. However, if you can’t do that, then you can use something like this instead (for 8.5 and later):

    proc magictrap {code body} {
        if {$code <= 4} {error "bad magic code"}; # Lower values reserved for Tcl
        if {[catch {uplevel 1 $body} msg opt] == $code} return
        return -options $opt $msg
    }
    proc magicthrow code {return -code $code "doesn't matter what this is"}
    
    while {[gets $thefile line] >= 0} {
       magictrap 5 {
          for {set i 1} {$i<$count_table} {incr i} {
             if { [regexp "pattern_$i" $line] } {
                for {set break_lines 1} {$break_lines<$nb_lines} {incr break_lines} {
                   if {[gets $thefile line_$break_lines] < 0} {magicthrow 5}
                }
             }
          }
       }
       #some other process to do
    }
    

    The 5 isn’t very special (it’s just a custom result code; Tcl reserves 0–4, but leaves the others alone) but you need to pick a value for yourself so that it doesn’t overlap with any other uses in your program. (It is mostly possible to redo the code so it works with 8.4 and before too, but it’s quite a bit more complex to rethrow an exception there.)

    Be aware that custom exception codes are a “deep magic” part of Tcl. Please use ordinary refactoring instead if you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a simple loop while read line do printf ${line#*//}\n done <
The while loop I have while reading in from a file doesn't break. I'm
(Scanner Input=new Scanner(System.in)) I have a while loop in my program with Input.hasNext() condition.
I have the following code in a loop: while true: # Define shell_command p1
I often use this code pattern: while(true) { //do something if(<some condition>) { break;
i have a code which reads a file line by line using a while
I have a while loop that goes while a BuffedReader still has data, what
I have a while loop that loops through 3 results and echo's these out
hi i have a while loop: var i = 0; while(i < 20) {
So with pygame you have a while loop that loops continuously, then your event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.