0% found this document useful (0 votes)
56 views22 pages

Repetition Structures: CSE115: Computing Concepts

This program will iterate infinitely because the loop condition i != 9 will never become false. i is incremented by 2 each iteration, so it will be 0, 2, 4, 6, 8... and never reach 9 to make the condition false. So the loop has no way to terminate normally.

Uploaded by

Khaleda Ali
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)
56 views22 pages

Repetition Structures: CSE115: Computing Concepts

This program will iterate infinitely because the loop condition i != 9 will never become false. i is incremented by 2 each iteration, so it will be 0, 2, 4, 6, 8... and never reach 9 to make the condition false. So the loop has no way to terminate normally.

Uploaded by

Khaleda Ali
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/ 22

Lecture 07

Repetition Structures
CSE115: Computing Concepts
An Example Problem
• Read in 10 integers and output their
sum
An Example Problem
• Read in 10 integers and output their
sum<stdio.h>
#include printf("Enter a number: ");
int main() scanf("%d",&a);
{ sum += a;
int a, sum = 0; printf("Enter a number: ");
printf("Enter a number: scanf("%d",&a);
"); sum += a;
scanf("%d",&a); printf("Enter a number: ");
sum += a; scanf("%d",&a);
printf("Enter a number: sum += a;
"); printf("Enter a number: ");
scanf("%d",&a); scanf("%d",&a);
sum += a; sum += a;
printf("Enter a number: printf("Enter a number: ");
"); scanf("%d",&a);
scanf("%d",&a); sum += a;
sum += a; return 0;
printf("Enter a number: }
");
Repetition Structure (Loop)
• Executes a number of statements more than
one time without having to write the
statements multiple times.
• Two designs of loop :
• To execute a number of instructions from the
program for a finite, pre-determined number of
time (Counter-controlled loop)
• To execute a number of instructions indefinitely
until the user tells it to stop or a special
condition is met (Sentinel-controlled loop)
Repetition Structure (Loop)

Loop
condition
cond? false

Each round of
true
the loop is called
an iteration. Some
statement(s)

loop body
Repetition Structure (Loop)

• There are 3 types of loops in C:


• while
• for
• do while
Repetition : while Loop
• Syntax : Similar as in the if statement, the condition
is an expression that can return true or
while (condition) false.

statement;

false
cond?
 As long as the condition is met (the
condition expression is true), the true
statement inside the while loop (also
Loop
called loop body) will get executed. body
• When the condition is no longer met (the
condition expression is false), the
program will continue on with the next
instruction (the one after the while loop).
Repetition : while Loop
• In this example :
• (i < 5) is known as loop repetition condition (counter-controlled) .
• i is the loop counter variable.
• In this case, this loop will keep on looping until the counter variable is
equal to 4. Once i = 5, the loop will terminate.
• The printf() statement will get executed as long as the variable i is
less than 5. Since the variable i is incremented each time the loop is
executed, the loop will stop after the 5th output.
• Output:
Example:
i = 0
i = 1
int i = 0;
while (i < 5)
i = 2 {
i = 3 printf(“i = %d\n”, i);
i = 4 i++;
}
Done
printf(“Done”);
Sum of 10 Integers
#include <stdio.h>
int main()
{
int i, a, sum;
sum = 0; Initialization
i = 0;
while(i < 10) Loop
{ condition
printf("Enter a number: ");
scanf("%d", &a);
sum = sum + a;
i++;
} Increment/decrement
printf("Total is %d\n", sum); Loop
return 0; body
}
Repetition : for Loop
• Syntax :

for (expression1; expression2; expression3)


statement;

• expression1: initialize variables including the loop counter variable


• expression2: the loop condition
• expression3: changes the value of loop counter variable after each
iteration (one cycle of the loop)

• Note that each expression is separated by a semicolon (;)


Sum of 10 Integers
Initialization
#include <stdio.h>
int main() Loop
{ condition
int i, a, sum;
sum = 0;
for(i = 0; i < 10; i++) Increment/decrement
{
printf("Enter a number: ");
scanf("%d",&a);
sum += a;
}
printf("Total is %d\n", sum); Loop
return 0; body
}
Repetition : for Loop

• Notice that the output is the same as the


one for the while loop example. In fact,
the two examples are exactly equivalent.
Using a for loop is just another way of
writing a while loop that uses a controlling
variable.
Repetition : for Loop
• It is also possible to omit one or more of the for
loop expressions. In such a case, we just put the
semicolon without the expression.
#include <stdio.h>
int main()
{
int i, a, sum;
sum = 0;
i = 0;
for(; i < 10; i++)
{
printf("Enter a number: ");
scanf("%d",&a);
sum += a;
}
printf("Total is %d\n", sum);
return 0;
}
Repetition : for Loop
• It is also possible to have multiple
initializations/increment/decrement statements
separated by comma.
#include <stdio.h>
int main()
{
int i, a, sum;
for(i = 0, sum = 0; i < 10; i++)
{
printf("Enter a number: ");
scanf("%d",&a);
sum += a;
}
printf("Total is %d\n", sum);
return 0;
}
Loop Until User Inputs 100
#include <stdio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d",&a);
for(; a != 100; )
{
printf("Enter a number: ");
scanf("%d",&a);
}
return 0;
}
Repetition : do while Loop
• Syntax
do
{
statement;
} while(condition);
• A do while loop is pretty much the same as the while
loop except that the condition is checked after the
iteration is complete.
• When there is a do while loop, the statement(s) inside it
will be executed once no matter what. Only after that, the
condition will be checked to decide whether the loop
should be executed again or just continue with the rest of
the program.
Repetition : do while Loop
#include <stdio.h>
int main()
{
int a;
do
{
printf("Enter a number: ");
scanf("%d",&a);
}
while(a != 100);
return 0;
}
Repetition : do while Loop
• Print the digits of an integer number in reverse
order. For example if the input is 45718, your
program should output 81754.
Repetition : do while Loop
• Print the digits of an integer number in reverse
order. For example if the input is 45718, your
program should output 81754.
#include <stdio.h>
int main()
{
int number, digit;
printf(“Enter a number: ");
scanf("%d", &number);
while(number > 0)
{
digit = number % 10;
printf("%d", digit);
number = number / 10;
}
return 0;
}
Repetition : do while Loop
• Sample run 1:
• Enter a number: 2145
• 5412
• Sample run 2:
• Enter a number: 9
•9
• Sample run 3:
• Enter a number: 0

• Sample run 4:
• Enter a number: 8711541
• 1451178
Infinite Loop
#include <stdio.h>
int main()
{
int i;
for(i = 0; i != 9; i+=2)
printf("i = %d\n", i);
return 0;
}
How many times will the loop iterate?
Infinite Loop

• If somehow the program never goes out of


the loop, the program is said to be stuck in
an infinite loop.
• The infinite loop bug happens because the
condition expression of the while loop
always return a true.
• If an infinite loop occurs, the program
would never terminate and the user would
have to terminate the program by force.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy