Embedded Development Board Learning


If switch statements

Date: agosto 25, 2025

Author: Guillermo Garcia

Categories: Embedded C programming Tags:

In this article we will look at the if and switch selection statements, which are the most commonly used in C language programs for embedded systems.

Selection statements

C provides three types of selection statements. The first type is the if selection statement that either performs an action if a certain condition is met or skips the action otherwise. An type is the if else statement that allows to perform one out of two actions depending on whether the condition is met or not. The last type (switch) allows to select one of many possible actions.

if selection statement

if the expression between parenthesis yields a number different from 0 (=true), statement 1 is executed followed by statement 2. On the other hand, if the expression equals 0 (=false), statement 1 is skipped and statement 2 is executed directly.

if (<expression>)
 <statement 1>;
<statement 2>;

If more statements are to be executed when a certain condition is met, the statements can be grouped into a code block:

if (<expression>)
{
 <statement 1>;
 <statement 2>;
}
<statement 3>;

The if … else selection statement

if the expression between parenthesis yields a number different from 0 (=true), statement 1 is executed followed by statement 3. If the expression equals 0 (=false), statement 2 is executed followed by statement 3. Also in this case, the statements 1 and 2 can be block-statements:

if (<expression>)
 <statement 1>;
else
 <statement 2>;
<statement 3>;

Other form:

if (<expression>)
{
 <statement>;
 <statement>;
 …
}
else
{
 <statement>;
 <statement>;
 …
}

The conditional operator (?:)

The conditional operator in C is closely related to the if … else statement. It takes 3 operands. The first one is a condition, the second one is the value for the entire conditional expression if the condition is true and the third one will be used as value for the entire expression if the condition is false.

<condition> ? <value if condition is true> : <value if condition is false>

Example:

if (a < b)
z = a + 1;
else
z = b – 1;

can be written with the conditional operator as:

z = a < b ? a + 1 : b – 1 ;

Nested if .. else statements.

The code-blocks inside the if statement can again contain an if … else statement. As such nested if … else statements are created:

if(expression1)
{
 statement;
 …
}
else
{
 if(expression2)
 {
 statement;
 …
 }
 else
 {
 statement;
 …
 }
}

The switch statement

To select one of many actions, nested if statements can be used. If the choice for the right action can be made based upon the result of an integer expression, a switch statement is shorter and provides a clearer structure.

switch (<integer expression>)
{
 case <value1> :
<0 or more statements>;
break;
 case <value2> :
<0 or more statements>;
break;
 …
 default:
<0 or more statements>;
}
<next statement>

The value of the integer expression is tested against the constant integral values of every case. At the first match, the corresponding statements are executed. If the last statement corresponding to that specific case is a break; the execution of the switch will stop and the after the switch will be handled. If not, the statements belonging to the next case will be executed. This If no match is found, the default statements are executed. The default can be omitted. In that case, nothing will happen when no match occurs.

Summary

summary statement in C


Card image cap
Guillermo Garcia Thanks for reading! I am one of the editors of this site and I am committed to providing you with the best information possible by sharing my experience in embedded systems.


Comments... There are no comments.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Publicidad