Skip to main content

The Switch-Case Fall-through

So, I am a Software Engineer at a reputed firm and still fell for the trap of the fall-through feature of the switch-case statement.

Rule of Thumb:

  • Starting from the matched case, all statements within the switch block are executed, unless a break statement is encountered.
  • After the break statement, or if no break is encountered within the matched case, control is transferred to the statement following the entire switch block.

Examples:

Here are some examples to test your understanding of switch-case:

The output of the example is AB. This is because there is no break statement, and therefore all the statements are executed after the first match of case 0.

The output of the example is BCD. This is because there is no break statement, and therefore all the statements are executed after the first match of case 1.

The output of the example is AB. This is because there is a break statement at the end of case 1. Therefore, after the first match at case 0, all the statements are executed until the break statement.

The output of the example is AB. This is because there is a break statement at the end of case 2. Therefore, after the first match at case 1, all the statements are executed until the break statement.

So basically, after the first case match, the switch-case falls through all the subsequent statements. Although fall-through can be useful for grouping multiple cases together, it has been criticized for its potential to introduce complexity. Some languages, like C#, do not allow blocks without break statements unless the block is empty.

Comments

Popular posts from this blog

Architecture of High Performance Computing Server at BIT Mesra

A High-Performance Computing (HPC) server was installed a few years back. It was a replacement for PARAM 10000, the supercomputer that is no longer available for use. Initially, the HPC was under the Department of Computer Science. The Department of Chemical Engineering and Biotechnology was the primary user of the HPC (mostly for simulation purposes), and so the administration decided to move it under the Central Instrumentation Facility (CIF). You need permission from the CIF to access the HPC. HPC is only available for research purposes, and you need to provide a good reason along with a proper recommendation from a professor to gain access to the HPC. The HPC is at least 20 times more powerful than the most powerful PC that anyone has on campus. Also, I recently checked the usage and realized that not even 10% of its power is being utilized. I hope this blog post will help you in understanding the core architecture of the HPC. Architecture The Architecture of High Performance Compu...

Setting up Machine Learning Environment on High Performance Computing Server

In the last article, I discussed the architecture of the HPC. If you have not read that article, I would recommend that you read it before proceeding with this one. Architecture of High-Performance Computing Server at BIT Mesra  The power of HPC can be utilized for its most important application in the field of computer science: Machine Learning. I am assuming that you already have obtained your SSH credentials to log on to the master node. Also, we will be setting up the environment in Python. Let's jump straight to the steps. How To? Step 1: Download and Install Anaconda on the Master Node Note that you are not the root user of the HPC. You are just a regular user, and therefore, administrative commands (such as sudo or su) will not work. Anaconda has made it much easier to install Python packages for non-root users, and we will be using Anaconda for setting up Python 3 and installing the required packages. Log in to the master node     > ssh be1005815@172.16.23.1 Go...