How can one modify for loop in QTP? - for-loop

I was asked this question in an interview, here it goes
'You have written for loop from 1 to 100, but I want to run the for loop from 70 to 100, ignoring previous iteration from 1 to 69. You are not allowed to change anything in the script, how can you automate this in QTP?'
Can somebody please tell me how I can achieve this?

Please follow the below steps and you will be able to work with for loop :
Step 1. Put a break point where the loop starts.
Step 2. Run the Script and when it stops at break point, go to view-->debug-->Console and change the value of your iterator variable from 0(start value) to 70.
example : i = 70
Step 3. Run the script and you will achieve your target.

You could always check the value of your iteration variable as the first step in your 'For' loop and if the value is 1, change it to 70

Related

Lua for loop reduce i? Weird behavior [duplicate]

This question already has answers here:
Decrementing a loop counter as loop is executing
(3 answers)
Closed 7 years ago.
Can someone explain me this?
for i = 1, 5 do
print(i)
i = i - 1
print(i)
end
Output is:
1
0
2
1
3
2
and so forth
I exspected i to alter between 1 and 0. But obviously it keeps increasing as if I did not change it at all. What's going on?
I have to delete an i'th table element every now and then. So the next element to process would be i again. In C I would just write --i at the end of my loop content. Any official way in lua? :)
The loop index (i in your case) is a variable local to the body of the loop, so any modifications you do to it have no effect on the loop conditions or the next element being processed (for the for loop).
If you need to have better control over the index to delete elements and keep processing, you should use the while loop form. See For Statement section for details.
What about using a while(true) loop instead? Then you'll have to break manually, but that might work.
local i = 5
while(true) do
print(i)
i = i - 1
if (i == 0) then break; end
end
Attempting to set the loop control variable causes a failsafe behavior.
I can't find it in the Language Incompatibility section of recent versions of the manual but I recall it being listed somewhere as a change. It was more of a formalization of previous undefined behavior.
If you are really curious, see the listing of your program from luac. The loop control variable is given an internal name so it can't be changed. If the same name is used in an assignment, a local variable is synthesized as a stand-in. It prevents unintentionally causing the for to become infinite.
You need another kind of loop.

For loops being skipped without cause in VBA

My code, as written, works for all the ways I've tested it. I have two questions though. First, Why in the blue blazes do I HAVE to use Do While loops instead of For loops in my code? I've searched Everywhere I can to help me on this issue. I can't reinstall excel, but I've reset as many of the settings as I can, but, invariably, the compiler skips over every for loop I have that isn't a for each loop... Except the first for loop in my programming... It is the weirdest and most bizarre behavior I have ever seen. I have used step through (F8) 10000 times to try and figure out why it keeps skipping. But every single time i make a for loop, it doesn't even run the first line of it.
To be clear, every place I have a Do While ... Loop, it SHOULD be a For Next. But making this change breaks the code every time because every spot the do while is changed to for, the code is skipped entirely. Even if I reset the i value to 0. The issue happens even if I have a different iterator for each loop.
Running w8 on an Intel i5 with Office 2010.
For i=1 To i = 100
If (i >= startRow And i <= stopRow And Not rowDone(i) And Not i = colFocus) Then
colCurPayAmounts(i) = S_Debt.Cells(i, 5)
Else
colCurPayAmounts(i) = 0
End If i = i + 1
Next i
The problem is it should be For i = 1 To 100 and not For i = 1 To i = 100 #Rory

How can I make gdb stop at the breakpoint after loop has been executed n number of times

I want to stop the execution, when the loop has been executed n number of times. I don't want to use the conditional breakpoint using value of loop-counter because initial value of loop-counter is different at different time of run.
You need to use the ignore command. Set a breakpoint at the relevant place in your loop and then use ignore to indicate how many times that the breakpoint is to be ignored.
Syntax, as per help ignore is:
Set ignore-count of breakpoint number N to COUNT. Usage is `ignore N
COUNT'.

Multiple Wait Commands

I haven't been able to thoroughly concise my question, which means search engines can't give me a straight answer, and I've used this site in the past many times, so it's time I ask my own question..
So, I have a script (in bash) that runs through my list of pictures and determines if they need to be resized, which works well. I want to have a maximum of 3 running at a time. This is what I have.
if [ "$COUNT" = "$(echo "$(cat /proc/cpuinfo|grep processor|wc -l)+1"|bc)" ] ;then
wait
COUNT="0"
fi
Above, I set a COUNT variable to 0, and append 1 in a for loop (the same for loop that runs the convert statement). Once it hits (in this case) 3, (number of processors+1), it will 'wait' until the childs are done, then continue.
This is all fine and dandy, but let's say all my images are 48k, except for 1 which is 250mb (not true, but for explanation purposes). Somewhere down the line, the script would wait for that ONE picture when the rest could be running.
So finally, my question. Given the context and background info above, is there a way (using the 'wait' command or not) to have a script stop at 3 (processors+1) childs, and only execute 1 as one finishes? In context, is there a way to have 2 pictures running while the 250mb picture is doing its thing, then have 3 once it finishes? As opposed to what I do now, just wait for all 3, then execute 3 more.
Thank you for any and all suggestions!

How do I make a function happen 50% of the time in vb6

Making a small app, and I want a function to execute 50% of the time. So if I were to dbl click the exe half the time the function would execute, and the other half it wouldn't. I can't seem to find anyway to easily do this, the one solution I tried seemed to determine the chance on compile rather than on run. Thanks in advance!
Generate a random decimal number between 0 and 1. If it is greater than 0.5 run, if it is less than or equal to 0.5 do not run.
Don't forget to seed the randomizer! Otherwise it will always give you the same value every time. You seed it using "Randomize Timer", e.g.:
Private Sub Main()
Randomize Timer
If Rnd > 0.5 Then
ExecuteFunction ()
End If
End Sub
For example:
Private Sub Main()
If Rnd > 0.5 Then
ExecuteFunction ()
End If
End Sub
If you want it to randomly run, others have already provided that solution. If you want a more deterministic behavior (it must run exactly every second time), you will need to store state between executions.
You can save the state in either the registry or on the file system by (for example) attempting to read an integer from a file (set it to zero if the file's not there), add 1 and write it back to the same file.
If the number written back was even, run your function otherwise exit.
That way, you'll alternate between execute and don't-execute.

Resources