Trusted answers to developer questions

What are conditional statements in programming?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Human beings designed computer systems to make life easier for themselves. Computers make our life easier in various ways, too many to mention. For example, we use computers to perform complex ANOVA calculations, and computers are used to run software programs.

Since computers cannot think on their own, and human beings want to put computers into performing complex and even simple works, software need to be designed to run on the computer system and give it certain instruction to follow. To be able to know and understood the activity it needs to perform for its owner at any given time. Programing languages are used to achieve these aims.

Conditional statements, expressions, or simply conditionals are features of programming languages that tell the computer to execute certain actions, provided certain conditions are met.

Conditional statements are used through the various programming languages to instruct the computer on the decision to make when given some conditions. These decisions are made if and only if the pre-stated conditions are either true or false, depending on the functions the programmer has in mind.

All programming languages have conditional expression syntax (although the syntax slightly differs from one programming language to the others). For example, a typical C# conditional statement reads thus:

public static void Weather(string myDay)
{
if (myDay == "Sunny")
{
Console.WriteLine("Read in the Library");
}
else if (myDay == "Raining")
{
Console.WriteLine("Read at Home");
}
else if (myDay == "Cloudy")
{
Console.WriteLine("Read in the Garden");
}
else
{
Console.WriteLine("Get some Sleep");
}
}

From the above example, we can see how a program could be used to express various decisions based on the truthy or falsy of the given conditions. We have tested it for Weather("Cloudy").

Besides if, else if, and else conditional statements (as illustrated in the example above), programming languages also have if else (C and C++ programming language), elif (Python), and switch case (C#, Java, JavaScript, and a new feature in Python version 3.10).

The conditional statements are vital in the field of programming and software engineering, in that the conditions can be used by the programmers and software engineers to allow a machine to simulate the behavior of a person who has the ability to make choices and perform some actions based on the decision taken.

With conditional expressions, programming language gives the programmers tools and features they can manipulate to set a machine to effective work.

For the sake of clarity, let’s dive into the types of conditional statements. Bear in mind that all conditional statements return bool, i.e., true or false.

if statement

The if statement is the first condition a programmer uses to open the ground of the conditional statements. if syntax is as simple as writing if with open and close braces, followed by the condition the programmer intends to compare or check. The if expression simply compares whether the condition (or conditions) enclosed in the braces are true or false.

if true, the ifcode block executes. If false, the execution moves to the next block to check.

if (day == " Monday") // Condition
{
// Decision
Console.WriteLine("Go to School");
}

else if statement

While the if statement can be used to check one condition, else if is used to check multiple conditions. The else if statement (or elif in Python), has syntax similar to the if statement, then followed by the else if block. For example:

if (myDay == " Sunny")
{
// Decision
Console.WriteLine("Read in the Library");
}
// 2nd condition
else if (myday == "Raining")
{
//decision
Console.WriteLine("Read at Home")
}
// 3rd condition
else if (MyDay == "Cloudy")
{
// Decision
Console.WriteLine("Read in the Garden")
}

In else if statements, the conditions are checked from the top-down, if the first block returns true, the second and the third blocks will not be checked, but if the first if block returns false, the second block will be checked. This checking continues until a block returns a true outcome.

else statement

The else statement is the default statement of all the conditional expressions, in all programming languages. That is, when all the if and else if conditions return false (from top to bottom), then the final (default) else block statement executes. The else statement syntax is simply writing else followed by the default statement in open and close curly braces.

using namespace Conditional;
static void Main(string[] args)
{
// This method determines what a user should do
// for the day based on the weather condition
public void Weather(string myDay)
{
// 1st condition
if (myDay == " Sunny")
{
// Decision
Console.WriteLine("Read in the Library");
}
// 2nd condition
else if (myday == "Raining")
{
//decision
Console.WriteLine("Read at Home")
}
// 3rd condition
else if (MyDay == "Cloudy")
{
// Decision
Console.WriteLine("Read in the Garden")
}
else
{
// Default Decision
Console.WriteLine("Get some Sleep")
}
}
}

Find the executable example below:

using System;
namespace Conditional
{
class Program
{
static void Main(string[] args)
{
Weather("Sunny");
Weather("Raining");
Weather("Cloudy");
Weather("Unknown");
}
public static void Weather(string myDay)
{
if (myDay == "Sunny")
{
Console.WriteLine("Read in the Library");
}
else if (myDay == "Raining")
{
Console.WriteLine("Read at Home");
}
else if (myDay == "Cloudy")
{
Console.WriteLine("Read in the Garden");
}
else
{
Console.WriteLine("Get some Sleep");
}
}
}
}

switch

switch is another version of the conditional statement. It makes codes cleaner and more readable than the conventional if, else if, and else conditional statements.

In switch expressions, each block is terminated by a break keyword. The statements in switch are expressed with cases. For clarity, let’s use a switch statement to illustrate our previous example:

using System;
namespace Conditional
{
class Program
{
static void Main(string[] args)
{
int myDay = 4; // setting the value to test
switch (myDay)
{
case 1:
Console.WriteLine("Read in the Library");
break;
case 2:
Console.WriteLine("Read at Home");
break;
case 3:
Console.WriteLine("Read in the Garden");
break;
default:
Console.WriteLine("Get some Sleep");
break;
}
}
}
}

In the example above we have provided MyDay = 4

As previously stated, the switch conditional expression is a cleaner way of writing conditional statements.

Each case is tested until a true value is returned, then the code execution hits the break keyword.

If all the cases return false, the default block is executed.

Therefore, from the code example above, the default value will be executed since none of the test cases 1, 2, and 3 return true.

(The true case in this expression is myDay = 4;.)

RELATED TAGS

conditionals
edpressocompetition

CONTRIBUTOR

Abdulhafiz Suleiman Danmaigoro
Did you find this helpful?