If statement not working like needed.

So on press the score goes up one and “GoOne” is printed. What I want is so the user can press 4 time and the “GoOne” is printed. Then on the 5 and so on press the “GoTwo” statement prints. But the code here doesn’t do that. Why?

local score = 0 local function updateScore(event) score = score + 1 end local function go(event) if event.phase == "ended" then updateScore() if score \>= 0 then print("GoOne") elseif score \>= 5 then print("GoTwo") end end end Runtime:addEventListener( "touch", go )

–SonicX278 

Switch the positions. If score is greater than 5 it will always be greater than 0 which means that “GoOne” will always be printed. 

if score \>= 5 then print("GoOne") else if score \>= 0 then print("GoTwo") end

Because the first part of your statement is always satisfied, so the logic never progresses to the  elseif portion of the statement.

Yes, as Alex pointed out, your first if statement is always satisfied. The elseif is only check in the case that the first one is not satisfied. i.e. if ‘score >= 0’ IS true then it does the first part of code (prints “GoOne”) - but if ‘score >= 0’ is NOT satisfied, then it will check the elseif statement. But it never gets that far, since score is always 0 or more.

So, to fix it, you need to add another condition to your first if;

local score = 0 local function updateScore(event) score = score + 1 end local function go(event) if event.phase == "ended" then updateScore() if score \>= 0 and score \< 5 then print("GoOne") elseif score \>= 5 then print("GoTwo") end end end Runtime:addEventListener( "touch", go )

If you add in the “and score < 5” then once score gets to 5, the first IF is NOT true, so then it’ll check the elseif conditions.

Cheers,

Simon

Dixon Court Entertainment

Switch the positions. If score is greater than 5 it will always be greater than 0 which means that “GoOne” will always be printed. 

if score \>= 5 then print("GoOne") else if score \>= 0 then print("GoTwo") end

Because the first part of your statement is always satisfied, so the logic never progresses to the  elseif portion of the statement.

Yes, as Alex pointed out, your first if statement is always satisfied. The elseif is only check in the case that the first one is not satisfied. i.e. if ‘score >= 0’ IS true then it does the first part of code (prints “GoOne”) - but if ‘score >= 0’ is NOT satisfied, then it will check the elseif statement. But it never gets that far, since score is always 0 or more.

So, to fix it, you need to add another condition to your first if;

local score = 0 local function updateScore(event) score = score + 1 end local function go(event) if event.phase == "ended" then updateScore() if score \>= 0 and score \< 5 then print("GoOne") elseif score \>= 5 then print("GoTwo") end end end Runtime:addEventListener( "touch", go )

If you add in the “and score < 5” then once score gets to 5, the first IF is NOT true, so then it’ll check the elseif conditions.

Cheers,

Simon

Dixon Court Entertainment