Making decisions:   if, then, else, switch-case


One of the most useful things we can do in computer code is to instruct the machine to make information-based decisions. In this section we'll learn how to use the statements if, then, else, and switch-case to make decisions about program flow.

You can think of these instructions just as you would a flowchart, like this one.

In this simple example, a sort of absolute-value evaluator, we begin with a number that can be positive or negative. We ask whether the number is greater than or less than zero. If it's greater, it is used in a calculation. If not, it is first multiplied by -1 to make it positive (absolute value), and then used in the same calculation.

In this section we'll code this simple example, then do some more complicated decision structures.


Simple if - else example (Python)




Even simpler

Just to be clear, an else statement isn't needed. If you just need your code to make a decision to do something or not and move on, a simple if statement like this will work:



if the variable stored in value is True, then the phrase will be printed, otherwise the reading of the code moves on.

instruction explanation
number = input("Enter number", n) This is an input statement. It prints the message to the screen, then waits for the result to be typed in. The value n is stored in number.
if (number > 0): If the number entered is greater than zero (i.e. positive), the indented code is executed. Otherwise, the program moves on.
else: else captures any other contingency, namely that number is negative or zero. The code indented below the else: statement is executed.

simple if-else example ( c )



instruction explanation

printf("Enter ...);
scanf("%f", &number);

The printf statement prints a message to the screen, and the scanf function reads the next thing the user types in as a floating-point number.
if (number > 0) { ... } if statements in Python and c are similar. c always uses {curly braces} to enclose the code to be executed if the if condition is met.
else { ... } else captures all other contingencies. If the if condition is not met, then the instructions in braces under the else statement are executed.

if - elif block (Python)


Don't use   =   for comparisons

A very common mistake is to use = to compare two variables in an if statement. The = sign is the assignment operator. It assigns a value to a named memory location.

To compare two variable values, use the == operator. For example (if a == b): asks the question "is a the same as b?"

instruction explanation
x =input ("Enter a letter) Ask the user to enter a character.
if (x == 'a'): if the value of the character is lower-case a, execute the instruction indented below, in this case it's assigning a value of 1.0 to number.
elif elif is a contraction of else-if. The block of elif statements is evaluated, each in turn, until one matches, or until the code under the else statement is evaluated. If one condition matches, that code is executed and then the program exits the loop. Only one condition can be true.

Comparison operators (Python & c)

Op. means Op. means
== equal to <> not equal to
!= not equal to >= greater than or equal to
> greater than < less than
<= less than or equal to === This javasrcript comparator checks that the value and variable type are the same.


instruction explanation
printf(%s, "Enter choice " Print a string message to the screen to prompt the user for a letter.
scanf(%f, &x) Accept the next character after an ENTER from the keyboard. Remember to pass scanf the address ( & ) of x, not x itself.
if (x == 'a'): if the value of the character is lower-case a, execute the instruction below (no braces needed if it's only one instruction, in this case it's assigning a value of 1.0 to number.
else if ( ... ) else if statements are evaluated until one is found to be true, then the others are ignored. If none is true, the else statement - the default - is executed.

switch - case - default (c only)

The switch statement and its associated keywords case & default is a nice way to make decisions, especially when there are many options.

Here's an example of some c-code that reads a character from the keyboard and counts the number of a's, b's, c's and "other" characters pressed, printing an updated list on each iteration.



instruction explanation
printf(%s, "Enter choice " Print a string message to the screen to prompt the user for a letter.
scanf(%f, &x) Accept the next character after an ENTER from the keyboard. Remember to pass scanf the address ( & ) of x, not x itself.
if (x == 'a'): if the value of the character is lower-case a, execute the instruction below (no braces needed if it's only one instruction, in this case it's assigning a value of 1.0 to number.
else if ( ... ) else if statements are evaluated until one is found to be true, then the others are ignored. If none is true, the else statement - the default - is executed.

Creative Commons License   optimized for firefox
xaktly.com by Dr. Jeff Cruzan is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. © 2016-2019, Jeff Cruzan. All text and images on this website not specifically attributed to another source were created by me and I reserve all rights as to their use. Any opinions expressed on this website are entirely mine, and do not necessarily reflect the views of any of my employers. Please feel free to send any questions or comments to jeff.cruzan@verizon.net.