0% found this document useful (0 votes)
7 views10 pages

Xii Chapter 7 by Sir Sheikh Emad

Chapter 7 discusses selection control statements in programming, focusing on control structures like if, if-else, nested if, else if, and switch statements. It explains their definitions, structures, and use cases, highlighting the importance of decision-making in programming. The chapter also includes examples and comparisons between if-else and else-if statements.

Uploaded by

fatime.brr99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Xii Chapter 7 by Sir Sheikh Emad

Chapter 7 discusses selection control statements in programming, focusing on control structures like if, if-else, nested if, else if, and switch statements. It explains their definitions, structures, and use cases, highlighting the importance of decision-making in programming. The chapter also includes examples and comparisons between if-else and else-if statements.

Uploaded by

fatime.brr99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CHAPTER

7
SELECTION CONTROL STATEMENTS

OBJECTIVE:
When you have completed this chapter, you will be able to
know about the

Short Answer Questions


Q1. Define Control Structure.
Q2. Define if and if else statement.
Q3. Define Nested if statement.
Q4. Define Else if Statement.

Long Answer Questions


Q1. Explain Conditional Statements.

Q2. Explain the Switch Statement, provide its structure, and also an example of code in C
language.

Q3. Write the difference between if else and else if statement.

PREPARED BY: SIR SHEIKH EMAD


CHAPTER # 7
SELECTION CONTROL STATEMENTS
=============Short Questions and Answers============

Q1. Define Control Structure.

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.

Types of control structures


1. Sequence
2. Selection
3. Repetition
1). Sequence: Statements are executed in a specified order. No statement is skipped and no
statement is executed more than once.
Flowchart:

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.

PREPARED BY: SIR SHEIKH EMAD


3). Repetition:
In this structure the statements are executed more than one time. It is also known as iteration or loop
e.g while loop, for loop do-while loops etc.

Flow chart

Q2. Define if and if else statement.


C if Statement:
The if statement in C is used to execute a block of code based on a specified condition.

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.

PREPARED BY: SIR SHEIKH EMAD


if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}

PREPARED BY: SIR SHEIKH EMAD


Q4. Define Else 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.

=============LONG Questions and Answers============

Q4. Explain Conditional Statements.

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.

Need of Conditional Statements


There are situations in real life when we need to make decisions, and based on these
decisions, we decide what to do next. Similar situations arise in programming as well, where
we need to make decisions and, based on these decisions, execute the next block of code. For
example, in C, if x occurs, then execute y; otherwise, execute z. There can also be multiple
conditions, such as in C: if x occurs, execute p; else if condition y occurs, execute q;
otherwise, execute r. The else-if condition in C is one of the many ways to handle multiple
conditions. To learn how to use these control structures alongside data structures in C, the C
Programming Course Online with Data Structures provides detailed lessons on program
control and logic.
Types of Conditional Statements in C:
Conditional statements in C: The following are the decision-making statements available in C.

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
}}

PREPARED BY: SIR SHEIKH EMAD


4. if-else-if Ladder in C
The if else if statements are used when the user has to decide among multiple options. The C
if statements are executed from the top down. As soon as one of the conditions controlling the
if is true, the statement associated with that if is executed, and the rest of the C else-if ladder
is bypassed. If none of the conditions is true, then the final else statement will be executed. if-
else-if ladder is similar to the switch statement.
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

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;
}

PREPARED BY: SIR SHEIKH EMAD


Q2. Explain the Switch Statement, provide its structure, and also an example of code
in C language.

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.

PREPARED BY: SIR SHEIKH EMAD


Rules for switch statement in C language:
1. The switch expression must be of an integer or character type.
2. The case value must be an integer or character constant.
3. The case value can be used only inside the switch statement.
4. The break statement in switch case is not must. It is optional. If there is no break statement found in
the case, all the cases will be executed present after the matched case. It is known as fall through the
state of C switch statement.

Break and Default Keywords in the switch Statement


Let us explain and define the break and default keywords in the context of the switch
statement.

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.

PREPARED BY: SIR SHEIKH EMAD


Q3. Write the difference between if else and else if statement.

Aspect if-else else-if


Number of Checks only two conditions: true Allows checking of multiple conditions in
Conditions or false. sequence.
Suitable for binary decisions (two
Use Case Suitable for checking multiple conditions.
outcomes).
Executes one of the two blocks: Checks multiple conditions sequentially
Flow Control
if true, or else false. and executes the first block that matches.
Composed of one if and one else Composed of one if, multiple else-if
Structure
block. blocks, and optionally an else block.
Simpler when only two possible More efficient when checking multiple
Efficiency
conditions need to be checked. possible conditions.
if (x > 0) { // positive } else { // if (x > 0) { // positive } else if (x == 0) {
Example
non-positive } // zero } else { // negative }

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.

An example of the code is included.

PREPARED BY: SIR SHEIKH EMAD

You might also like