0% found this document useful (0 votes)
5 views79 pages

11-14 Repetitive Structures For, While Loops Do While

The document explains iterative statements and loop control structures in programming, detailing how loops execute a set of statements repeatedly based on conditions. It categorizes loops into fixed (for loop) and unfixed (while and do-while loops) iterations, and discusses the components of loops such as control variables, body of the loop, test conditions, and step values. Additionally, it provides examples of loop implementations and highlights the importance of correctly designing loops to avoid infinite iterations.

Uploaded by

anshulshah216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views79 pages

11-14 Repetitive Structures For, While Loops Do While

The document explains iterative statements and loop control structures in programming, detailing how loops execute a set of statements repeatedly based on conditions. It categorizes loops into fixed (for loop) and unfixed (while and do-while loops) iterations, and discusses the components of loops such as control variables, body of the loop, test conditions, and step values. Additionally, it provides examples of loop implementations and highlights the importance of correctly designing loops to avoid infinite iterations.

Uploaded by

anshulshah216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

Iterative Statements

• Loop is a repetitive structure used in a program, to execute a set of


statements for a given number of times , in order to carry out specific
task.

• Iteration Statement is nothing but the same set of instruction or


statements will be executed again and again until the condition become
FASLE.

• Repeat the execution of a list of statements, depending on the value of


an integer expression.

05/16/2025 1
• Loop contains the following parts:

• Control Structure: A variable which start with an initial value and determines the
duration of the repetition is known as control variable

• Body of the loop: A set of statements, which are executed within the loop
simultaneously.

• Test Condition: Each loop contains a test condition. The loop has to be repeated
or terminated, depends on test conditions. If the test condition is true, otherwise
terminates.

• Step Value: Looping structure determines the increment or decrement of the


control variables.

05/16/2025 2
Loop Control Structures
• Loops
Unfixed Iterations: While and Do-While loop
Fixed Iterations: For loop
• Unfixed Iterative Loops:
• This types of looping construct is used when the number of iterations is
unknown. In this situation, the loop repeats the execution of the belonging
statements till the given condition is true.
• Fixed Iterative Loops:
• This types of looping structure, the statements are repeated for fixed number of
times. The control terminates after repeating number of times. The control
terminates after repeating the execution of the statements for a given number of
times.
• Syntax :
• Initial Value Test Condition Update
• For(a=1; a<=10; a++)

05/16/2025 4
Controlling the program flow
• Forms of controlling the program
flow:
– Executing a sequence of statements
– Using a test to decide between
alternative sequences (branching) Statement1
Statement2
– Repeating a sequence of statements Statement3
(until some condition is met) Statement4
(looping) Statement5
Statement6
Statement7
Statement8

05/16/2025 5
Program Looping
• A set of statements that executes repetitively for a number of
times.
• Simple example: displaying a message 100 times:

printf(hello !\n”);
printf(hello !\n”)
printf(hello !\n”)

Repeat 100 times
printf(hello !\n”)
printf(hello !\n”)
printf(hello !\n”)

Program looping: enables you to develop concise programs containing


repetitive processes that could otherwise require many lines of code !

05/16/2025 6
05/16/2025 7
The need for program looping

Example problem: computing triangular numbers.


(The n-th triangular number is the sum of the integers from 1 through
n)
#include <stdio.h>
int main (void)
{
int triangularNumber;
triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;
printf(“The eighth triangular number is
%d“,triangularNumber);
return 0;
}

What if we have to compute the 200-th (1000-th, etc) triangular number ?

We have 3 different statements for looping.


05/16/2025 8
Iterative (loop) control structures
 Three kinds of loop control structures:
while
do while
for

05/16/2025 9
Iterative (loop) control structures
 Each loop control structure will have
 Program loop: body of loop.
 control statement  tests certain conditions & then directs repeated
execution of statements within the body of loop.

 Two types: Based on position of control statement.

1) Entry controlled loop: control is tested before the start of the loop. If
false, body will not be executed.

2) Exit controlled loop: test is performed at the end of the body. i.e. body
of loop executed at least once.

05/16/2025 10
Entry Controlled & Exit controlled loops
Entry
Entry

Test False
Body of
Condition
The loop

True

Body of
True
The loop Test
Condition

False

