How to iterate For loop until certain condition meets? - for-loop

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.

Related

In Marie.js how would I go about doing an if statement and loop for the following pseudocode?

max = 0
value = 0
LOOP
INPUT value
IF (value == 0)
EXIT LOOP
ENDIF
IF (value > max)
max = value
ENDIF
ENDLOOP
PRINT max
STOP
I am using https://marie.js.org/ but i'm having a lot of trouble trying to figure out how to do an if statement. I've attempted to use skipcond. I'm also struggling a bit with the endless loop. Any help to get me started off would be really appreciated.
First, convert the pseudo code to the if-goto style of assembly language & machine code.
if a then
b
endif
translates into
if !a then goto endif1
b
endif1,
Second, translate your pseudo code variables into Marie assembly language/machine code variables.
For example, you have an integer max in the pseudo code, so in the data area put:
max, dec 0
Finally, translate each line of if-goto code into assembly.
Conditional tests if a < b goto are done by comparison using subtraction.  So, load a into the accumulator, subtract b, which sets the condition codes, and then do a SkipCond and goto to skip or not skip code you want to execute.
Marie.js has a number of simple examples.  Look at the multiplication example, to see data/variable declarations, conditional branches, loops, input, output.

Get loop value when condition is true in robotframework

I am new to Robot Framework, I want to get loop value when the condition is true. Below I mention sample code.
FOR ${INDEX} IN RANGE 1 10
Run Keyword If ${INDEX}==5
END
You are missing the call to a keyword, and I assume you don't want to continue the loop after it, so for example:
FOR ${INDEX} IN RANGE 1 10
Run Keyword If ${INDEX}==5 Run Keywords No Operation Exit For Loop
END
Log For loop variable is ${INDEX}

'for' loop with dynamic array size

I have an array that gets elements added to it when it calls the function findVar. The problem seems to be on the for loop that does not update the number of elements once started running.
When I do echo at the end of the for loop I get the correct number of elements and the last element but it seems not to be checking on the for conditions once started.
for var in "${tempV[#]}"
do
num_words=${#tempV[#]}
let i=i+1
if ! [ $i -gt $num_words ]
then
findVar $objBKP $var
fi
done
Your attempt is looping over every original element in the array and then ensuring that you haven't looped more times than that and calling your function.
That doesn't work because the original expansion of tempV happens once and so the added entries are never seen. But that also doesn't make sense since, by definition, if you are looping over the elements of the array you can't loop more times then there are elements in the array.
What you want to be doing (assuming a non-sparse, integer-indexed array that is only appended to) is looping numerically and checking that you haven't exceeded the array size as the loop condition.
Something like this (untested):
i=0
while [ "$i" -lt "${#tempV[#]}" ]; do
var=${tempV[i]}
findVar "$objBKP" "$var"
i=$((i + 1))
done
You're not using $i for anything other than an iteration counter. It's completely unnecessary in your posted example. Instead, just iterate over the contents of the variable expansion. For example:
for var in "${tempV[#]}"; do
findVar "$objBKP" "$var"
done

Break from imbricated for loop

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

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