Xii Chapter 7 by Sir Sheikh Emad
Xii Chapter 7 by Sir Sheikh Emad
7
SELECTION CONTROL STATEMENTS
OBJECTIVE:
When you have completed this chapter, you will be able to
know about the
Q2. Explain the Switch Statement, provide its structure, and also an example of code in C
language.
Control Structure
A statement that is used to control the flow of execution in a program is called control
structure. It combines instruction into logical unit. Logical unit has one entry point and one
exit point.
2). Selection:
It selects a statement to execute on the basis of condition. Statement is executed
when the condition is true and ignored when it is false e.g. if, if else, switch
structures.
Flow chart
if (condition) {
// code to be executed if the condition is true
}
C if-else Statement
The if-else statement is a decision-making statement that is used to decide whether the
part of the code will be executed or not based on the specified condition (test
expression). If the given condition is true, then the code inside the if block is executed,
otherwise the code inside the else block is executed.
if (condition) {
// code executed when the condition is true
}
else {
// code executed when the condition is false
}
Q3.Define Nested if statement.
Nested if-else in C:
A nested if in C is an if statement that is the target of another if
statement. Nested if statements mean an if statement inside another if statement. Yes, C
allow us to nested if statements within if statements, i.e. we can place an if statement inside
another if statement.
1. The If Statement: This stage has its first condition. If the condition is true in the first step,
the block of code will execute.
2. The Else If Statement: If the condition is false in this second stage, the program flow will
go to the else if statement, and again it checks the condition. If the condition in the else if
statement is true, the block of code within it will execute directly.
3. The Else Statement: If both of the conditions specified in the if and else if statements are
true, the code block within the Else statement will execute directly.
CONDITIONAL STATEMENTS:
The conditional statements (also known as decision control structures) such as if, if else,
switch, etc. are used for decision-making purposes in C programs.
They are also known as Decision-Making Statements and are used to evaluate one or more
conditions and make the decision whether to execute a set of statements or not. These
decision-making statements in programming languages decide the direction of the flow of
program execution.
if Statement
if-else Statement
Nested if Statement
if-else-if Ladder
switch Statement
1. if in C
The if statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e. if a certain condition is
true then a block of statements is executed otherwise not.
Syntax of if Statement:
PREPARED BY: SIR SHEIKH EMAD
if(condition)
{
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. C if statement accepts boolean
values – if the value is true then it will execute the block of statements below it otherwise not.
If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement
will consider the first immediately below statement to be inside its block.
If the condition in the if statement is false, the block of code below the if statement is not
executed.
2. if-else in C:
The if statement alone tells us that if a condition is true, it will execute a block of statements,
and if the condition is false, it won’t execute the block. But what if we want to perform a
different action when the condition is false? This is where the else statement comes in. We can
use the else statement with the if statement to execute a block of code when the condition is
false. The if-else statement consists of two blocks: one for the true condition and one for the
false condition.
The block of code following the else statement is executed when the condition in the if
statement is false.
if (condition) {
// code executed when the condition is true
}
else {
// code executed when the condition is false
}
3. Nested if-else in C:
A nested if in C is an if statement that is part of another if statement. Nested if statements
mean placing an if statement inside another if statement. Yes, C allows us to nest if
statements within other if statements, i.e., we can place an if statement inside another if
statement.
if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}}
else {
if (condition_3)
{
// statement 3
}
else
{ // Statement 4
}}
5. Switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be used to execute
the conditional code based on the value of the variable specified in the switch statement. The
switch block consists of cases to be executed based on the value of the switch variable.
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Switch Statement:
The switch case statement is an alternative to the if else if ladder that can
be used to execute the conditional code based on the value of the variable specified in the
switch statement. The switch block consists of cases to be executed based on the value of the
switch variable.
Explanation:
The switch-case statement is a control structure in programming that provides
an alternative to the if-else-if ladder. While the if-else-if ladder involves checking multiple
conditions one by one, the switch-case statement simplifies this process by allowing you to
check the value of a single variable against several possible values. In a switch statement, you
specify the variable you want to evaluate, and then the code block consists of cases, where
each case corresponds to a potential value that the variable can hold. When the switch
statement is executed, it compares the variable's value with the value in each case. If a match
is found, the code block associated with that case is executed. This makes the switch-case
statement an efficient way to handle multiple conditional branches based on the same
variable, particularly when the number of conditions is large. For any value that doesn't match
any of the specified cases, a default case can be used to provide a fallback action.
Break Keyword:
The break keyword is used within the code block of each case to terminate the switch
statement prematurely. When the program encounters a break statement inside a case block,
it immediately exits the switch statement, preventing the execution of subsequent case
blocks. The break statement is crucial for avoiding the switch statement's "fall-through"
behavior.
#include <stdio.h>
int main() {
int num = 3;
switch (num) {
case 1:
printf("Value is 1\n");
break; // Exit the switch statement after executing this case block
case 2:
printf("Value is 2\n");
break; // Exit the switch statement after executing this case block
case 3:
printf("Value is 3\n");
break; // Exit the switch statement after executing this case block
default:
printf("Value is not 1, 2, or 3\n");
break; // Exit the switch statement after executing the default case block
}
return 0;
}
Default Keyword:
When none of the case constants match the evaluated expression, it operates as a catch-all
case. If no matching case exists and a "default" case exists, the code block associated with
the "default" case is run. It is often used to handle circumstances where none of the stated
situations apply to the provided input.
IF-ELSE: if the condition is true, then the block of code inside the IF statement is executed;
otherwise, the block of code inside the ELSE is executed.
IF-ELSE-IF: if the condition is true, then the block of code inside the IF statement is
executed. After that, the ELSE-IF block is checked.