0% found this document useful (0 votes)
36 views28 pages

Loops

This C program uses nested for loops to output the multiplication table of 2 from 1 to 10 in 3 sentences: The outer for loop iterates from 1 to 10 for i, while the inner for loop iterates from 1 to 10 for j, printing

Uploaded by

priyabmishralove
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)
36 views28 pages

Loops

This C program uses nested for loops to output the multiplication table of 2 from 1 to 10 in 3 sentences: The outer for loop iterates from 1 to 10 for i, while the inner for loop iterates from 1 to 10 for j, printing

Uploaded by

priyabmishralove
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/ 28

Programming for

problem solving
using C

Dr Manaswini Jena 1
Looping Statements in C
• The purpose of the C loop is to repeat the same code a number
of times – It executes a sequence of statements many times until
the stated condition becomes false.

• “To run the body continuously until a given condition is true, is


called looping and when the condition will become false the
execution of the loop will be stopped.”

• A loop in C consists of two parts,


1. Body of a loop
2. Control statement
• Control statement - combination of some conditions that directs
the body of the loop
• Body of the loop – Set of instruction to be executed inside that 2
condition
Types of Loops in C
Depending upon the position of a control statement in a program,
looping statement in C is classified into two types:
1. Entry controlled loop

2. Exit controlled loop

• In an entry control loop in C, a condition is checked before


executing the body of a loop. It is also called as a pre-checking
loop.
• In an exit controlled loop, a condition is checked after executing
the body of a loop. It is also called as a post-checking loop.
3
Loops in C

4
While loop and do…while Loop

While loop do … while loop

while (condition) do
{ {
//instruction body //instruction body
//statement 1; //statement 1;
//statement 2; //statement 2;
} }
while(condition); 5
while loop do…while loop

6
Syntax of ‘for’ Loop

for loop

for(initialization ; condition ; incrimination or decrimentation)


{
// statement 1;
// statement 2;
// statement 3;
}

7
for loop

8
Comparing for loop and while loop

9
Comparing all the loops
Sl.
Loop Type Description
No.
• In while loop, a condition is evaluated before
processing a body of the loop.
1. While Loop • If a condition is true then and only then the body of
a loop is executed.

• In a do…while loop, the condition is always executed


Do-While after the body of a loop.
2.
Loop • It is also called an exit-controlled loop.

• In a for loop, the initial value is performed only


once, then the condition tests and compares the 10
3. For Loop
counter to a fixed value after each iteration,
stopping the for loop when false is returned.
WAP to print numbers 1 to 10
#include<stdio.h> Output:
int main(void) 1
{ 2
int i=1; 3
while(i<=10) 4
{ 5
printf("%d\n",i);
6
i = i+1; //i++;
7
}
8
printf("\n");
return 0; 9
} 10 11
Execution Process
• We have initialized a variable called num with value 1. We are
going to print from 1 to 10 hence the variable is initialized with
value 1. If you want to print from 0, then assign the value 0
during initialization.
• In a while loop, we have provided a condition (num<=10), which
means the loop will execute the body until the value of num
becomes 10. After that, the loop will be terminated, and control
will fall outside the loop.
• In the body of a loop, we have a print function to print our
number and an increment operation to increment the value per
execution of a loop. An initial value of num is 1, after the
execution, it will become 2, and during the next execution, it
will become 3. This process will continue until the value
becomes 10 and then it will print the series on console and
terminate the loop. 12
WAP to print numbers 1 to 10
#include<stdio.h>
int main()
{ int num = 1; / * Initialize starting number */
int target = 10; /* Initialize target number */
//Start do while loop
do
{
printf("%d\n", num); //increment the number by 1
++num; //once number is > target in while condition
//then control will be out of loop
} while (num <= target);
return 0; 13
}
WAP to print a table of number 2
#include<stdio.h> Output:
#include<conio.h> 2
int main() 4
{ 6
int num=1; //initializing the variable 8
do //do-while loop 10
{
12
printf("%d\n",2*num);
14
num++; //incrementing operation
16
}while(num<=10);
return 0; 18
} 20 14
Execution process
• First, we have initialized a variable ‘num’ with value 1. Then
we have written a do-while loop.
• In a loop, we have a print function that will print the series by
multiplying the value of num with 2.
• After each increment, the value of num will increase by 1, and
it will be printed on the screen.
• Initially, the value of num is 1. In a body of a loop, the print
function will be executed in this way: 2*num where num=1,
then 2*1=2 hence the value two will be printed. This will go on
until the value of num becomes 10. After that loop will be
terminated and a statement which is immediately after the
loop will be executed. In this case return 0 15
WAP number series from 1-10 using for loop

