0% found this document useful (0 votes)
10 views17 pages

Understanding Loop Structures in Programming

The document covers control flow in programming, focusing on sequential instructions, conditional statements like 'if' and 'switch', and the use of loops for repetition. It details three types of loops: 'for', 'while', and 'do-while', and explains their general structure, including the importance of iteration and control mechanisms. Additionally, it provides examples of using 'for' loops to perform various tasks such as printing messages, calculating powers, and determining average scores.

Uploaded by

evatar50
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)
10 views17 pages

Understanding Loop Structures in Programming

The document covers control flow in programming, focusing on sequential instructions, conditional statements like 'if' and 'switch', and the use of loops for repetition. It details three types of loops: 'for', 'while', and 'do-while', and explains their general structure, including the importance of iteration and control mechanisms. Additionally, it provides examples of using 'for' loops to perform various tasks such as printing messages, calculating powers, and determining average scores.

Uploaded by

evatar50
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

Control of flow

• We learned that default flow of Read b and c

instructions is sequential.
a = b+c

Display a

• Then, we learned how to control


Read age

age<
T F

the flow using "if" and "switch."


=25 ?

Message: "You Message: "You


are young" are mature"

Rest of the program

• Now, we will learn how to repeat Any


students
left?

a group of instructions.
F
T

Read score of the student

Add it to the overall sum

Display sum

CSE1145 - Introduction to Computer Programming 0


Types of loops
• There are three types of loops:
– "for" loop
– "while" loop
– "do-while" loop
• You can implement anyone of these loops
using the others (and possibly an
additional if statement and duplication of
some statements).
– The idea is to use the type of loop that is
best suited for your problem.
CSE1145 - Introduction to Computer Programming 1
General structure of a loop
• A loop is typically composed of three
parts:
– the statement block which is to be repeated;
– a control mechanism to decide whether the
statement block should be repeated once
more (based on an expression);
– an update mechanism that affects the value
of the control expression (to avoid infinite
loops).
• The update mechanism may be embedded
in the statement block.
CSE1145 - Introduction to Computer Programming 2
General structure of a loop
• One execution of the statement block is
called an iteration.
• Thus, a loop iterates the statement block
several times.
• Make sure:
– it is possible to enter the loop;
– it is possible to get out of the loop, once you
enter it.

CSE1145 - Introduction to Computer Programming 3


For loop
• Use for loop if you know the number of
iterations.
– You don't have to know the exact number of
iterations; it is enough if you can express it.

CSE1145 - Introduction to Computer Programming 4


For loop
• Syntax:
for (initial_stat(s);int_expr; final_stat(s))
stat_block
initial_stat(s)

F
int_expr
final_stat(s)
T

stat_block

Rest of the program

CSE1145 - Introduction to Computer Programming 5


For loop
• Note that initial and final statements as
well as the integer expression are
optional according to the syntax.
– If initial_stat(s) is missing, start
directly with the comparison.
– If final_stat(s) is missing, go directly to
the comparison after the execution of the
statement block.
– If int_expr is missing, the comparison is
always true.
• Make use of the break statement (to be
discussed later).
CSE1145 - Introduction to Computer Programming 6
for: Example 1
Write a code segment that prints the text "I didn't do
my homework" 100 times with every line enumerated.
printf("1. I didn't do my homework.\n");
printf("2. I didn't do my homework.\n");
printf("3. I didn't do my homework.\n");
printf("4. I didn't do my homework.\n");
printf("5. I didn't do my homework.\n");
printf("6. I didn't do my homework.\n");
printf("7. I didn't do my homework.\n");
printf("8. I didn't do my homework.\n");
printf("9. I didn't do my homework.\n");
printf("10. I didn't do my homework.\n");
printf("11. I didn't do my homework.\n");
printf("12. I didn't do my homework.\n");
printf("13. I didn't do my homework.\n");
printf("14. I didn't do my homework.\n");
printf("15. I didn't do my homework.\n");
printf("16. I didn't do my homework.\n");
printf("17. I didn't do my homework.\n");
printf("18. I didn't do my homework.\n");
printf("19. I didn't do my homework.\n");
printf("20. I didn't do my homework.\n");
.
.
.
printf("91. I didn't do my homework.\n");
printf("92. I didn't do my homework.\n");
printf("93. I didn't do my homework.\n");
printf("94. I didn't do my homework.\n");
printf("95. I didn't do my homework.\n");
printf("96. I didn't do my homework.\n");
printf("97. I didn't do my homework.\n");
printf("98. I didn't do my homework.\n");
printf("99. I didn't do my homework.\n");
printf("100.I didn't do my homework.\n");

