Find your content:

Search form

You are here

Using or inside an if statement - unexpected or wrong results

 
Share

Hi I am having some issues with some simple conditions on an if statement when using 'or' to define the action, the below is an example that will have as a result a string with eleven characters, however for some reason the else if statement keep been excecute when that is not correct

Doesnt have apex the functionality of using this 'or' inside and if statement? however when using the code below on the developer console I dont get any error and it is been exccecuted. So I dont understand what is going on

string a = 'test string';
integer b;
b = a.length();
if(b <= 2){
    system.debug('b is less or equal to 2');
}else if(b > 2 || b<5){
    system.debug(b + ' is in between 2 and 5');
}else{
    system.debug('definetly longer than 5');
}

the result: Using or within if statement, APEX

Thanks


Attribution to: manza

Possible Suggestion/Solution #1

(b > 2 || b < 5) is true for the number 11 (11 is greater than 2).

Perhaps you meant instead (b > 2 && b < 5), which will be true for numbers between 2 and 5 (exclusive)?


Attribution to: Jonathan Hersh
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31952

My Block Status

My Block Content