0% found this document useful (0 votes)
3 views

5 Loop

Uploaded by

lhumayra204
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

5 Loop

Uploaded by

lhumayra204
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Programming in C

Control statement: Looping/Iteration

Ashis Talukder, PhD


Associate Professor
Department of MIS, DU
Control Statement: Loop/iteration
#include <stdio.h> • The looping can be defined as repeating the
main ( ) same process multiple times until a specific
{ condition satisfies.
int i, n; / * declarations * I • There are three types of loops used in the C
language.
scanf ( " % d " , &n) ; /* input size*/ • Why use loops in C language?
• The looping simplifies the complex problems into
the easy ones.
for(i = 1; i < n; i++) • It enables us to alter the flow of the program so
that instead of writing the same code again and
printf("%d", i) ; / * numerical output */ again, we can repeat the same code for a finite
} number of times.
• For example, if we need to print the first 10
natural numbers then, instead of using the
printf statement 10 times, we can print inside
a loop which runs up to 10 iterations.
Dr. Ashis Talukder, Associate Professor, MIS, DU 2
Control Statement: loop/iteration
#include <stdio.h>
main ( ) • Advantage of loops in C
{ • It provides code reusability.
int i,n; / * declarations * I • Using loops, we do not need to write the
same code again and again.
scanf ( " % f " , &n) ; /* input size*/ • Using loops, we can traverse over the
elements of data structures (array or
for(i = 1; i < n; i++) linked lists).
printf("%d", i) ; / * numerical output */
}

Dr. Ashis Talukder, Associate Professor, MIS, DU 3


Parts of a loop/iteration

• There are FOUR of a loop:


• Loop Initialization
• Loop Condition
• Loop body
• Increment/Decrement

Dr. Ashis Talukder, Associate Professor, MIS, DU 4


sum = 13 + 23 +…+ n3
Parts of a loop
i = 1; Initialization

• There are FOUR of a loop: sum = 0;


• Loop Initialization while(i <= n ) Condition

• Loop Condition {
term = i*i*i;
• Loop body
sum = sum + term;
• Increment/Decrement Loop body

i++; Increment

Dr. Ashis Talukder, Associate Professor, MIS, DU 5


Control Structure: loop/iteration

• Types of C Loops: There are three types of loops in C


language that is given below:
• while
• do while
• for

Dr. Ashis Talukder, Associate Professor, MIS, DU 6


𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
• The while loop in c is to be used in the scenario
where we don't know the number of iterations in
advance.
• 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:

Dr. Ashis Talukder, Associate Professor, MIS, DU 7


𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
• Parts of while loop
• Unlike for loop, the while loop does not
sum = 13 + 23 +…+ n3

contain initialization and update part. It i = 1; Initialization


contains only two parts - condition and body sum = 0;
of loop. while(i <= n ) Condition
• The condition is a boolean expression {

evaluating an integer value. It is similar to term = i*i*i;

if-else condition and define loop sum = sum + term;

termination condition. Loop body


i++;
• Body of loop contains single or set of }
Increment

statements. It define statements to repeat.


Dr. Ashis Talukder, Associate Professor, MIS, DU 8
𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
• Parts of while loop
• At this point, you might be thinking about loop
counter variable-initialization and variable-update
part.
• Where to put these?
• You are free to initialize loop counter variables anywhere in
the program before its use.
• However, best practice is to initialize all important loop
variable just before the loop.
• Likewise, you can keep your loop update part just before the
end of loop.

Dr. Ashis Talukder, Associate Professor, MIS, DU 9


𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
• Parts of while loop variable initialization;
• Initialization while( condition )
• Condition {
• Loop Body statements 1;
• Increment/decrement
statements 2;
statements n;
• Unlike for loop, the while loop does
variable increment or decrement;
not contain initialization and update part
in the while statement. Rather, the }
initialization is placed before the loop.
Dr. Ashis Talukder, Associate Professor, MIS, DU 10
𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
• The condition is placed in the while variable initialization;
statement.
while( condition )
• The condition is a boolean expression
evaluating an integer value. It is similar to {
if-else condition and define loop statements 1;
termination condition. statements 2;
• Body of loop contains single or set of statements n;
statements. It define statements to variable increment or decrement;
repeat.
}
• The incr/decr statement updates the
loop variable.
Dr. Ashis Talukder, Associate Professor, MIS, DU 11
𝒅𝒅𝒅𝒅 − 𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
• 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:

Dr. Ashis Talukder, Associate Professor, MIS, DU 12


sum = 13 + 23 +…+ n3
𝒅𝒅𝒅𝒅 − 𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 loop
i = 1; Initialization

sum = 13 + 23 +…+ n3
sum = 0;
do {
i = 1; Initialization

sum = 0;
term = i*i*i;
while(i <= n ) Condition sum = sum + term;
{ Loop body
term = i*i*i;
sum = sum + term; i++; Increment
Loop body
i++;
Increment
} }while(i <= n ); Condition

Dr. Ashis Talukder, Associate Professor, MIS, DU 13


sum = 13 + 23 +…+ n3
𝒇𝒇𝒇𝒇𝒇𝒇 loop
sum = 0; Initialization

Initialization Condition Increment


for( i = 1 ; i < n ; i++ )
{
term = i*i*i;
sum = sum + term;
Loop body

Dr. Ashis Talukder, Associate Professor, MIS, DU 14


Example of while: GCD
GCD (a,b)
{
input a,b;
while (a > 0) {
r = b % a;
b = a;
a = r;
}
}
Dr. Ashis Talukder, Associate Professor, MIS, DU 15
Nested loop
• C supports nesting of loops in C: Loops inside another loopLoops inside
another loop

• Nesting of loops is the feature in C that allows the looping of statements


inside another loop.
• Any number of loops can be defined inside another loop, i.e., there is no
restriction for defining any number of loops.
• The nesting level can be defined at n times.
• You can define any type of loop inside another loop; for example, you can
define 'while' loop inside a 'for' loop.
Dr. Ashis Talukder, Associate Professor, MIS, DU 16
Nested loop #include <stdio.h>
int main()
{
int n,i,j;
printf("Enter the value of n :");
for(i=1;i<=n;i++) // outer loop
{
for(j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j));
}
printf("\n");
}
} Dr. Ashis Talukder, Associate Professor, MIS, DU 17
Infinite loop
• Loop that does not terminate
• Loop condition is always true
• Usually, this is an error. For example, you might have a loop that
decrements until it reaches 0.

