Hi,
I have the following sample if elseif statement. When I execute this, the else if is executed and right after that it entered in to last else statement. I thought the else statement will be executed only when all if conditions are false. Not sure whats wrong I am doing.
This is my actual code
int jobCntNum = 21
int countTotalJobsListed = ‘’
int lastPageNum = Integer.parseInt(pageNum)
for (int i = 1; i <= lastPageNum; i++) {
List<String> jobCardsPerPage =
resultsSection.findElements(By.xpath('//div[@class=\'pagination-
results\']//div[@class=\'career-card-header\']/a'))
countTotalJobsListed = (countTotalJobsListed + jobCardsPerPage.size())
if (jobCntNum == countTotalJobsListed) {
CustomKeywords.'ciautils.ciaUtilities.checkStringDisplayed'('The Total jobs posted ', countTotalJobsListed,
jobCntNum)
} else if (i != lastPageNum) {
driver.findElement(By.xpath(GlobalVariable.jobResultsSection + '//*[name()=\'svg\' and @aria-label= \'Forwards\']')).click()
} else {
println('Total jobs posted doesn\'t match with the count shown as jobs found')
}
}
}
if (value == true) {
println(‘true’)
} else {
if (value == true) {
println(‘false’)
} else {
println(‘false else’)
}
}
Mantra:
ALWAYS use braces { }
around if-blocks and else-blocks.
ALWAYS indent each block correctly. Obey this rule for EVERY sub-block.
If you read the code above, that’s the best I can make of your logic. If it’s incorrect, only you know what you really meant.
you assign false
to value
. so, all conditions are false because you check for true.
Sorry, that was my mistake when I wrote this ticket, but the else if block has value == false. I edited in my code above to have actual script.
That is for nested if condition as what you gave me. but I am looking for if elseif … else. Please look in to my actual code edited above
String hero = "Robin"
if(hero == "Batman") {
} else {
if(hero == "Batgirl") {
} else {
println hero
}
}
those assignments looks weird. you declare an int but you pass an empty string as value
after that you declare another int, lastPageNum
parsing … an unknown pageNum
. which is further used in the else if
condition
so … how it is supposed we can debug your code, with so many unknown variables?
LE: @Russ_Thomas this variant it is also supported by java, should work same as your example, provided the conditions are properly written:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
@anon46315158, Thanks for your responses. I didn’t post the complete code as even if I post that it will not help you as pageNum is getting number from the application. So by looking at the variable names I thought it will be understandable as what type of data I might be sending. Sorry if that didn’t provide you the details.
Anyhow the issue got resolved and now I am able to go through the if conditions and get what I was expecting. All I did was I made sure all the variables used in that code are integers and so it worked fine.
@vtanguturi you can put some print variable
statements before the if
for all used in conditions to debug how your code is working
This is what it printed for all the variables with in that if condition
jobCntNum is 20, countTotalJobsListed is 20, and lastPageNum is 2