thinkingincode.ninja [Dark]

Conditional statements

In a program, it's often desired to execute different code if certain criteria are met. For example, if a user enters an incorrect password, a programmer may want that a popup be displayed and the user not allowed access to some resource. In such cases, conditional statements are used.

The most frequently used conditional statement is the if statement. There are three components, one of which is optional, to an if statement. The first is the condition; the second is what to do if the condition is met; the third (optional) component is what to do if the condition is not met. If the third component is present, a keyword like else will be present between the two blocks of code. In most languages the condition must be wrapped in parentheses ( ), and most languages require curly brackets { } around the latter two components. The keyword if is almost universally used for if statements.

int a = 53
int b = 97
int max


if(a > b) {
    max = a
} else {
    max = b
}

This bit of code will take the higher value between a and b and store it in max; running this code would result in max having a value of 97.

It's also possible to chain if statements together, like so:

int a = 53
int b = 97
string message


if(a > b) {
    message = "a is greater than b"
} else if(a == b) {
    max = "a is equal to b"
} else {
    max = "a is less than b"
}

Say you wanted to write a very simple calculator program. It may look vaguely like this (here I'm not prompting for the calculation or displaying the output, I'm focusing on the calculation):

float num1 = 25.2
float num2 = 19.7
float result
string operation = "divide"

if(operation == "add") {
    result = num1 + num2
} else if(operation == "subtract") {
    result = num1 - num2
} else if(operation == "multiply") {
    result = num1 * num2
} else if(operation == "divide") {
    result = num1 / num2
}

While that isn't terrible, it can definitely be improved. Many languages have something called a switch statement which can help in situations like this. First, I'll show you what it looks like, then I'll explain. Here I'm also introducing the semicolon; many languages require a semicolon at the end of every statement, which, amongst other things, lets you write more than one statement on a line (though generally speaking you only want one statement per line). I'm also adding in a little bit of error checking, to gradually introduce code samples that are more complex, as well as changing up the style in which I'm writing the code (it sounds worse than it is).

var num1: float = 25.2;
var num2: float = 19.7;
var result: float;
var operation: string = "divide";
var error_message: string;

switch(operation)
{
    case "add":
        result = num1 + num2;
        break;
    case "subtract":
        result = num1 - num2;
        break;
    case "multiply":
        result = num1 * num2;
        break;
    case "divide":
        if(num2 == 0)
        {
            error_message = "Cannot divide by zero";
            break;
        }
        result = num1 / num2;
        break;
    default:
        error_message = "Unknown operation given";
        break;
}

The switch statement allows a programmer to define an expression, and then define different blocks of code to be executed when the expression evaluates to specific values, as well as a default case when the value doesn't match any of the cases. The keyword break is usually used to signify the end of a block of code for a case, and will cause execution to continue at the next statement following the switch statement.

Suggested next reading: Loops