Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

COMSATS University Islamabad, Abbottabad Campus

Department of Computer Science


LAB MIDTERM EXAMINATION – FALL 2022
Class: BCS-1B Date: 08-11-2022
Subject: Introduction to ICT Instructor: Faiza Qazi
Total Time Allowed: 1 hours 30 minutes Max Marks: 50
Name: Fahad Hassan
Registration #: FA22-BCS-078

Contents
C Loops: For, While, Do While, Looping Statements with Syntax & Example .............................................. 3
1. What is Loop in C?............................................................................................................................. 3
➢ Types of Loops in C........................................................................................................................ 3
2. While Loop in C ................................................................................................................................. 4
Syntax of While Loop in C: ................................................................................................................ 4
3. C for loop:.......................................................................................................................................... 5
Syntax of for loop: ................................................................................................................................. 5
Flow Diagram of for loop ................................................................................................................... 6
TASK 4: CREATE A TABLE IN MS WORD......................................................................................................... 6
Figure 1: Sample Loop ........................................................................................................................4
Figure 2: while loop flow chart ...........................................................................................................5
Figure 3: Flow diagram for loop ..........................................................................................................6
C Loops: For, While, Do While, Looping Statements with Syntax & Example

1. What is Loop in C?

Looping Statements in C execute the sequence of statements many times until the stated
condition becomes false. A loop in C consists of two parts, a body of a loop and a control
statement. The control statement is a combination of some conditions that direct the body of the
loop to execute until the specified condition becomes false. The purpose of the C loop is to
repeat the same code a number of times.

In this tutorial, you will learn about

• What is Loop in C?
• Types of Loops in C
• While Loop in C
• Do-While loop in C
• For loop in C
• Break Statement in C
• Continue Statement in C
• Which loop to Select?

➢ Types of Loops in C

Depending upon the position of a control statement in a program, looping statement in C is


classified into two types

1. Entry controlled loop


2. Exit controlled loop

In an entry control loop in C, a condition is checked before executing the body of a loop. It is
also called as a pre-checking loop.

In an exit controlled loop, a condition is checked after executing the body of a loop. It is also
called as a post-checking loop.
Figure 1: Sample Loop

The control conditions must be well defined and specified otherwise the loop will execute an
infinite number of times. The loop that does not stop executing and processes the statements
number of times is called as an infinite loop. An infinite loop is also called as an "Endless
loop." Following are some characteristics of an infinite loop:

1. No termination condition is specified.


2. The specified conditions never meet.

The specified condition determines whether to execute the loop body or not.

'C' programming language provides us with three types of loop constructs:

1. The while loop


2. The do-while loop
3. The for loop

2. While Loop in C

A while loop is the most straightforward looping structure. While loop syntax in C programming
language is as follows:

Syntax of While Loop in C:

while (condition) {
statements;
}
Flow chart for while loop

Figure 2: while loop flow chart

3. C for loop:

This is one of the most frequently used loop in C programming

Syntax of for loop:

for (initialization; condition test; increment or decrement)


{
//Statements to be executed repeatedly
}
Flow Diagram of for loop

Figure 3: Flow diagram for loop

TASK 4: CREATE A TABLE IN MS WORD

A1 A2 A3 C1 D1

B1

A4 A5 A6 D2 D3

A7 A8 A9 B2 C2

D4 E1

A10 C3

F1

TASK 5: INSERTING AN EQUATION IN MS WORD

𝑥⁄ + 5√𝑥& + ∫𝑛 ∑𝑚 𝑥 2
𝑦 𝑥−1 𝑦=1 𝑦

You might also like