0% found this document useful (0 votes)
22 views19 pages

3 Loops

This document discusses different types of loops in programming languages including while, for, and do-while loops. It provides examples of using each loop type to iterate through operations like printing values. It also covers concepts like break and continue statements, infinite loops, nested loops, and examples of problems that can be solved using loops like calculating sums, factorials, and printing patterns like stars.
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)
22 views19 pages

3 Loops

This document discusses different types of loops in programming languages including while, for, and do-while loops. It provides examples of using each loop type to iterate through operations like printing values. It also covers concepts like break and continue statements, infinite loops, nested loops, and examples of problems that can be solved using loops like calculating sums, factorials, and printing patterns like stars.
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/ 19

Programming Language

Loops / Repetition Statements


Loops / Repetition Statements
• Repetition statements allow us to execute a statement multiple
times
• Often they are referred to as loops
• C has three kinds of repetition statements:
▫ the while loop
▫ the for loop
▫ the do while loop
• The programmer should choose the right kind of loop for the
situation
while Loop
• A while statement has the following syntax:
while ( condition ) while ( condition ){
statement; statement1;
statement2;

}
• If the condition is true, the statement or a block of statements is
executed
• Then the condition is evaluated again, and if it is still true, the
statement/block is executed again
• The statement/block is executed repeatedly until the condition
becomes false
Example
• Print “The sky is the limit!” n times. n will be user input
scanf(“%d”,&n);
int count = 1;
while (count <= n) {
printf (“The sky is the limit”);
count++;
}
• If the condition of a while loop is false initially, the statement is
never executed
• Therefore, the body of a while loop will execute zero or more times
Example
• Print first n natural numbers.
▫ Upwards
▫ Downwards
• Print odd numbers up to n.
• Print even numbers up to n.
• Print summation of first n numbers.
• Print summation of all odd numbers up to n.
• Print summation of all even numbers up to n.
• Print factorial of n
• Print xn , where x and n are integers.
for Loop
• A for statement has the following syntax:
The statement is executed until the
The initialization is executed once
condition becomes false
before the loop begins

for ( initialization ; condition ; increment )


statement;

The increment portion is executed at


the end of each iteration
for Loop
• An example of a for loop:
for(count=1; count <= 5; count++)
printf (“%d\n”, count);
for(count=5; count >= 1; count--)
printf (“%d\n”, count);
• The initialization section can be used to declare a variable.
• Like a while loop, the condition of a for loop is tested prior to
executing the loop body
• Therefore, the body of a for loop will execute zero or more times
for Loop
• The increment section can perform any calculation

int num;
for(num=100; num > 0; num -= 5)
printf (“%d\n”, num);

• A for loop is well suited for executing statements a specific


number of times that can be calculated or determined in advance
The break and continue Statement
• Sometimes we need:
▫ to skip some statements inside the loop (continue)
▫ or terminate the loop immediately without checking the test
condition (break).
• In such cases, break and continue statements are used.
The break Statement
• The break statement terminates the loop immediately when it is
encountered
while(testExpression){ do{
// codes // codes
if(condition to break){ if(condition to break){
break; break;
} }
// codes // codes
} }while(testExpression);

for(init; testExpression; update){


// codes
if(condition to break){
break;
}
// codes
}
The continue Statement
• The continue statement skips statements after it inside the loop.

while(testExpression){ do{
// codes // codes
if(condition to break){ if(condition to break){
continue; continue;
} }
// codes // codes
} }while(testExpression);

for(init; testExpression; update){


// codes
if(condition to break){
continue;
}
// codes
}
Some example problems
• Write down a program to find the summation of the following series:
1  2  3  4  ......  up to n
12  22  32  42  ......up to n2
• Find factorial of n, where n will be input
• Find xn, where x and n both will be input, assume n is integer
• Show all factors of a number n
• Show smallest factor of a number n (other than 1)
• Show largest factor of a number n (other than itself)
• Prime number testing
• Perfect number testing
• GCD of two numbers
• Fibonacci series
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (1 == 1){
printf (“%d\n”, count);
count = count - 1;
}
for( ; ; ){
printf (“%d\n”, count);
count = count - 1;
}
• This loop will continue executing until interrupted (Ctrl-C) or
until an underflow error occurs
do while Loop
• A do statement has the following syntax:
do {
statement1;
statement2;

}while ( condition );
• The statement is executed once initially, and then the condition is
evaluated
• The statement is executed repeatedly until the condition becomes
false
do while Loop
• An example of a do while loop:
int count = 1;
do{
printf(“%d\n”, count);
count++;
} while (count <= 5);

• The body of a do while loop is executed at least once


Example: Printing reverse of a
number
• Write down a program that prints the digits of a number in
reverse.
int a, n, reverse = 0;
• For example: scanf(“%d”,&n);
• input: 6457 do{
a = n%10;
• output: 7546
n = n/10;
reverse = reverse*10 + a;
} while (n != 0);
Nested Loop
• Similar to nested if statements, loops can be nested as well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates
completely

for(i=1; i <= 3; i++){


for(j=1; j <= 2; j++){
printf(“Sky is the limit\n”);
}
printf(“The world is becoming smaller\n”);
}
Some more Examples
• Write a program that prints all prime numbers up to x. The integer
x will be input to your program.
• Write down a program that will take an integer x as input and will
count and print the number of prime numbers up to x.
• Write a program that prints all prime factors of a number x given
as input.
• Write down a program that will take an integer n as input and will
count and print the number of fibonacci numbers.
Example: Star
• Write a program that prints the following. The total number of
lines will be input to your program.
*
**
***
****
*****
******
*******
********
*********
**********

You might also like