0% found this document useful (0 votes)
5 views

Selection Structures

The document explains selection structures in C programming, which are used for decision making based on boolean conditions, including simple and compound statements. It covers branching types such as two-way and multi-way branching, as well as nested statements and the switch-case statement for handling multiple conditions. Additionally, it describes the use of getchar() for single character input from users.

Uploaded by

gamboamarcjill
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Selection Structures

The document explains selection structures in C programming, which are used for decision making based on boolean conditions, including simple and compound statements. It covers branching types such as two-way and multi-way branching, as well as nested statements and the switch-case statement for handling multiple conditions. Additionally, it describes the use of getchar() for single character input from users.

Uploaded by

gamboamarcjill
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SELECTION STRUCTURES

(Decision Making in C)

What is Selection Structure in C Programming?


Selection Structure use to make a decision or comparison and then, based on the result of that
decision or comparison, to select one of two paths. The condition must result in either a true (yes)
or false (no) answer. If the condition is true, the program performs one set of tasks.

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;

Compound Statements (Blocks):


▪ Compound statements, also known as blocks, are sequences of one or more statements
enclosed within curly braces {}.
▪ They allow you to group multiple statements together, making them appear and function as a
single unit.
▪ Compound statements are used for defining the body of functions, loops, conditional
statements, and more.
Example of a compound statement (block):
if (x > 0) {
y = x * 2;
z = y + 5;
}
In this example, the compound statement contains two simple statements.
(y = x * 2; and z = y + 5;)
The entire block is executed conditionally based on the outcome of the "if" condition.

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.

SWITCH CASE STATEMENT


switch case statement evaluates a given expression and based on the evaluated value(matching
a certain condition), it executes the statements associated with it. Basically, it is used to perform
different actions based on different conditions(cases).
• Switch case statements follow a selection-control mechanism and allow a value to
change control of execution.
• They are a substitute for long if statements that compare a variable to several integral
values.
• The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
In C, the switch case statement is used for executing one condition from multiple conditions. It
is similar to an if-else-if ladder.
The switch statement consists of conditional-based cases and a default case.
Syntax of switch Statement in C
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;

default: default_statement;
}

Rules of the switch case 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.

// C program to Demonstrate returning of day based numeric


// value
#include <stdio.h>

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.

When should use getchar() in C?


The getchar function is part of the <stdio. h> header file in C. It is used when single character input
is required from the user.

You might also like