CSE1145 - Introduction to Computer Programming 7


for: Example 1 (cont'd)
Lazy student's solution ☺

int i;

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


printf("%d. I didn't do my homework.\n", i);

CSE1145 - Introduction to Computer Programming 8


for: Example 2
Find ab. (a and b integers)
int a, b, result=1, i;

scanf("%d %d", &a, &b);


for (i=0; i<b; i++)
result *= a;

CSE1145 - Introduction to Computer Programming 9


for: Example 2 (cont'd)
Same question, solved with fewer variable
(but the value of b changes).
int a, b, result=1;

scanf("%d %d", &a, &b);


for (; b; b--)
result *= a;
printf("result=%d", result);
CSE1145 - Introduction to Computer Programming 10
for: Example 3
Find the average of the midterm scores of
the students in CSE1145.
int i, sum, no_stu, score;
float avg;

scanf("%d", &no_stu);
for (sum=i=0; i<no_stu; i++)
{
What is wrong here?
scanf("%d", &score);
sum += score;
What else?
}
What if "no_stu" is zero?
avg = sum/no_stu;

CSE1145 - Introduction to Computer Programming 11


for: Example 4
Calculate BMI (Body Mass Index) of all
students in class 
• weight/height
for (i=0; i<100; i++)
{
scanf("%d %d", &weight, &height);
printf("BMI: %f\n", (float)weight/height);
}

What if "height" is zero?

CSE1145 - Introduction to Computer Programming 12


for: Example 5
What are the differences between these code segments?
for (i=0; i<5; i++) for (i=0; i<=5; i++) for (i=1; i<5; i++)
printf("%d;", i); printf("%d;", i); printf("%d;", i);
printf("{%d}",i); printf("{%d}",i); printf("{%d}",i);
0;1;2;3;4;{5} 0;1;2;3;4;5;{6} 1;2;3;4;{5}

for (i=1; i<=5; ++i) for (◼; i<5; i++) for (i=0;◼; i++)
printf("%d;", i); printf("%d;", i); printf("%d;", i);
printf("{%d}",i); printf("{%d}",i); printf("{%d}",i);
Starts from anything ...;3;4;{5}
1;2;3;4;5;{6} OR MAYBE SOMETHING LIKE {795} 0;1;2;...∞

for (i=0; i<5;◼) for (i=0; i<5;◼) for (i=0; i++<5;◼)


printf("%d;", i); printf("%d;",i++); printf("%d;", i);
printf("{%d}",i); printf("{%d}",i); printf("{%d}",i);
0;0;0;0;...∞ 0;1;2;3;4;{5} 1;2;3;4;5;{6}

CSE1145 - Introduction to Computer Programming 13


for: Example 6
Now, compare these code segments.
for (i=7; i<5; i++) for (i=7;++i<5;◼) for (i=7;i++<5;◼)
printf("%d;", i); printf("%d;", i); printf("%d;", i);
printf("{%d}",i); printf("{%d}",i); printf("{%d}",i);
{7} {8} {8}

for (i=7; ++i<5; ++i) for (i=0; i<5; ++i)


printf("%d;",++i); printf("%d;",++i);
printf("{%d}",i); printf("{%d}",i);

{8} 1;3;5;{6}

CSE1145 - Introduction to Computer Programming 14


for: Example 7
• What if Carl Friedrich Gauss knew how to
program when he was in elementary school?
– Let's be more generic; add numbers from 1 to n.
scanf("%d", &n);
for (sum=0, i=1; i<=n; i++)
sum += i;
• Of course Gauss would be clever enough to
do
scanf("%d", &n);
sum = n * ((n+1)/2);
– What he right?
CSE1145 - Introduction to Computer Programming 15
for: Example 8
Find whether a number is perfect or not.
(A number is perfect if sum of its positive divisors except
itself is equal to itself. Eg: 6=1+2+3; 28=1+2+4+7+14)
int number, i, sum = 0;

scanf("%d",&number);
for (i = 1; i <= number / 2 ; i++)
if (number % i == 0)
sum += i;
if (number == sum)
printf("%d is a perfect number",number);
else
printf("%d is not a perfect number",number);

CSE1145 - Introduction to Computer Programming 16

You might also like