Selection Structures
Selection Structures
(Decision Making in C)
Selection Structure in C:
To get a better idea of how selection structures work, the following three examples demonstrate
how if-end, if-else, and else if structures can be used to test if a variable is above or below a certain
value or values. if-end selection structures are used when only one boolean condition is necessary.
SIMPLE and COMPOUND statement refer to the types of statements that are used to create code
blocks. These terms relate to the structure and complexity of the code within the program.
Simple Statements:
Simple statements are single operations that end with a semicolon. They are the basic building
blocks of C programming. Examples of simple statements include variable assignments, function
calls, and arithmetic operations.
Example of a simple statement:
x = 10;
In summary:
Simple statements: Single operations that end with a semicolon.
Compound statements (blocks): Groups of one or more statements enclosed within curly braces,
used for defining the body of functions, loops, conditionals, and more.
Using both simple and compound statements, you can build complex programs by combining and
arranging these building blocks to achieve the desired functionality.
BRANCHING
Branching in programming refers to the ability to make decisions and execute different code paths
based on certain conditions. There are two main types of branching: two-way branching and multi-
way branching.
Two-Way Branching:
Also known as binary branching, this involves making a decision between two possible outcomes.
It's typically implemented using an "if-else" statement. If the condition specified in the "if"
statement is true, one block of code is executed; if the condition is false, the code in the "else"
block is executed.
Example Code:
num = 10;
if (num > 0)
printf("Number is positive");
else
printf("Number is not positive");
Multi-Way Branching:
Also known as n-way branching, this involves making decisions among three or more possible
outcomes. It's often implemented using a series of "if-else if" (or "if-elif") statements. The
conditions are evaluated one after another, and when a true condition is found, the corresponding
code block is executed.
Example Code:
int score = 85;
if (score >= 90) {
printf( "A" );
}
else if (score >= 80) {
printf( "\n B" );
}
else if (score >= 70) {
printf( "\n C" );
}
else if (score >= 60) {
printf( "\n D" );
}
else {
printf( "\n F" );
}
In multi-way branching, only the code block corresponding to the first true condition is executed.
Once a true condition is found, the rest of the "else if" conditions are skipped.
Both two-way and multi-way branching are essential in programming for creating flexible and
adaptable code that can handle various scenarios and conditions.
Nested Statements:
Nested statements refer to the practice of placing one statement or block of statements within
another statement or block.
They are commonly used to achieve more intricate and detailed control flow by embedding
conditional statements or loops within other statements.
Example of nested if statements:
if (x > 0) {
if (y > 0) {
// Code to execute if both x and y are positive
}
}
In this example, the second "if" statement is nested within the first "if" statement, creating a nested
structure.
Nested statements: Placing one statement or block of statements within another, often used to
create more complex control flow by embedding statements within other statements.
default: default_statement;
}
Following are some of the rules that we need to follow while using the switch statement:
1. In a switch statement, the “case value” must be of “char” and “int” type.
2. There can be one or N number of cases.
3. The values in the case must be unique.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.
int main()
{
// switch variable
int var = 1;
// switch statement
switch (var) {
case 1:
printf("Case 1 is Matched.");
break;
case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;
default:
printf("Default case is Matched.");
break;
}
return 0;
}
How switch Statement Work?
The working of the switch statement in C is as follows:
1. Step 1: The switch variable is evaluated.
2. Step 2: The evaluated value is matched against all the present cases.
3. Step 3A: If the matching case value is found, the associated code is executed.
4. Step 3B: If the matching code is not found, then the default case is executed if present.
5. Step 4A: If the break keyword is present in the case, then program control breaks out
of the switch statement.
6. Step 4B: If the break keyword is not present, then all the cases after the matching case
are executed.
7. Step 5: Statements after the switch statement are executed.