Comp Xii Chapter 8 Sir Sheikh Emad
Comp Xii Chapter 8 Sir Sheikh Emad
8
Operators & Expressions
OBJECTIVE:
When you have completed this chapter, you will be
able to know about the
Short Answer Questions
Q1. What is loop? How many types of loops are there in C?
Q2. Define For Loop.
Q3. Define while loop.
Q4. Define Do- while loop.
Q5. Define Nested Loop.
Q1. W h a t i s l o o p ? H o w m a n y t y p e s o f l o o p s a r e t h e r e i n C ? ?
Loops in programming are used to repeat a block of code until the specified condition is
met. A loop statement allows programmers to execute a statement or group of statements
multiple times without repetition of code.
There are mainly two types of loops in C Programming:
Entry Controlled loops: In Entry controlled loops the test condition is checked before
entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of
the loop body. The loop body will execute at least once, irrespective of whether the
condition is true or false. Do-while Loop is Exit Controlled loop.
Loop
Type Description
first Initializes, then condition check, then executes the body and at last,
for loop
the update is done.
first Initializes, then condition checks, and then executes the body, and
while loop
updating can be inside the body.
do-while
do-while first executes the body and then the condition check is done.
loop
for Loop:-
for loop in C programming is a repetition control structure that allows programmers
to write a loop that will be executed a specific number of times. for loop enables
programmers to perform n number of steps together in a single line.
Syntax:
initialization expression;
while (test expression)
{
// body of the while loop
update expression;
}
Q5. Define Do- while loop.
do-while Loop:
The do-while loop is similar to a while loop but the only difference lies in the do-while loop
test condition which is tested at the end of the body. In the do-while loop, the loop body
will execute at least once irrespective of the test condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
}
while (test_expression);
NESTED LOOP:
A nested loop means an inside another loop statement. That is why nested
loops are also called “loop inside loops“. We can define any number of loops inside
another loop.
12345
WHILE LOOP: The while loop in C allows a block of code to be executed repeatedly as long
as a given condition remains true. It is often used when we want to repeat a block of code
till some condition is satisfied.
Syntax of while Loop
The while loop syntax is as follows:
while (condition) {
// Body
OUTPUT
updation
}
GfG
Example:
GfG
int main()
GfG
{
GfG
int i = 1;
GfG
// Condition for the loop
while (i <= 5) {
printf("GfG\n");
i++;
}
return 0;
}
Initialization: In this step, we initialize the loop variable to some initial value. Initialization
is not part of while loop syntax but it is essential when we are using some variable in the
test expression
Conditional: This is one of the most crucial steps as it decides whether the block in the
while loop code will execute. The while loop body will be executed if and only the test
condition defined in the conditional statement is true.
Body: It is the actual set of statements that will be executed till the specified condition is
true.
Updation: It is an expression that updates the value of the loop variable in each iteration.
It is also not part of the syntax but we have to define it explicitly in the body of the loop.
STEP 1: When the program first comes to the loop, the test condition will be evaluated.
STEP 2(i): If the test condition is false, the body of the loop will be skipped program will
continue.
STEP 2(ii): If the expression evaluates to true, the body of the loop will be executed.
STEP 3: After executing the body, the program control will go to STEP 1. This process will
continue till the test expression is true.
return 0;
}
Explanation: The do…while loop in this C program prints “Geeks” three times by executing
the loop body at least once and continuing until the condition i < 3 becomes false.
Syntax of do while Loop
do {
// Body of the loop
// Update expression
} while (condition);
How does the do…while Loop works?
The working of the do…while loop is explained below:
1. When the program control first comes to the do…while loop, the body of the loop is
executed first and then the test condition/expression is checked, unlike other loops
where the test condition is checked first. Due to this property, the do…while loop is
also called exit controlled or post-tested loop.
2. When the test condition is evaluated as true, the program control goes to the start of
the loop and the body is executed once more.
3. The above process repeats till the test condition is true.
4. When the test condition is evaluated as false, the program controls move on to the
next statements after the do…while loop.
As with the while loop in C, initialization and updation is not a part of the do…while loop
syntax. We have to do that explicitly before and in the loop respectively.
int main() {
OUTPUT
12345
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25