[go: up one dir, main page]

0% found this document useful (0 votes)
84 views20 pages

Lesson 02: Iteration/Looping in C Programming C Programming

1) The document discusses different types of loops in C programming - while, do-while, and for loops. Loops are used to repeat a block of code multiple times until a certain condition is met. 2) The key components of a loop are discussed - a counter, initialization of the counter, a condition to check the counter against, statements to execute each iteration, and incrementing/decrementing the counter. 3) Examples are provided of using each type of loop to print numbers in a range or calculate values like a multiplication table for a given number.

Uploaded by

Prashant Kumar
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)
84 views20 pages

Lesson 02: Iteration/Looping in C Programming C Programming

1) The document discusses different types of loops in C programming - while, do-while, and for loops. Loops are used to repeat a block of code multiple times until a certain condition is met. 2) The key components of a loop are discussed - a counter, initialization of the counter, a condition to check the counter against, statements to execute each iteration, and incrementing/decrementing the counter. 3) Examples are provided of using each type of loop to print numbers in a range or calculate values like a multiplication table for a given number.

Uploaded by

Prashant Kumar
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/ 20

Lesson 02

Iteration/Looping
in
C Programming

Pijushkanti Panigrahi
What is Looping ?

• The looping can be defined as repeating the same


process multiple times until a specific condition
satisfies. It is known as iteration also. There are
three types of loops used in the C language. In
this part of the tutorial, we are going to learn all
the aspects of C loops..

2
Why looping?

• The looping simplifies the complex problems into the


easy ones.
• It enables to alter the flow of the program so that
instead of writing the same code again and again, we
can execute the same code for a finite number of
times.
For example, if we need to print ‘UNIVERSITY OF
CALCUTTA’ 10-times then, instead of using the printf
statement 10 times, we can use printf once inside a
loop which runs up to 10 iterations.
3

What are the advantages of Looping?

1) It provides code reusability.


2) Using loops, we do not need to write the same
code again and again.
3) Using loops, we can traverse over the elements of
data structures (array or linked lists).

4
Types of C Loops

There are three types of loops in C


language those are given below:
while
do while
for

5
Essential components of a loop

• Counter
• Initialisation of the counter with initial value
• Condition to check with the optimum value
of the counter
• Statement(s) to be executed by iteration
• Increment/decrement

6
Flowchart for a loop

7
while loop in C

• The while loop in c is to be used in the scenario where


the block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is
also called a pre-tested loop.
• The syntax of while loop in c language is given below:
initialisation;
while(condition)
{
block of statements to be executed ;
increment;
}
8
Write a C-program to print 10 natural numbers

#include<stdio.h> Output
main() 1
2
{
3
int i=1; 4
while(i<=10) 5
6
{
7
printf("%d \n",i); 8
i=i+1; 9
10
}
}
9
Program to print table for the given number using while loop in C

#include<stdio.h> Output
main() Enter a number: 5
{ 5
int i=1,number,b; 10
15
printf("Enter a number: ");
20
scanf("%d",&number); 25
while(i<=10) 30
{ 35
b=number*i; 40
printf("%d \n", b); 45
50
i=i+1;
}
}
10
do-while loop in C

• The do-while loop continues until a given condition


satisfies. It is also called post tested loop. It is used
when it is necessary to execute the loop at least once
(mostly menu driven programs).
• The syntax of do-while loop in c language is given below:
do
{
code to be executed ;
}
while(condition); 11
Flowchart for do-while loop

12
6.i++;
7.}while(i<=10);
8.return 0;
9.}
Output
1 2 3 4 5 6 7 8 9 10

#include<stdio.h> Output
main() 1
{ 2
int i=1; 3
do 4
{ 5
printf("%d \n",i); 6
i=i+1; 7
} 8
while(i<=10); 9
} 10 13
Example: Program to add two integers

1 /* - - -- - - -*/
2 /* Program to prnt multipliction table of a number*/
3 #include <stdio.h>
4 Outline
5 main()
6 {
1. Variables declaration
7 int number, i,m; /* declaration */
2. Input
8 printf( "Enter the number\n" ); /* prompt */
9 scanf( "%d", &number); /* read a number */
10 i=1; /* read a number */
11 do{ /* Starting of loop */ 2.1 loop starts
12 m=number*i; /* calculation of m*/
2.2 print values
13 printf(“%d, ”, m); /*printing m */
14 }while(i<=10); /* condition testing*/
3. Condition testing
15
4. end
16
17 }

Enter first integer


5
5, 10, 15, 20, 25,30,35,40, 45,50, Program Output

14
for loop in C

• The for loop in C language is used to iterate the


statements or a part of the program several times. It
is frequently used to traverse the data structures like
the array and linked list.
• The syntax of for loop in c language is given below:

for(Expression1; Expression2; Expression3)


{
codes to be executed;
}

15
Expression 1 (Optional)
• Represents the initialization of the loop variable.
• More than one variable can be initialised.

Expression 2
• Expression 2 is a conditional expression. It checks for a
specific condition to be satisfied. If it is not, the loop is
terminated.
• Expression 2 can have more than one condition. However,
the loop will iterate until the last condition becomes false.
Other conditions will be treated as statements.

Expression 3
• Expression 3 is increment or decrement to update the valu of
the loop variable
16
Flowchart : for loop

17
Program to print natural numbers 1to15

#include <stdio.h>
main()
{
int i;
for(i=1;i<=15;i=i+1)
{
printf("%d, ", i);
}
}

output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
18
Example: Program to add two integers
1 /* - - -- - - -*/
2 /* Addition program */
3 #include <stdio.h>
4 Outline
5 main()
6 {
7 int integer1, integer2, sum; /* declaration */ 1. Variables
8 declaration
9 printf( "Enter first integer\n" ); /* prompt */ 2. Input
10 scanf( "%d", &integer1 ); /* read an integer */
11 printf( "Enter second integer\n" ); /* prompt */
12 scanf( "%d", &integer2 ); /* read an integer */
13 sum = integer1 + integer2; /* assignment of sum */ 2.1 Sum
14 printf( "Sum is %d\n", sum ); /* print sum */
3. Print
15
16
17 }

Enter first integer


23
Program Output
Enter second integer
24
Sum is 47

19
Practice such simple programs
• Write a C program to calculate the perimeter and area of a land
• Write a C program to swap the value of to variables with the help of
a third variable and without the help of a third variable.
• Write a C program to find the large number among two
• Write a C program to find the large number among three
• Write a C program to print even and odd numbers from 1-50 using
while, for and do-while
• Write a C program to find the largest number among ten
• Write a C program to print the series 1 1 2 3 5 8 13 21 upto 150
• Write a C program to reverse a 3-digit integer
• Write a C program to find the prime number
• Write a C program to compile the mark-sheet of BLIS course
• Write a C program to prepare a bill for 10 books after 20% discount
for your library

20

You might also like