Content
• Loops & Functions: Iteration and loops: use of while,
do while and for loops, multiple loop variables, use of
break and continue statements.
Introduction to Loops
• In programming, loops are used to repeat a
block of code until a specified condition is
met.
• C programming has three types of loops.
1. for loop
2. while loop
3. do...while loop
• There are mainly two types of loops:
• Entry Controlled loops: In this type of loop, the test
condition is tested before entering the loop body. For
Loop and While Loop is entry-controlled loops.
• Exit Controlled Loops: In this type of loop the test
condition is tested or evaluated at the end of the loop
body. Therefore, the loop body will execute at least
once, irrespective of whether the test condition is true
or false. the do-while loop is exit controlled loop.
For loops
The syntax of for loop in c language is given
below:
for(Expression 1; Expression 2; Expression 3)
{
//code to be executed
}
Flowchart of for loop in C
C for loop Examples
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}
C Program: Print table for the given number
using C for loop
C Program: Print table for the given number
using C for loop
#include<stdio.h>
int main(){
int i=1,number=0,y;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++){
y= (number*i);
printf("%d*%d=%d \n",number, i, y);
}
return 0;
}
Example
#include <stdio.h>
int main()
{
int i=1;
for(;i<5;i++)
{
printf("%d ",i);
}
}
Example
#include <stdio.h>
int main()
{
int i;
for(i=0;;i++)
{
printf("%d",i);
}
}
Example
#include<stdio.h>
void main ()
{
int i=0,j=2;
for(i = 0;i<5;i++,j=j+2)
{
printf("%d %d\n",i,j);
}
}
Loop body
#include<stdio.h>
void main ()
{
int i;
for(i=0;i<10;i++)
{
int i = 20;
printf("%d ",i);
} }
Factors of a Positive Integer
#include <stdio.h>
int main()
{
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; i++)
{
if (num % i == 0)
{
printf("%d ", i);
}
}
return 0;
}
Nested for
A nested loop means a loop statement inside another loop statement.
That is why nested loops are also called “loop inside loops“. We
can define any number of loops inside another loop.
1. Nested for Loop
Nested for loop refers to any type of loop that is defined inside a ‘for’
loop. Below is the equivalent flow diagram for nested ‘for’ loops:
Syntax:
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
•
Eg Display the pattern like right angle triangle using a
number
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
}
Eg.7
• Write a program in C to make such a pattern like right
angle triangle with a number which will repeat a
number in a row.
• The pattern like :
1
22
333
4444
Program
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
}
Eg. 8
• Write a program in C to make such a pattern
like right angle triangle with number increased
by 1. (Floyd’s Triangle)
• The pattern like :
• 1
• 23
• 456
• 7 8 9 10
#include <stdio.h>
void main()
{
int i,j,rows,k=1;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d ",k++);
printf("\n");
}
}
While Loop
• The syntax of the while loop is:
while (testExpression)
{
// statements inside the body of the loop
}
How While Loop Works?
• The while loop evaluates the
test expression inside the
parenthesis ().
• If the test expression is true,
statements inside the body
of while loop are executed.
Then, the test expression is
evaluated again.
• The process goes on until the
test expression is evaluated to
false.
• If the test expression is false,
the loop terminates (ends).
• It is also known as entry
controlled loops.
Example:
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
++i;
}
return 0;
}
Do-while Loop
• The do-while loop is similar to the while loop with one
important difference. The body of do-while loop is
executed at least once. Only then, the test expression
is evaluated. It is also known as exit controlled loop.
• The syntax of the do-while loop is:
do
{
// statements inside the body of the loop
}
while (testExpression);
How do-while loop works?
• The body of do...while loop is
executed once. Only then, the
test expression is evaluated.
• If the test expression is true,
the body of the loop is
executed again and the test
expression is evaluated.
• This process goes on until the
test expression becomes false.
• If the test expression is false,
the loop ends.
Example:
// Program to add numbers until the user enters zero
#include <stdio.h>
int main()
{
double number, sum = 0;
// the body of the loop is executed at least once
do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
Infinitive do while loop
• The do-while loop will run infinite times if we
pass any non-zero value as the conditional
expression.
do
{
//statement
}while(1);
break & continue
break statement:
The syntactic form of break statement is:
• break;
KEY POINTS
✓ A break statement can appear only inside, or as a body
of, a switch statement or a loop.
✓ A break statement terminates the execution of the
nearest enclosing switch or the nearest enclosing loop.
✓ The execution resumes with the statement present
next to the terminated switch statement or terminated
loop.
• This example jumps out of the loop when i is equal to 4:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
return 0;
}
Continue
• The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.
• This example skips the value of 4:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Break and Continue in While Loop
• You can also use break and continue in while loops:
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
printf("%d\n", i);
i++;
}
return 0;
}
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
printf("%d\n", i);
i++;
}
return 0;
}
Exercise:
• Stop the loop if i is 5
#include <stdio.h>
int main()
{int i;
for (int i = 0; i < 10; i++) {
if (i == 5) {
?
}
printf("%d\n", i);
}}
Programming practice using loops
Q1.Write a program in C to display the first 10
natural numbers.
Expected Output :
1 2 3 4 5 6 7 8 9 10
Solution 1
#include <stdio.h>
void main()
{
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("\n");
}
Eg 2.
• Write a C program to find the sum of first 10 natural
numbers.
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
Eg. 2
#include <stdio.h>
int main()
{
int j, sum = 0;
printf("The first 10 natural number is :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
Eg. Write a program in C to read 10 numbers from keyboard and find their sum and
average.
Solution
#include <stdio.h>
void main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);
scanf("%d",&n);
sum +=n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n",sum,avg);
}
Eg .Write a program in C to display the cube of
the number upto a given integer.
#include <stdio.h>
void main()
{
int i,ctr;
printf("Input number of terms : ");
scanf("%d", &ctr);
for(i=1;i<=ctr;i++)
{
printf("Number is : %d and cube of the %d is :%d
\n",i,i, (i*i*i));
}
}
Factorial of a number
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Inverted half pyramid of *
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
• printf("%d ",j);
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=65;i<=69;i++)
{
for(j=i;j<=69;j++)
printf("%c",j);
printf("\n");
}
}
Mirror Right angle triangle
#include <stdio.h>
int main()
{
int i, j, rows;
/* Input rows from user */
printf("Enter number of rows: ");
scanf("%d", &rows);
/* Iterate through rows */
for(i=1; i<=rows; i++)
{
/* Print spaces in decreasing order of row */
for(j=i; j<rows; j++)
{
printf(" ");
}
/* Print star in increasing order or row */
for(j=1; j<=i; j++)
{ printf("*"); }
/* Move to next line */
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int i, j, n = 5;
char t='A';
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%c ", t);
t++;
}
printf("\n");
}
return 0;
}
Alphabet Pattern
#include <stdio.h>
int main()
{
int i, j, n = 5;
char t='A';
for (i = 1; i <= n; i++) {
char t='A';
for (j = 1; j <= i; j++) {
printf("%c ", t);
t++;
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int i, j;
for (i = 69; i >=65; i--) {
for (j = i; j <= 69; j++) {
printf("%c ", j);
}
printf("\n");
}
return 0;
}
Eg. 9
➢Write a program in C to make such a pattern
like a pyramid with numbers increased by 1.
• 1
23
456
7 8 9 10
#include <stdio.h>
void main()
{
int i,j,spc,rows,k,t=1;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d ",t++);
printf("\n");
spc--;
}
}
THANK-YOU