29
CSE 1310 - Introduction to Computers & Programming Loops Dr. Alex Dillhoff University of Texas at Arlington Fall 2020

CSE 1310 - Introduction to Computers & Programming - Loopsvlm1.uta.edu/~dillhoff/teaching/fall2020/cse1310/files/loops.pdf · CSE 1310 - Introduction to Computers & Programming Loops

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Adding Control – exit

int main() {

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

if (i == 5) {

exit();

}

}

return 0;

}