top of page

Conditional Statements in C

Introduction


Imagine a fork in the road, where each route leads to a different place.



In the same way, conditionals allow our program to take many paths, and thus, lead to many unique outcomes.


So...


if (you're interested in learning C)
{
	// continue reading
}

Booleans and Common Conditionals


Inherently, all conditional statements rely on some boolean expression that evaluates to either true or false.


For instance:

#include <stdio.h>

int main(void)
{
	// retrieves user input, storing the value at the address of x
	scanf("%d", &x);

	// returns -1 if the number the user has entered was not 3
	if (x != 3)
		return -1;

	return 0;
}

Here, the boolean expression was x != 3, which could evaluate to true or false depending on what the user enters. Notice how if the conditional was false and x was indeed 3, the if would be skipped and 0 would be returned.


Some fairly common examples of conditionals include:


  • if

if (boolean-statement)
{
	...
}
  • if-else

    • where else is a catch-all and requires no boolean expression, unlike if

    • else only runs if the if statement was false, and vice versa

if (boolean-statement)
{
	...
}
else
{
	...
}
  • if-else if ... - else

    • else if is essentially an else with a boolean expression

    • else if only runs if the statement previous to it was false

    • can be a chain of else if statements

    • *does not need to end with an else

if (boolean-statement)
{
	...
}
else if (boolean-statement)
{
	...
}
else if (boolean-statement)
{
	...
}
else if (boolean-statement)
{
	...
}
else
{
	...
}

Switch( ) Statement


switch( ) statements are very similar to if-else if-else chains, in that they result in different outcomes based on their input.


They can be, however, more dynamic, as multiple statements can execute after one another like a cascade.


#include <stdio.h>

int main(void)
{
	scanf("%d", &x);

	switch(x)
	{
		case 1:
			printf("One!\n");
			break;
		case 2:
			printf("Two!\n");
			break;
		case 3:
			printf("Three!\n");
			break;
		default:
			printf("No cases were met!\n");
			break;
	}

	return 0;
}

Here, with x as input, the switch( ) statement compared the value stored in x with each value following each case. For example, if I had entered 1, the program would print "One!" If I hadn't entered any of the three case values, the program would resort to the default.


So how is this different from traditional conditionals?


Well, notice how we had to use a break statement after each print() in order to stop the cascading behavior mentioned before.


Remove these breaks, and should the user enter 2, the program would output:

One!
Two!
Three!
No cases were met!

You can imagine how this type of conditional could be useful in things like a countdown.


Shortcuts...


For writing trivially short conditional statements, C is equipped with what is called a ternary operator ( ? : ).


I could rewrite:

const int num = 2;

// assume the user enters an integer guess
scanf("%d", &userGuess);

int points;
if (userGuess == num)
{
	points = 1;
}
else
{
	points = 0;
}

as:

const int num = 2;

// assume the user enters an integer guess
scanf("%d", &userGuess);

int points = (userGuess == num) ? 1 : 0;

The ternary operator allows us to check the first boolean expression (userGuess == num) and if that is true, the variable holds the value after the ( ? ). If that expression is false, the variable holds the value after the ( : ).


Final Thoughts


Hopefully by the end of this article you've at least gained a brief idea of the different types of conditional statements and when to them. Thanks for reading!


Meanwhile, stay tuned for updates by following my blog and LinkedIn page.

Comments


bottom of page