Break from imbricated for loop - bash

I want to know if there is a way to leave imbricated for loop:
check_mac_address() {
local mac="$1"
for wunit in `get_wunit`; do
for iuc in `get_iuc`; do
for assoc_mac in `get_iuc $wunit $iuc`;do
if [ "$assoc_mac" = "$mac"]; then
local int_type="WF"
break #---> break from all loop
else
int_type="ETH"
break #---> break from all loop
fi
done
done
done
}
any help is appreciated

break takes a parameter which specifies how many levels of surrounding loops to break; in your case I believe it would be 3:
http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins

From http://tldp.org/LDP/abs/html/loopcontrol.html
A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.
So in your case you to break from all three loops you can do
break 3

Related

How to iterate For loop until certain condition meets?

I need to iterate for loop till certain condition meets in Robot Framework.
${counter}= Set Variable 1
:FOR ${item} IN RANGE ${counter}
\ Check condition
\ ${counter} = ${counter} + 1
Is it possible to increase the ${counter} variable value here?
Yes.
${counter}= Set Variable 1
FOR ${item} IN RANGE 1 20
${counter}= Evaluate ${counter} + 1
Log To Console ${counter}
Exit For Loop If ${counter} == 10
END
And FOR loops can be exited using Exit For Loop or Exit For Loop If keywords. Keywords documentation.
EDIT after comments.
You're asking about a while loop. Robot doesn't have a while loop. There's simply no support for it, and robot probably won't support it until at least 2021. The only looping construct is a for loop.
You seem to have an aversion to setting a limit of 20, but there must be a practical limit to the number of iterations, whether it's 1,000, 10,000, or 1 million or more. Just use a FOR loop that has a huge upper limit, and for all intents and purposes you've created a while loop.
FOR ${item} IN RANGE 1000000
Exit FOR loop if <some condition>
${counter}= evaluate $counter + 1
END
While it doesn't look quite as pretty as While <some condition>, the end result will be the same, assuming your condition becomes true at some point before one million iterations.

confused about Pascal syntax

I ran across this piece of pascal code. I am curious as to how this does not repeat endlessly.
repeat
if xs>(torgx+xlim)
then begin
x:=xlim;
BREAK;
end;
if xs<(torgx-xlim)
then begin
x:=0-xlim;
BREAK;
end;
x:=xs-torgx;
BREAK;
until 0<>0;
I am confused as to how zero would ever be greater than or less than zero.
A loop that continues until 0 <> 0 is supposed to be endless.
But inside the loop there are some conditions that will break the loop, hence the use of the keyword break.
In fact, the repeat..until loop will only run once. The comparison is made that if a value is larger than a condition or less than another it will break out of the loop. If none of those conditions are met, it will break anyway.

Python: For loops breaking issue

I'm using a for loop, and inside the for loop are two if-statements that are both being checked for each iteration of the for loop. My issue is that once one of the if-statements = True, then I want the True if-statement to stop being checked, and only check the second if-statement. Or vice versa.
print("Guess the numbers it is! Your goal is to guess a two digit number from 00-99. The two digits may be the same.")
numGuessesN = int(input("How many guesses would you like? (2, 3, or 4) "))
for numGuessesN in range (firstGuess, numGuessesN + 1) :
print("Guess number", numGuessesN)
userGuessN = int(input("What is your guess? "))
if (userGuessN == randomNum1) :
print("You've guessed the first number!")
break
else :
print("The first number is not", userGuessN)
if (userGuessN == randomNum2) :
print("You've guessed the second number!")
break
else :
print("The second number is not", userGuessN)
I know the breaks in the if-statements will completely exit the for loop, and that's not what I want. But somehow I need the for loop to "break" for only each individual statement if it turns out the statement is true, and keep checking the remaining statement until it's true, or the loop runs out of iterations.
Thanks guys! I'm new to all of this, so I'm sorry if I'm not really clear haha.
either you use nested if-statements, or (in case you have a lot, so you would have a lot of repeated code) you set a variable
first_wrong=False
if bla:
pass
else:
first_wrong=True
if bla2 and not first_wrong:
pass
if bla3 and not first_wrong:
pass

Switch statement into while loop or while loop into case blocks?

I'm refactoring some code I wrote and I have to decide if I wanna put a switch statement into a while loop or repeat the while loop into each case blocks. The two different ways are something like the following pseudocode:
Option 1)
while (cnt > 0) {
switch (value) {
case A:
doSomething();
break;
case B:
doSomethingElse();
break;
...
default:
default();
break;
}
cnt--;
}
Option 2)
switch (value) {
case A:
while( cnt > 0){
doSomething();
cnt--;
}
case B:
while( cnt > 0){
doSomethingElse();
cnt--;
}
...
default:
while( cnt > 0){
default();
cnt--;
}
}
I think the first option follows better the DRY principle because I don't repeat every time the while and the cnt--. Although I like a bit better the second option because once it picks a case in the switch statement, it starts to loop in the while and it doesn't have to evaluate the switch condition anymore.
So, which of these two solutions would you pick and why?
NB: We can assume that the switch-condition (i.e. the variable "value" in the code below) doesn't change for the entire while loop, in other words: the operations doSomething(), doSomethingElse() etc. do not affect the switch statement.
NB2: In the real code I'm working on, the while loop can be very huge. "Cnt", for some test cases, can be of the order of 10^9
In most cases, a Loop-switch sequence is an antipattern you should avoid for the sake of clarity, and since you mention cnt can become quite huge also for the sake of performance.
If cnt is used only to control how many times doSomething(), etc., are called, you should consider passing it into the method and looping inside the method. Normally I would say don't worry about performance, but if you're actually talking about 10^9 iterations, you should avoid repeating the switch.
Option 2 is more straightforward as you are putting the emphasis on the switch-statement and having to work through a case in every iteration might consume more time.

Confused by this unless statement in rubykoans

Lines 4 & 5 are causing me grief:
1 def test_break_statement
2 i = 1
3 result = 1
4 while true
5 break unless i <= 10
6 result = result * i
7 i += 1
8 end
9 assert_equal 3628800, result
10 end
I'm not sure what needs to remain true in the while true statement, however I believe it is the code that follows it. This leads to further confusion because I am reading the line:
break unless i <= 10 as break if i is not smaller or equal to 10. What procedure is this code going through ie how does the while and break statements interplay. I think I am nearly there but can't put the process in my head. Thanks.
The code will break out of the endless while loop when i is greater than 10.
But I'm not sure why the condition isn't checked in the while statement.
Edit: Had I read the method name I would have understood why the condition isn't checked directly with the while statement. The method's purpose is to test the break statement.
while statements test whatever comes after the word while. If the expression that follows them is true they execute the code within the loop. If the expression is false, they do not.
Thus, as other posters have pointed out, while true will always execute the code within the loop. Luckily for your code there is a break statement within the loop. If there wasn't, the loop would run forever and you'd have to kill the process running your program.
In your code sample the break keyword is followed by unless which means that it will break the loop unless the expression following it is true. Your code will break out of the loop when i is greater than 10.
while true is an infinite loop. break, when executed, will exit it immediately, to continue with the first line after it (assert_equal...).
In this specific case (nothing intervening between while and break unless), it is equivalent to this:
while i <= 10
result = result * i
i += 1
end
while true it is endless loop.
break unless i <= 10 is same as break if i > 10 it will break that loop if i is smaller or equal to 10

Resources