DEV Community

Cover image for Conditional Logic
Greg Ross
Greg Ross

Posted on • Updated on

Conditional Logic

Computer science concepts in my words.

If this, then that

Evaluating data based on a defined reason either allows the information to undergo additional computation or bypasses that circumstance until a certain condition is met or else. Pun intended.

if (condition) {
    run code 
}
Enter fullscreen mode Exit fullscreen mode
if (condition) {
    run code
} else {
    run code
}
Enter fullscreen mode Exit fullscreen mode

Checks

Maintaining sanity in a chain of commands starts with prioritizing the most important first. The data can be cascaded from one block to another until nothing checks true.

if (condition) {
    run code
} else if (other condition) {
    run code
} else if (other condition 2) {
    run code
} else {
   run code
}
Enter fullscreen mode Exit fullscreen mode

Statements

The conditions that are checked are evaluated based on the expressions stated. An oversimplification would be a simple yes or no. Meaning true or false. Yes you can pass or no do not pass.

boolean lessThan = a < b; 
boolean lessThanOrEqual = a <= b;

boolean greaterThan = a > b;
boolean greaterThanOrEqual = a >= b;

boolean equal = a == b;
boolean notEqual = a != b;
Enter fullscreen mode Exit fullscreen mode

More Complex

In addition to a daisy chain of if/else blocks. The conditions being evaluated can be intrinsically linked together and complicated.

int ohThis = 7; 
int that = 13; 

boolean and = (ohThis == 7 && that == 13); 
boolean or = (ohThis == 7 || that == 13); 

boolean isTrueAndTrue = (ohThis >= 10 && ohThis <= 15) && (that >= 10 && that <= 15);
boolean isTrueOrFalse = (ohThis >= 10 && ohThis <= 15) || (that >= 10 && that <= 15);
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)