Dr. Ashis Talukder, Associate Professor, MIS, DU 18


Infinite loop

Dr. Ashis Talukder, Associate Professor, MIS, DU 19


Infinite loop: When used
• An infinite loop is useful for those applications that accept the user input and
generate the output continuously until the user exits from the application
manually.
• Examples:
• All the operating systems run in an infinite loop as it does not exist after performing
some task.
• It comes out of an infinite loop only when the user manually shuts down the system.
• All the servers run in an infinite loop as the server responds to all the client requests. It
comes out of an indefinite loop only when the administrator shuts down the server
manually.
• All the games also run in an infinite loop. The game will accept the user requests until
the user exits from the game.

Dr. Ashis Talukder, Associate Professor, MIS, DU 20


Programs for loop
1. Find GCD of two numbers. 7. Calculate factorial of a number.
2. Find a number prime or not. 8. All Prime numbers between 1 to
n.
3. Series:
9. Fibonacci series up to n terms.
a) Sum = 1 + 2 + 3 + … + n
b) Sum = 12 + 22 + 32 + … + n2 10.Pascal triangle upto n rows.
11.Find coefficients in the
expansion of 𝑥𝑥 + 𝑦𝑦 𝑛𝑛
4. Reverse a number.
12.Print the given star patterns.
5. Count number of digits in a 13.Print the given number patterns.
number.
14. Find Armstrong number in a
6. A number is palindrome or not. Range

Dr. Ashis Talukder, Associate Professor, MIS, DU 21


Programs for loop
14. Find Armstrong number in a Range

Dr. Ashis Talukder, Associate Professor, MIS, DU 22


Programs for loop
15. Find the sequence and total number of numbers from
Ackermann Functions:

Dr. Ashis Talukder, Associate Professor, MIS, DU 23


Pascal’s Triangle
1
1 1
1 2 1
1 3 3 1

1
1 1
1 2 1
1 3 3 1

Dr. Ashis Talukder, Associate Professor, MIS, DU 24


Thank You

Dr. Ashis Talukder, Associate Professor, MIS, DU 25

You might also like