Demystifying Programming Series (Part 3): Conditional Control and Functions

July 20, 2020
Demystifying Programming Series (Part 3): Conditional Control and Functions

Demystifying Programming Series: Part I Part II

In part III of the Demystifying Programming series, we’ll explore two fundamental concepts used in programming: conditional control and functions. They are both essential to programs and a programmer should know both in order to write good programs.

Conditional Control

Conditionals perform different actions depending on a single condition. This is crucial to programs, just like it is in life. Your whole life is made of conditionals which you base your actions on. Let’s imagine you’re doing a banal action such as filling up a glass of water. If you’re glass is empty, you’re going to fill it up. If your glass is full, you’re going to drink it. This is a conditional: the action you’ll take depends on the condition of the glass (whether it’s full or empty). The way to program conditionals in a program is using “if” statements. The structure is pretty simple to understand:

if(condition) {
	action1
} else {
	action2
}

If you have multiple outcomes from a single condition, you can use a switch statement. Let’s use the glass of water example again. Here’s what the structure looks like:

switch(condition) {
	case outcome1:
		break;
	case outcome2:
		break;
	
}

Let’s say we wanted to write a program that outputs the level in the glass. We could have the following switch statement:

switch(state of the glass of water) {
	case empty:
		print(The glass is empty)
		break;
    case quarter-full:
        print(The glass is a quarter full)
		break;
    case half-full:
        print(The glass is half full)
		break;
    case three-quarter-full:
        print(The glass is three-quarter full)
		break;
    case full:
        print(The glass is full)
		break;
}

Functions

Functions are reusable blocks of code which have an input and an output. They are a set of instructions which uses the input to produce the output. Functions have multiple names: functions, methods, procedures, etc. I like to use the term functions. For example, still using the glass of water example, we could have a function which would determine the state of the glass of water. This function could look like this:

function determineStateOfGlass(glass): string {
    string state = "";
    switch(glass.volume) {
        case 0:
            state = "empty";
            break; 
        case 0.25:
            state = "quarter-full";
            break; 
        case 0.50:
            state = "half-full";
            break; 
        case 0.75:
            state = "three-quarter-full";
            break; 
        case 1:
            state = "full";
            break; 
    }

    return state;
}

You can see this method takes in which glass of water we want to determine the state of and returns the state of the glass. Therefore, the input of a function is represented by the arguments that are passed into the function, which are passed in within the parentheses. Also, since we’re passing in the glass of water as an argument to the function, the method could be used for any glass of water, not just the one you’re filling up at the moment. This is one, if not the biggest, advantage of using functions: it allows to remove duplicate code and helps make your code a lot more readable. The output is whatever value is returned using the “return” statement. In the example above, it is possible to see the return value is the state of the glass.

Hopefully you now understand the use and importance of conditionals and functions in programming!