Control Flow In C Programming

Danny Padron
4 min readJan 29, 2021

Generally like most other programming languages C executes code from the top of the source file to the bottom. Having Control Flows within your program can change the flow of how the program is executed based on decision making, looping or branching statements. These three different statements are all used for specific things.

The decision making statement uses the keywords if, if...else, switch, goto . Decision making statements require that you specify one or more conditions to be evaluated for it to be executed. The most common statement used for decision making statements is the if statement. The if statement is a boolean expression that is followed by one or more statements. If the boolean evaluates to true then the statement is executed. If the boolean evaluates to false then some other statement will be executed. Usually in most if statements there is an else statement that follows which is what will be executed if the boolean evaluates to false. You can even have up to multiple else if statements as well. Here are some examples of those :

// Basic 'if' statement syntax#include <stdio.h>int main ()
{
int score = 95;
int big = 90;
if (score > big)
printf("Jackpot!");
return 0;
}
// Basic 'if...else' statement syntax#include <stdio.h>int main () 
{
int score = 95;
int big = 90;
if (big > score)
printf("Jackpot!");
else
printf("You lose!");
return 0;
}

In the first example we can see how if (score > big) is making a decision. It is evaluating to see if the variable score has a greater value than the variable big . That is our boolean expression and in this example score does have a greater value so it will evaluate to true which will print out Jackpot!

In the second example we can see almost the same if statement but with an added else statement after. This time we want our if statement to evaluate whether the variable big has a greater value than score . In this example this boolean expression evaluates to false, which then moves down to the else statement and the program ends up printing out You lose! . This is just a basic use of the if and if...else statements. They can get much more complex because you can nest if statements into other if statements or else if statements as well and have the program evaluate multiple different boolean expression to make the decision making more specific.

To make more complex decisions like this or more specific when the program needs to choose one out of multiple different alternatives there is a much easier decision making statement that can be used for this, it is the switch statement. Using the switch statement also requires you to use certain other keywords within your statement for it to run properly. The following example is a simple switch statement syntax using enums :

#include <stdio.h>int main ()
{
enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday};
enum Weekday today = Tuesday;
switch(today)
{
case Monday:
printf("Today is Monday.");
break;
case Tuesday:
printf("Today is Tuesday.");
break;
case Wednesday:
printf("Today is Wednesday.");
break;
case Thursday:
printf("Today is Thursday.");
break;
case Friday:
printf("Today is Friday.");
break;
default:
printf("It's the Weekend go party!!");
break;
}
}

In the example we made an enum called Weekday and then set the enum Weekday today to Tuesday. When we use the switch(today) statement the program will go through every case , which is working the same as an else if statement, and see which case statement is going to evaluate to true according to the value of today . It will check case Monday: which will evaluate to false, then it will move on to case Tuesday: which when evaluated is going to equal the same value as today which means it will evaluate true and then it will move into that program statement of printf("Today is Tuesday."); . The break; after every case is needed because without it the program would carry on into the next case. For example if the break; wasn’t there after printf("Today is Tuesday."); then the program would end up printing out Today is Tuesday. Today is Wednesday. because it didn’t know to stop. The break: statement essentially stops the switch statement from continuing and then transfers the following execution of the program to the immediately following statement after the switch. The last important keyword that is needed for the switch statement to work properly is the default: keyword. The default: statement must always evaluate to true. If today was anything other than the five days of the week, the program will run the default program statement and print out It's the Weekend go party!! .

The other common form of Control Flow used in C programming as well as other languages is Loops. Loops are useful when you need the program to execute a certain block of code multiple times. There are different types of loops: while loop, for loop, do...while loop, and also nested loops. The main purpose of the loop is it will repeat a statement while the given conditions of the loop are true. It tests the condition before executing the body continuously until the condition is evaluated to false. If the loop never evaluates to false then it will be considered an infinite loop because it will never end, it will continue to repeat the same block of code because it will continue to evaluate to true.

This is just the basic use of these multiple different control flow options that are available to use within C programming. These control flows can all get very complex depending on the program but even with complex nested if else statements or even nested loops knowing the difference between them and knowing when to use which one in which situation is what will make great code that isn’t that complex to follow along with.

--

--

Danny Padron

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!