Physics Key Book
Physics Key Book
4 Control structures
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.
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)
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)
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)
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)
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:
General Form:
If (condition)
{
Block of statements – 1
}
else
{
Block of statements - 2
}
Explanation:
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.
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:
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: