cse 1310 - introduction to computers & programming -...
TRANSCRIPT
![Page 1: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/1.jpg)
CSE 1310 - Introduction toComputers & Programming
Loops
Dr. Alex Dillhoff
University of Texas at Arlington
Fall 2020
![Page 2: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/2.jpg)
Loops
Loops allow us to express multiple iterations ofstatements compactly.
We will cover:
I Loop statementsI whileI do-whileI for
I Exiting gracefully
![Page 3: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/3.jpg)
while Loops
Syntax
while (EXPRESSION)
STATEMENTS
Simple Example
int count = 0;
while (1) {
printf("%d\n", count++;);
}
![Page 4: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/4.jpg)
while Loops
I EXPRESSION evaluated at the top of the loop.
I Statements in loop are executed.
I Once the bottom is reached, return to the top.
I Control the loop through the EXPRESSION.
![Page 5: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/5.jpg)
while Loops
/* Count to 10 */
int count = 0;
while (count < 10) {
printf("%d\n", count++);
}
Does this program do what was intended?
Answer: No! It only counts to 9.
![Page 6: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/6.jpg)
while Loops
/* Count to 10 */
int count = 0;
while (count < 10) {
printf("%d\n", count++);
}
Does this program do what was intended?Answer: No! It only counts to 9.
![Page 7: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/7.jpg)
while Loops
Let’s modify this slightly.
/* Count to 10 */
int count = 0;
while (count <= 10) {
printf("%d\n", count++);
}
Does this program do what was intended?
Answer: Yes! It now includes 10.
![Page 8: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/8.jpg)
while Loops
Let’s modify this slightly.
/* Count to 10 */
int count = 0;
while (count <= 10) {
printf("%d\n", count++);
}
Does this program do what was intended?Answer: Yes! It now includes 10.
![Page 9: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/9.jpg)
while Loops
Example: Is Prime? (is_prime.c)
![Page 10: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/10.jpg)
while Loops
Example: Guessing Game (guess.c)
![Page 11: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/11.jpg)
while Loops
Example: System Menu (menu.c)
![Page 12: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/12.jpg)
for Loops
for loops provide a convenient syntax for looping aspecified number of times.
Syntax
for (INIT.; CONDITION; PROCESSING)
STATEMENTS
Simple Example
/* Count to 10 */
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
![Page 13: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/13.jpg)
for Loops
I Initialization - Allows us to create the loopcounting variable.
I Condition - Set the test condition for whichthe loop should continue or stop.
I Processing - Defines what should happenafter each iteration of the loop.
![Page 14: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/14.jpg)
for Loops
Example: Multiples of 3 and 5 (multiple.c)
![Page 15: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/15.jpg)
do-while Loops
do-while loops guarantee a single iteration of theloop.
Syntax
do
STATEMENTS
while (CONDITION)
![Page 16: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/16.jpg)
do-while Loops
Example: Guessing Game Again (guess2.c)
![Page 17: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/17.jpg)
Infinite Loops
Infinite loops are most common with while loops.
I Make sure the condition can be broken.
I Remember to update your loop counter (ifapplicable).
I Use control statements.
![Page 18: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/18.jpg)
Infinite Loops
With a while loop:
while (1);
![Page 19: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/19.jpg)
Infinite Loops
With a for loop:
for (;;);
![Page 20: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/20.jpg)
Nested Loops
I Any amount of loops can be nested.
I Increases the computation time.
I Useful for having an outer control loop to keepthe user in a program.
![Page 21: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/21.jpg)
Nested Loops
EXAMPLE: Prime Factorization(prime_factor.c)
![Page 22: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/22.jpg)
Additional Control
Additional control is available with loops throughthe following statements.
I break;
I continue;
I return;
I exit();
![Page 23: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/23.jpg)
Additional Control – break
The break statement immediately exits a loop.
If the loop is the inner loop of a nested loop, it willreturn control to the outer loop.
![Page 24: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/24.jpg)
Additional Control – break
while (!found) {
// Break if target found
if (input == target) {
break;
}
input++;
}
![Page 25: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/25.jpg)
Adding Control – continue
The continue statement skips to the bottom ofthe loop.
This is commonly used to skip unnecessarycalculations depending on the data.
![Page 26: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/26.jpg)
Adding Control – continue
// Don't divide by anything
// that is divisible by 11
for (int i = 0; i < n; i++) {
if (i % 11 == 0)
continue;
input /= i;
}
![Page 27: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/27.jpg)
Adding Control – return
The return statement immediately exits thecurrent function.
If executed in main, the program exits.
![Page 28: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/28.jpg)
Adding Control – exit
The exit() function will immediately exit theprogram, regardless of where it is executed.
There is typically always a better way to exit thefunctions and program without it.
![Page 29: CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops](https://reader033.vdocuments.site/reader033/viewer/2022060911/60a60294a89cb242337b7626/html5/thumbnails/29.jpg)
Adding Control – exit
int main() {
for (int i = 0; i < 10; ++i) {
if (i == 5) {
exit();
}
}
return 0;
}