05/16/2025 11
05/16/2025 12
05/16/2025 13
05/16/2025 14
#include<stdio.h>
#include<conio.h>
int main()
{
int i=3;
while(i!=0)
{
printf("Meow");
i=i-1;
}
getch();
return 0;

05/16/2025 15
05/16/2025 16
05/16/2025 17
05/16/2025 18
05/16/2025 19
05/16/2025 20
05/16/2025 21
05/16/2025 22
05/16/2025 23
05/16/2025 24
05/16/2025 25
05/16/2025 26
05/16/2025 27
• The order of execution of the loop is as follows:

• First of all variable a is initialized to 1/0;

• Control check the condition, if it true, then it executes the statements


enclosed within the { }

• After executing the loop body, kt return back to the for statement and
increment/decrement value of a by 1;

• The steps are repeated from (2) and finally the control exits the loop, as
soon as the test condition is fasle.

05/16/2025 28
• For example

• Int I, s=0;

• For(i=1;i<=5;i++)
• S =S+I;
• Printf(“Sum=%d”,s);

Sum=1
Sum=3
Sum=6
Sum=10
Sum=15

05/16/2025 29
05/16/2025 30
05/16/2025 31
05/16/2025 32
05/16/2025 33
05/16/2025 34
05/16/2025 35
05/16/2025 36
05/16/2025 37
For Loop

05/16/2025 38
05/16/2025 39
05/16/2025 40
05/16/2025 41
05/16/2025 42
The do – while statement
General form:
do
{
body of the loop
}
while(test condition);

Exit controlled loop. At the end of the loop, the test condition is evaluated.
After do statement, program executes the body of the Loop.
Then, the condition is tested, if it is true, body of the loop is executed once again
& this process continues as long as the condition is true.
Body of the loop is executed at least once.
do-while loop can be nested.

05/16/2025 43
05/16/2025 44
05/16/2025 45
The do statement
do
program
statement
while Loop with the
( loop_expression ); test at the end !
Body is
executed at least
once !
3 1 Statement(s)

yes
loop_expression
2
No

Next statement 4

05/16/2025 46
05/16/2025 47
05/16/2025 48
Example – 200th triangular number
Statement before triangularNumber = 0
loop

init_expression n=1

no
loop_condition n<=200

yes
triangularNumber =
Statement(s) triangularNumber + n

loop_expression n=n+1

Statement after loop Print triangularNumber

05/16/2025 49
The ‘for’ loop
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}

1 init_expression

no
5 2 loop_condition

yes

3 Program statement

4 Loop expression

Next Statement

05/16/2025 50
How for works
• The execution of a for statement proceeds as follows:
1. The initial expression is evaluated first. This expression usually sets a
variable that will be used inside the loop, generally referred to as an
index variable, to some initial value.
2. The looping condition is evaluated. If the condition is not satisfied (the
expression is false – has value 0), the loop is immediately terminated.
Execution continues with the program statement that immediately
follows the loop.
3. The program statement that constitutes the body of the loop is executed.
4. The looping expression is evaluated. This expression is generally used to
change the value of the index variable
5. Return to step 2.

05/16/2025 51
The for statement
sum = 0;

no

1 2 5 4
for ( n = 1; n yes
<= 200; n = n + 1 )
{ sum = sum + n; }

Next Statement
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}

05/16/2025 52
Finding sum of natural numbers up to 100

#include <stdio.h>
int main()
{
int n;
int sum;
sum=0; //initialize sum

for(n = 1; n <=100; n=n + 1)


{
sum=sum + n;
}
printf(“%d”,sum);
return 0;
}

05/16/2025 53
Infinite loops
• It’s the task of the programmer to design correctly the algorithms so
that loops end at some moment !
// Program to count 1+2+3+4+5
#include <stdio.h>
int main()
{
What is wrong here ?
int i, n = 5, sum =0; Does the loop end?
for ( i = 1; i <= n; n = n + 1 )
{
sum = sum + i;
printf(“%d”,sum);
}
return 0;
}

05/16/2025 54
Example – for with a body of 2 statements

// Program to generate a table of triangular numbers

#include <stdio.h>
int main()
{
int n, triangularNumber=0;

printf(“TABLE OF TRIANGULAR NUMBERS\n\n“);


printf(“Sum from 1 to n\n“);

for ( n = 1; n <= 10; n++ )


{
triangularNumber += n;
printf(“The %d th triangular number is %d\
n”,n,triangularNumber);
}
return 0;
}

05/16/2025 55
for loop variants
• Multiple expressions (comma between…)
for(i=0 , j=10 ; i<j ; i++ , j--)

• Omitting fields (semicolon have to be still…)


i=0;
for( ; i<10 ; i++ )

• Declaring variables
for(int i=0 ; i=10 ; i++ )

05/16/2025 56
while-loop
General format:

while (test expression) Note: braces optional if


