0% found this document useful (0 votes)
6 views8 pages

Comp Xii Chapter 8 Sir Sheikh Emad

Chapter Eight covers operators and expressions in C, focusing on loops. It explains different types of loops including for, while, do-while, and nested loops, detailing their syntax and functionality. The chapter also includes short and long answer questions to reinforce understanding of loop concepts.

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)
6 views8 pages

Comp Xii Chapter 8 Sir Sheikh Emad

Chapter Eight covers operators and expressions in C, focusing on loops. It explains different types of loops including for, while, do-while, and nested loops, detailing their syntax and functionality. The chapter also includes short and long answer questions to reinforce understanding of loop concepts.

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/ 8

CHAPTER EIGHT

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.

Long Answer Questions

Q1. Explain all types of Loops used in C.

PREPARED BY: SIR SHEIKH EMAD


CHAPTER # 8
Operators & Expressions

===========Short Questions and Answers=========

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

Q2. Define For 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.

PREPARED BY: SIR SHEIKH EMAD


Syntax:
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
Q3. Define While Loop.
While Loop
While loop does not depend upon the number of iterations. In for loop the
number of iterations was previously known to us but in the While loop, the
execution is terminated on the basis of the test condition. If the test condition will
become false then it will break from the while loop else body will be executed.

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

Q5. Define Nested loop.

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.

PREPARED BY: SIR SHEIKH EMAD


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

Q5. Explain all the types of loops used in C.

FOR LOOP: In C programming, the for loop is used to


repeatedly execute a block of code as many times as Hi
instructed. It uses a variable (loop variable) whose Hi
value is used to decide the number of repetitions. It is Hi
generally used when we know how many times we want Hi
to repeat the code. Hi
Let’s take a look at an example Hi
#include <stdio.h> Hi
int main() { Hi
Hi
// for loop to print "Hi" 5 times Hi
for (int i = 5; i < 10; i++) {
printf("Hi\n");
}
return 0;
}
Explanation: In this example, the loop prints the text “Hi” 5 times. The loop variable i is
initialized to 1. The condition i <= 5 is checked before each iteration. After each iteration, i
is incremented by 1. So this loop was executed 5 times.
Syntax of for Loop
for (initialization; condition; updation) {
// Body of the loop
}
where,
 Initialization: This step is executed first, and it is typically used to initialize a loop
control variable.
 Condition: This expression is checked before each iteration. If the condition is true,
the loop body executes. If it’s false, the loop terminates.
 Updation: This step is executed after each iteration, often used to update the loop
control variable.
 Body: The body of the loop are the set of statements that are executed when the
condition is true.
How for Loop Works?
The working of for loop is mentioned below:
Step 1: Initialization is the basic step of for loop this step occurs only once during the start
of the loop. During Initialization, variables are declared, or already existing variables are
assigned some value.
Step 2: During the Second Step condition statements are checked and only if the condition
is the satisfied loop we can further process otherwise loop is broken.
Step 3: All the statements inside the loop are executed.
Step 4: Updating the values of variables has been done as defined in the loop.
Continue to Step 2 till the loop breaks.
PREPARED BY: SIR SHEIKH EMAD
FLOWCHART of the FOR Loop Examples of for Loop
The below examples demonstrate
the use of for loop in different
cases:

Print First N Natural Numbers


#include <stdio.h>
int main()
{
int n = 5,i;

// Initialization of loop variable


for (i = 1; i <= n; i++)
printf("%d ", i);
return 0;
}
OUTPUT

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

PREPARED BY: SIR SHEIKH EMAD


where,

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.

Working of while Loop


We can understand the working of the while loop by looking at the above flowchart:

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.

PREPARED BY: SIR SHEIKH EMAD


DO-WHILE LOOP: The do…while loop is a type of loop in C that executes a block of code
until the given condition is satisfied. The feature of do while loops is that unlike the while
loop, which checks the condition before executing the loop, the do…while loop ensures that
the code inside the loop is executed at least once, even if the condition is false from the
start.
Example: OUTPUT
int main() {
// Loop variable declaration and initialization
int i = 0;
Geeks
// do while loop Geeks
do { Geeks
printf("Geeks\n");
i++;
} while (i < 3);

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.

PREPARED BY: SIR SHEIKH EMAD


Nested for Loop:
Nested for loop refers to any type of loop that is defined inside a ‘for’ loop. Below is the
equivalent flow diagram for nested ‘for’ loops:
Syntax:

for ( initialization; condition; increment ) {

for ( initialization; condition; increment ) {

// statement of inside loop


}

// statement of outer loop


}
Nested for loop in C:
1. The concept of the nested for loop in C programming language provides a versatile
approach to achieve complex iterations and effectively execute repetitive tasks.
2. In C, nested for loops make it easy to handle nested iterations, making jobs like
browsing multidimensional arrays simpler.
3. This hierarchy enhances the readability, maintainability, and organization of the
code.
4. To tackle complex programming problems and investigate new avenues in software
development, mastery is necessary.

int main() {

// Outer for loop to print a multiplication


// table for all numbers upto 5
for (int i = 1; i <= 5; i++) {

// Inner loop to print each value in


table
for (int j = 1; j <= 5; j++) {
printf("%d ", i * j);
}
printf("\n");
} return 0;
}

OUTPUT
12345
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

PREPARED BY: SIR SHEIKH EMAD

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy