You are on page 1of 4

ashwini

Department of Computer Engineering

Academic Year: 2018-19 Semester: II


Class / Branch: FE
Subject: Structured Programming Approach
Instructor: Shweta Mahajan

Experiment 8

1. Aim: Write programs that demonstrate working of for loop.

2. Tools used:

gedit, gcc, OS-Ubuntu 14.04.3 LTS

3. Theory :

Loop Control Statements:

Loop control statements in C are used to perform looping operations until the given condition is
true. Control comes out of the loop statements once condition becomes false.

Types of loop control statements in C:


There are 3 types of loop control statements in C language. They are,
1. for
2. while
3. do while

Prof.Shweta Mahajan
1. For Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.

Syntax:

for ( initialisation; test_expression; update_expression )


{
statement(s);
}

Here is the flow of control in a 'for' loop −


 The initialisation step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as long
as a semicolon appears.

 Next, the test_expression is evaluated. If it is true, the body of the loop is executed. If it
is false, the body of the loop is not executed and the flow of control jumps to the next
statement just after the 'for' loop.

 After the execution of body of the 'for' loop, the flow of control jumps back up to the
update_expression statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
test_expression.

 The test_expression is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
test_expression becomes false, the 'for' loop terminates.
Example:

Write a program to display numbers from 1 to 10 using for loop.

Prof.Shweta Mahajan
Output:

Program:

Write a Program to calculate the sum of first n natural numbers(Positive integers 1,2,3...n
are known as natural numbers) using for loop.

Prof.Shweta Mahajan
Output:

 Conclusion : Hence we have successfully studied for loop statements.

5. Exercise: Write a program to calculate factorial integer number using for loop.

Prof.Shweta Mahajan

You might also like