#include<stdio.h> Output:
int main() 1
{ 2
int number; 3
for(number=1;number<=10;number++) 4
//for loop to print 1-10 numbers 5
{ 6
printf("%d\n",number); 7
} 8
return 0; 9
} 10 16
For loop execution process
• We have declared a variable of an int data type to store
values.
• In for loop, in the initialization part, we have assigned value 1
to the variable number. In the condition part, we have
specified our condition and then the increment part.
• In the body of a loop, we have a print function to print the
numbers on a new line in the console. We have the value one
stored in number, after the first iteration the value will be
incremented, and it will become 2. Now the variable number
has the value 2. The condition will be rechecked and since the
condition is true loop will be executed, and it will print two on
the screen. This loop will keep on executing until the value of
the variable becomes 10. After that, the loop will be
terminated, and a series of 1-10 will be printed on the screen. 17
Some more possible formats in for loop
• It can have multiple expressions separated by commas in each
part, For example:

for (x = 0, y = num; x < y; i++, y--)


{
// statements;
}

• The initial value expression can be skipped, condition and/or


increment by adding a semicolon, For example:

int i=0; int max = 10;


for (; i < max; i++)
{
18
printf("%d\n", i);
}
Program to calculate the sum of first n natural numbers
( Positive integers 1,2,3...n are known as natural numbers)
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
} Output:
Enter a positive integer: 10
Sum = 55
printf("Sum = %d", sum); 19
return 0;
}
Nested Loop
• Loops can also be nested where there is an outer loop and an
inner loop.
• For each iteration of the outer loop, the inner loop repeats its
entire cycle.
for ( init; condition; increment )
{
for ( init; condition; increment ) Nested
{ for loop
statement(s); syntax
}
statement(s);
20
}
while(condition)
{
while(condition)
Nested
{ while loop
statement(s); syntax
}
statement(s);
}

do {
statement(s); Nested
do {
do…while
statement(s); loop syntax
}while( condition );
21

}while( condition )
WAP to output multiplication tables
#include <stdio.h>
int main() 1x0=0
1x1=1
{
1x2=2
int i, j; 1x3=3
int table = 2; 1x4=4
1x5=5
int max = 5;
for (i = 1; i <= table; i++) { // outer loop 2x0=0
for (j = 0; j <= max; j++) { // inner loop 2x1=2
2x2=4
printf("%d x %d = %d\n", i, j, i*j);
2x3=6
} 2x4=8
printf("\n"); /* blank line between tables */ 2 x 5 = 10
22
}
}
Infinite loop
• A loop that repeats indefinitely and does not terminate is
called an “Infinite loop” or “Endless loop.”
• So, when the control conditions are not well defined, the
loop does not stop executing and processes the
statements number of times, then it leads to an infinite
loop.
• Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.

23
Example of infinite loop
• Here the value of i is not updated.
int i = 1; So after each iteration, the value
while(i<10) of i remains the same. As a result,
the condition (i<10) will always be
{ true.
printf("%d\n", i); • For the loop to work correctly i++;
} is to be added, just after the
printf() statement.

• This loop will produce no


int i = 0; output and will go on executing
indefinitely.
while(i<=5); • The ( ; ) at the end of while
condition is equivalent to a null
printf("%d\n", i) statement. 24
break statement in C
• The break is a keyword in C which is used to bring the
program control out of the loop.
• It is useful for immediately stopping a loop or to exit from
the loop.
• The break statement is used inside loops or switch
statement. The break statement breaks the loop one by
one, i.e., in the case of nested loops, it breaks the inner
loop first and then proceeds to outer loops.
• The break statement is used mostly in the switch
statement.
25
continue Statement in C
• The continue is a keyword in C language is used to bring
the program control to the beginning of the loop.
• The continue statement skips some lines of code inside
the loop and continues with the next iteration.

//loop statements.
continue;
//some lines of the code which is to be skipped

26
Example for using break
#include <stdio.h>
int main() Output:
{
int num = 5; 5
while (num > 0) 4
{
if (num == 3)
break;
printf("%d\n", num);
num--;
}
} 27
Example for using continue
#include <stdio.h>
int main() Output:
{
6
int nb = 7;
4
while (nb > 0) 3
{ 2
nb--; 1
if (nb == 5)
continue;
printf("%d\n", nb);
} 28

You might also like