Selection instruction, “if-else” and “switch”

Andrea Alicino
2 min readApr 6, 2021

“If-else” and “switch” are both selection instructions. Selection statements transfer program flow to the particular instruction block based on whether the condition is “true” or “false”.

if-else
The general form of if-else statements is as follows

where “if” and “else” are the keywords and statements can be a single statement or a block of statements. The expression evaluates to be “true” for any value other than zero and for zero it evaluates to “false”.
The if statement can contain an integer, a character, a pointer, a floating-point, or it can be a boolean type. The if statement is optional in an if-else statement. If the expression returns true, the statements within the if statement is executed, and if it returns false the statements within the else statement are executed, and, in the event that another statement is not created, no action is taken and program control is skipped out of an if-else statement.

switch
when you have to make a choice on a group of more than two it is better to use the switch that according to the condition selects the behavior to follow.

the keywords are ‘case’ which defines the block of instructions we will choose to execute. And ‘break’ which will immediately take us out of our switch. In addition to the various cases you can enter a neutral state, called ‘default’ which captures the case that none of the possible choices are selected.

The switch instruction is easy to modify as it has created separate instances for the different instructions whereas, in nested if-else instructions it becomes difficult to identify the instructions to be modified.

--

--

Andrea Alicino

Game Developer Unreal Engine/Unity. Computer science graduate. Seeking new opportunities.