Learning C++: Table-Driven Selection

Learning C++: Table-Driven Selection

The typical way to perform selection (or branching) in C++ is with the if statement. In this article I’m going to demonstrate several ways to replace complex if statements with tables.

The typical way to perform selection (or branching) in C++ is with the if statement. However, for many scenarios that come up consistently, putting the logic into a table is a more efficient way to perform selection in code. In this article I’m going to demonstrate several ways to replace complex if statements with tables.

Direct-Access Tables

The first type of table-driven selection I want to discuss is using direct-access tables. The example I will use to demonstrate direct-access tables is determining the number of days in a month.

The typical approach to this problem, or what I call the Computer Science 101 approach, is to encode the days in a month in an if-else if statement, like this:

if (month == 1) {
  days = 31;
}
else if (month == 2) {
  days = 28;
}
else if (month == 3) {
  days = 31;
}
else if (month == 4) {
  days = 30;
}
else if (month == 5) {
  days = 31;
}
else if (month == 6) {
  days = 30;
}
else if (month == 7) {
  days = 31;
}
else if (month == 8) {
  days = 31;
}
else if (month == 9) {
  days = 30;
}
else if (month == 10) {
  days = 31;
}
else if (month == 11) {
  days = 30;
}
else if (month == 12) {
  days = 31;
}

That’s a lot of code that takes up a lot of space in a program and a lot of time to read through.

An easier way to encode this data is in an array where the array index represents the month number and the element stored at that index is the number of days in that month.

Here is a program that demonstrates how to use an array as a direct-access table for looking up the days in a month:

#include <iostream>
using namespace std;

int main ()
{
  int month = 0;
  int days = 0;
  const int numMonths = 12;
  int monthDays[numMonths] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  string another = "y";
  do {
    cout << "Month number to look up? ";
    cin >> month;
    days = monthDays[month-1];
    cout << "There are " << days << " days." << endl;
    cout << "Do another (y/n)? ";
    cin >> another;
  } while (another == "y");
  return 0;
}

Here is an example of the output of this program:

Month number to look up? 7
There are 31 days.
Do another (y/n)? y
Month number to look up? 2
There are 28 days.
Do another (y/n)? n

The complete program using a direct-access table lookup is shorter than just the if-else if statement used above.

cpp learn-to-code programming-education learn-to-program programming-tips deep learning

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Deep Learning Development Services | Hire Deep Learning Developer

Inexture's Deep learning Development Services helps companies to develop Data driven products and solutions. Hire our deep learning developers today to build application that learn and adapt with time.

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

Learning to Code: How to Boost Up the Process?

Learning to Code: How to Boost Up the Process? I also often recommend different online and offline resources to my students to make their learning process easier, more effective, and faster. And in this post, I will share a few tips with you.

Learning C++: Variable Scope

Variable scope refers to the ability of a variable to be “seen” in a program. A variable is seen if, in a specific place in a program, the programmer has access to that variable’s value.

The Most Important Programming Lesson I Ever Learned

The most important lesson in programming is learning to debug code. Here is a list of my favorite debugging tips and tricks in JavaScript, Python, and more.