0% found this document useful (0 votes)
8 views4 pages

Physics Key Book

Chapter 4 discusses control structures in C++, including control statements, loops, and decision-making structures. It explains the differences between if-else and else-if statements, as well as the distinctions between for and while loops. The chapter also covers the purpose of break and continue statements, the exit function, and nested loops, providing syntax and examples for various control structures.

Uploaded by

u39404517
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)
8 views4 pages

Physics Key Book

Chapter 4 discusses control structures in C++, including control statements, loops, and decision-making structures. It explains the differences between if-else and else-if statements, as well as the distinctions between for and while loops. The chapter also covers the purpose of break and continue statements, the exit function, and nested loops, providing syntax and examples for various control structures.

Uploaded by

u39404517
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/ 4

Chapter No.

4 Control structures

Q.2 Write short answers to the following questions.

i. Why control statements are used in C++ programs?

Ans. Control statements (structures) allow programmers to control the flow of program execution. Three types of
control statements (structures) are sequential, conditional and repetition structures.

ii. Differentiate between if-else and else-if statements.

Ans. if-else statement helps in making decision among two courses of actions whereas else-if allows us to make
decision among various alternatives See Q.3 (i)

iii. Differentiate between for and while loop.

Ans. for loop iterates a predetermine number of times and we can know in advance that how many times the loop
body will execute. Whereas a while loop does not run a predetermine number of times and we cannot evaluate
in advance that how many times the body of while loop will execute. See Q.3 (iii)

iv. What is the purpose of break and continue statements in C programs.

Ans. Break statement: Break statement has two usage:

1. It is used to terminate a case in switch statement. For example (write program 7)


2. It is used to terminate a loop. For example (write program no. 13)

Continue statement: Continue statement is used inside a loop. When it is encountered, it transfers control to the
beginning of the loop, skipping the remaining statements. For example (write program 14)

v. What is the purpose of exit function?

Ans. The exit function is used to terminate a C++ program before its normal termination and exit to the operating
system. It requires stdlib.h. Its general form is:
exit(value);
Here, value is an integer value known as exit code. Exit code is “1” indicates error and “0” indicates no error.
vi. What is nested loop? Give one example.

Ans. A loop inside another loop is known as nested loop. We can nest a for, while or do while loop inside another
for, while or do while loop. For example (write program 15)

Q.3 Write long answers of the following questions.

i. What is decision control structure? Explain all types of if statements with syntax and examples.

Ans. Decision Control Structure or Conditional Structure or Decision Making Structure: Decision control
structures are used to make decision in programming. They allow programs to execute a specific statement or a
set of statement based on one or more conditions. Following are the types of if statements used in C++.
1. IF STATEMENT: The if statement is used to execute a block of statements based on a condition.
General Form:
If (condition)
{
Block of statements
}
Explanation:

 The condition is evaluated first.


 If the condition is true, the block of statements within the braces are executed.
 If the condition is false, the block of statements is skipped and control is transferred to the next statement
after the closing brace.
 Braces are not required if there is a single statement.
Example: write program 2
2. IF-ELSE STATEMENT: The if-else statement allows making decision between two courses of action based on a
condition.

General Form:

If (condition)
{
Block of statements – 1
}
else
{
Block of statements - 2
}
Explanation:

 The condition is evaluated.


 If the condition is true, the block of statements-1 is executed, the second block of statements-2 is skipped
and then control is transferred to the next statement.
 If the condition is false, the block of statements-1 is skipped and the second block of statements- 2
following the keyword else is executed.
 Braces are not required if there is a single statement.

Example: Write program 3

3. ELSE-IF STATEMENT: The else-if statement is used in situation where a decision is to be made from several
alternatives based on various conditions.
General Form:
If (condition – 1)
{
Block of statements – 1
}
else if (condition – 2 )
{
Block of statements – 2
}
.
.
.
else
{
Block of statements – n
}
Explanation:
 The condition – 1 is evaluated.
 If it is true, the block of statements – 1 is executed and control is transferred to the next statement
following else-if.
 If the condition-1 is false then condition-2 is evaluated. If it is true then the block of statements- 2
following condition – 2 is executed.
 In this manner conditions are evaluated one by one. When any condition is true, the block of statement
following that condition is executed, rest of the code is skipped and control is transferred to the next
statement.
 If none of the condition is true then the last block of statement following the keyword else is executed.
The else block is optional.
 Braces are not required if there is a single statement.

Example: Write program 5


ii. Explain the purpose of switch statement with syntax and one example.
Ans. SWITCH STATEMENT: Switch statement is used when a single block of statements it to be selected among
many choices.
General Form:
switch (expression / value)
{
case constant – 1 : block of statements;
break;
case constant – 2 : block of statements;
break;
case constant – 3 : block of statements;
break;
.
.
.
default : block of statements;
break;
}
Explanation:
 The expression within the brackets is evaluated.
 The result of the expression or the value of variable is compared in sequence with constant values given
after the keyword case.
 If result matches any constant value then the block of statements following that case is executed and
control exits from the body of the switch statement and goes to the first statement following the end of
the switch statement.
 If none of the constant values after the case keyword match with the result of expression or the value of
variable then the block of statements following the keyword default is executed. Its use is optional.
 The keyword break is used to terminate a case in switch statement.
Example: Write program 7
iii. What is looping control structure? Explain all types of looping statements with syntax and examples.
Ans. Looping Control Structure or Iteration Structure or loop: Looping control structure refers to execution of
same set of instructions several times till a condition is met. There are three types of loops in C++.
1. FOR LOOP OR COUNTER LOOP: A for loop is used to execute one or more statements for a specific number of
times.
General Form:
for (initialization; condition; increment / decrement)
{
Block of statements
}
Explanation:

 A variable, known as loop counter or loop variable, is assigned an initial value in the initialization part of
the loop. For example a=1.
 The condition which is a relational expression such as a < 5 is evaluated. If it is true then the block of
statements within the braces is executed.
 After the execution of the statements, control is transferred to the increment / decrement part of the
loop that increment or decrement the loop variable.
 The condition is again evaluated. If it is true then the body of the loop is again executed. This process goes
on till the loop condition becomes false.
 When the loop condition becomes false, the loop terminates and control is transferred to the next
statement.
 Braces are not required if there is a single statement.
Example: Write program 10.

2 WHILE LOOP: A while loop is a sentinel loop statement. The body of the loop executes as long as the condition
remains true. The loop condition is checked at the beginning of the loop.
General Form:

while (condition)
{
Block of statements
}
Explanation:

 The condition which is a relational expression such as a < 5 is evaluated.


 If the condition is true, the block of statements within the braces is executed.
 The condition is again evaluated and if it is true the body of the loop is executed again.
 This process goes on till the condition becomes false.
 When the loop condition becomes false, the loop terminates and control is transferred to the next
statement.
 Braces are not required if there is a single statement.

Example: Write program 12

3. DO-WHILE LOOP: A do-while is very similar to the while loop, except that the loop condition is checked at the
end of the loop. Therefore, the body of the loop is executed at least once.

General Form:

do
{
Block of statements
}
while (condition);
Explanation:

 The block of statements following the keyword do is executed.


 The condition at the end of the loop is evaluated. If the condition is true, control is transferred back to the
beginning of the loop.
 The loop is executed once again and then the condition is also checked again. This process continues till
the condition becomes false.
 When the loop condition becomes false, the loop terminates and control is transferred to the next
statement.
 Braces are not required if there is a single statement.

Example: Write program 13

You might also like