{ only one statement.
body of the loop
}

Entry controlled loop statement.


Test condition is evaluated & if it is true, then body of the loop is executed.
This is repeated until the test condition becomes false, & control transferred
out of the loop.
Body of loop is not executed if the condition is false at the very first attempt.
While loop can be nested.

05/16/2025 57
The while statement

while ( expression )
program statement Loop with the
test in the
beginning !
statement before loop Body might
never be
executed !

3 1 Loop_expression 4
No
yes
2 statement (s)

Next statement

05/16/2025 58
Finding sum of natural numbers up to 100

#include <stdio.h> #include <stdio.h>


int main() int main()
{
{ int n;
int n; int sum;
int sum; sum=0; //initialize
sum=0; //initialize sum sum
n=1;
while (n < 100)
for(n = 1; n < 100; n=n + 1) {
{ sum= sum + n;
sum=sum + n; n = n + 1;
} }
printf(“%d”,sum);
printf(“%d”,sum); return 0;
return 0; }
}

05/16/2025 59
Program to reverse the digits of a number
#include <stdio.h>
int main()
{
int number, rev=0, right_digit;

printf(“Enter your number.\n“);


scanf(“%d”,&number);

while ( number != 0 )
{
right_digit = number % 10;
rev=rev*10 + right_digit;
number = number / 10;
}
printf(“The reversed number is %d“, rev);
return 0;
}
05/16/2025 60
Example: Finding sum of natural numbers
up to 100

#include <stdio.h> #include <stdio.h>


int main() int main()
{
{
int n;
int n; int sum =0;
int sum=0;
n=1;
n=1; while (n<=100)
do {
do
{ sum=sum+n;
{ n = n +1;
sum = sum +
n; sum = sum + counter; }
counter
n = n= +1;
counter +1;}} printf(“%d”,sum);
} while
} while (n<<100);
(counter return 0;
=100);
printf(“%d”,sum); }
return 0;
05/16/2025
}
61
Program to reverse the digits of a number

#include <stdio.h>
#include <stdio.h> int main()
int main()
{
{
int number, rev=0, right_digit;
int number, rev=0, right_digit;
printf(“Enter your number.\n“);
printf(“Enter your number.\n“);
scanf(“%d”,&number);
scanf(“%d”,&number);
do
while ( number != 0 )
{
{
right_digit = number % 10;
right_digit = number % 10;
rev=rev*10 + right_digit;
rev=rev*10 + right_digit;
number = number / 10;
number = number / 10;
}
}
while ( number != 0 );
printf(“The reversed number is %d“, rev);
printf(“The reversed number is %d“,rev);
return 0;
return 0;
}
}
05/16/2025 62
Which loop to choose ?
• Criteria: category of looping
• Entry-controlled loop -> for, while
• Exit-controlledloop -> do

• Criteria: Number of repetitions:


• Indefinite loops ->while
• Counting loops -> for

• You can actually rewrite any while as a for and vice versa !

05/16/2025 63
05/16/2025 64
Nested For Loop
• Nested loops are programming structures where one or
more loops are placed inside another loop.

• This allows for more complex control flow and repetitive


execution in programs.
• This leads to the inner loop being fully executed multiple times
within a single iteration of the outer loop.
1.Outer Loop Execution: The outer loop is responsible for controlling
the overall flow of the nested structure.

2.Inner Loop Execution: When the control reaches the inner loop, it
follows a similar process: initialization, condition check, and
execution of statements.

3.Nested Iterations: For each iteration of the outer loop, the inner
loop undergoes its complete set of iterations.

4.Sequential Execution: The statements inside the innermost loop


are executed sequentially, following the order of appearance in the
code.

5.Completion of Outer Loop Iterations: After the inner loop


completes all its iterations for a specific iteration of the outer loop,
the control returns to the outer loop. The outer loop then progresses
to its next iteration or exits if its condition becomes false.
// C program to print right half pyramid pattern of star
#include <stdio.h>
#include<conio.h>

int main()
{
int rows = 5;

// first loop for printing rows


for (int i = 0; i < rows; i++) {

// second loop for printing character in each rows


for (int j = 0; j <= rows; j++) {
printf("* ");
}
printf("\n");
}
getch();
return 0;
}
• int main()
•{
• for (int i = 1; i <=5; i++) {
for(int j=5;j>=i;j--)
{
printf("*");
}
printf("\n");
i=1
} 4 3getch();
J=5 21 return 0; } i=1; j=5 4 3 2 1
• for (int i = 5; I >1; i--) {
for(int j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
} getch(); return 0; }

You might also like