For loop is executed even though if condition is not satisfied

For loop is getting executed (it doesn’t actually execute it just passes the statements in console which increases the execution time of the pgm) eventhough the if condition is not satisfied. In the below conditions the vv value is greater than 1. but as u can see the for loop is executed

2022-01-18 11:41:07.679 DEBUG testcase.tutor_add_class - 6: click(findTestObject(“Object Repository/tutor-add-class/classes_taught”))
2022-01-18 11:41:08.208 DEBUG testcase.tutor_add_class - 7: vv = classes_taught1
2022-01-18 11:41:08.209 DEBUG testcase.tutor_add_class - 8: if (vv != 0)
2022-01-18 11:41:08.210 DEBUG testcase.tutor_add_class - 1: for ([i = 1, i <= vv, (i++)])
2022-01-18 11:41:08.211 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:08.321 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:09.339 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:09.341 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:10.353 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:10.356 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:11.373 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:11.376 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:12.388 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:12.389 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:13.403 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:13.404 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:14.412 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:14.414 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:15.423 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:15.426 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:16.439 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:16.440 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:17.454 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:17.455 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:18.475 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:18.476 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:19.492 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:19.495 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:20.510 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:20.512 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:21.526 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:21.528 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:22.539 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:22.541 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:23.553 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:23.556 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:24.568 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:24.571 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:25.596 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:25.598 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:26.614 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:26.615 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:27.626 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:27.628 DEBUG testcase.tutor_add_class - 2: delay(1)
2022-01-18 11:41:28.643 DEBUG testcase.tutor_add_class - 1: robot.keyPress(VK_DOWN)
2022-01-18 11:41:28.645 DEBUG testcase.tutor_add_class - 2: delay(1)

You should rather say

In the below conditions the vv value is greater than 1. but therefore as u can see the for loop is executed

Can i know the solution for this prblm

I don’t understand what your problem is.

Perhaps we don’t understand your concern. You say that “vv value is greater than 1”. So that means that vv definitely does not equal 0 (zero), if (vv != 0), which means your condition is true. So, of course it should pass. Also, since vv is greater than 1, your for loop should also execute.

Or was there something else you were trying to do?

@grylion54 @kazurayam

Yes the if condition is true.
The vv value is 1
so the for loop will be (i=1;i<=1;i++)
{
WebUtil.robot.keyPress(KeyEvent.VK_DOWN)
}
So now when this exceutes it should only exceute 1 time ryt.
But it excutes more than 1 time

Here is the execution
Why is the down send keys is executing this much times. As you can see the vv value is 1. But the for loop gets executed more than 1 time.

6: click(findTestObject(“Object Repository/tutor-add-class/classes_taught”))
2022-01-20 11:24:54.029 DEBUG testcase.tutor_add_class - 7: vv = classes_taught_list_num
2022-01-20 11:24:54.030 DEBUG testcase.tutor_add_class - 8: print(“value of vv is : " + vv + " -”)
value of vv is : 1 -2022-01-20 11:24:54.031 DEBUG testcase.tutor_add_class - 9: if (vv != 0)
2022-01-20 11:24:54.032 DEBUG testcase.tutor_add_class - 1: for ([i = 0, i < vv, (i++)])
2022-01-20 11:24:54.033 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:54.661 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:55.101 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:55.500 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:55.896 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:56.287 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:56.677 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:57.110 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:57.483 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:57.863 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:58.263 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:58.651 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:59.042 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:59.430 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:24:59.827 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:00.181 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:00.582 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:00.958 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:01.356 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:01.750 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:02.137 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:02.514 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:02.898 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:03.273 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:03.658 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:04.050 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:04.479 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:04.855 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:05.263 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:05.643 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:06.030 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:06.402 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:06.776 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:07.163 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:07.535 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:07.928 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:08.341 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:08.717 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:09.109 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:09.482 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:09.886 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:10.257 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:10.637 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:11.026 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:11.409 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:11.785 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:12.191 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:12.578 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:13.028 DEBUG testcase.tutor_add_class - 1: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(DOWN))
2022-01-20 11:25:13.403 DEBUG testcase.tutor_add_class - 2: sendKeys(findTestObject(“Object Repository/tutor-add-class/classes_taught”), Keys.chord(ENTER))

This is the Actual script
WebUI.click(findTestObject(‘Object Repository/tutor-add-class/classes_taught’))
def vv = testCaseData.classes_taught_list_num
print(‘value of vv is : ‘+vv+’ -’)

			if(vv!=0)
			{
				for (int i = 0; i <vv; i++)
					{
						WebUI.sendKeys(findTestObject('Object Repository/tutor-add-class/classes_taught'), Keys.chord(Keys.DOWN))
					}
					WebUI.sendKeys(findTestObject('Object Repository/tutor-add-class/classes_taught'), Keys.chord(Keys.ENTER))
					WebUI.delay(1)

I’m just puzzled.

In the original post you attached the following.

Here I found a puzzling line

for ([i = 1, i <= vv, (i++)])

I do not understand this. This is not a valid Groovy code.

@ramprasad23

Are you using the “Manual mode”? or “Script mode”? or both?


I have a doubt, this could be a bug of “Manual mode” editor.

Could you please take 2 screenshots of

  1. how the for statement looks in the Manual mode.
  2. how the for statement looks in the Script mode.

@kazurayam i use script mode only

@kazurayam Here is what i more confused
As you can see the if condition is false. Even though the for loop is executed in console.
Am i doing wrong i am just beginner pls help me out here

I don’t think I am able to help you. Totally mysterious.

Please try some house-cleaning.

  1. make a full backup of your project just in case
  2. stop katalon studio
  3. remove the following 3 directories
  • <projectdir>/bin
  • <projectdir>/Libs
  • <projectdir>/.cache
  1. restart KS, reopen the project, and try runningi

You may want to

  • remove the current Katalon Studio installation from you disk
  • re-install Katalon Studio
  • and try

@kazurayam We have 4 other systems and all those system also face this issue

I guess you fed Gremlins gremlins after midnight, so you got problems.

@ramprasad23
perhaps you may have to look a bit more carefully in your code, e.g:

for (int i = 0, i !=vv, i++))

how it is supposed the for loop to end?
for your use case, perhaps a while loop is more suitable, see:
https://www.tutorialspoint.com/groovy/groovy_while_statement.htm

@bionel
As in the screen shot i used != for the if statement but it still executes the code even if the if condition is false

I would say you might have a concern outside of the area that you are showing us. Can you single click on each curly bracket of { and then see where Eclipse places its partner bracket? (Is that the Open Face curly bracket? Anyway, check if your brackets partner correctly.)

Lastly, you should also have a “robot.keyRelease(KeyEvent.VK_DOWN)” after the “keyPress”…

@grylion54 checked the bracket to i believe they are correctly placed

The if statement is executed on the console even if the condition is false.

This may sound stupid, but delete the if line and retype it. Another option, copy your code and paste it into NotePad++ and look for any non-printable text. Code does not do something like you’re stating unless there is a reason. (Otherwise, all your if condition statements should be doing the same thing.)