Creating conditional statements with single-alternative and dual-alternative decision structures. Welcome to the if statement!

Let’s break it down.

Decision structures are the baseline for understanding most programs. Given a boolean value (true or false) the structure dictates what will happen in the program.

Sing-alternative decision structures

For single-alternative decision structures, there is only a path to statement execution if the boolean value is true. If the boolean statement is false, the statement is skipped by the program.

Note that curly braces are unnecessary in C++ and C# if you have a single-alternative decision structure, but you may use them for readability.

C++

int num = 5;
if (num < 10)
    // Print out that the boolean is true
    cout << num;

C#

int num = 5;
if (num < 10)
    // Print out that the boolean is true
    Console.WriteLine("Returned true")

Go

var num int = 5
if 10 % num == 0 {
    fmt.Println("10 is divisible by num(5)")
}

Python

num = 5
if 10 > num:
    print("10 is greater than num(5)")

JavaScript

const num = 5
if (num != 0) {
    console.log("num(5) is not equal to 0!")
}

Ruby

num = 5
if num > 1
    puts "num(5) is greater than 1"

Dual-alternative decision structures

Dual-alternative decision structures add one more level of execution. If the boolean is true it will execute the code within the if and if the boolean is false it will execute the code within the else statement. These statements are separated by curly brackets.

In addition, we can add an elseif or elif in the middle of an if and else to catch various other conditions.

C++

#include <iostream>
using namespace std;

int main () {
   int num = 5;
   if( num < 5 ) {
      cout << "num is less than 5;" << endl;
   } else {
      cout << "num is not less than 5;" << endl;
   }
   cout << "value of num is : " << num << endl;
   return 0;
}

C#

int num = 5;

if (num != 5)
{
    Console.WriteLine("The num variable is a number other than five.");
}
else
{
    Console.WriteLine("The num variable is set to five.");
}

Go

package main
import "fmt"

func main() {
	if num := 5; num == 5 {
		fmt.Println("The number is 5")
	} else {
		fmt.Println("The number is not 5")
	}
}

Python

#!/usr/bin/python
num = 100
if num:
   print "Got a true expression value"
   print num
else:
   print "Got a false expression value"
   print num

saying = "You get what you pay for"
if saying:
   print "Got a true expression value"
   print saying
else:
   print "Got a false expression value"
   print saying

JavaScript

This is a switch statement. Its general function is to find truth, just like an if statement. JavaScript also has if/else statements.

Taking num into the switch, 0 is compared with the case variables. The first one is case 0:, so that’s the true statement in this instance. The program executes the code under the case 0: statement and breaks out of the switch statement entirely because its job is done.

const num = 0;
switch (num) {
  case 0:
    text = "Off";
    break;
  case 1:
    text = "On";
    break;
  default:
    text = "No value found";
}

Ruby

x = 5
if x > 10
    puts "x is greater than 10"
elsif x <= 2 and x != 0
    puts "x is 1"
else
    puts "I can't guess the number"
end

Happy coding!

E


Got Questions❓, Comments 🗣 or Edits ✍

Use the Twitter thread below and hashtag #E4Everything to get in touch with me regarding this blog post:

Previous: Basics - Part 1Next: Google Client Side OAuth with ReactJS