Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

conditionals
communitycreator

What are conditional statements in programming?

Abdulhafiz Suleiman Danmaigoro

Introduction

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:

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")
    }
  }
}
C# language demo of if, else if and else conditional statements

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.

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");
    }
Checks **if** the day is Monday which can result to only true or false (bool).

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")
    }
Checking Multiple Conditional Statements with, if, else if

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")
    }

  }

}
Example of else statement

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:

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;
}
switch statements code example.

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
communitycreator

CONTRIBUTOR

Abdulhafiz Suleiman Danmaigoro
RELATED COURSES

View all Courses

Keep Exploring