Intro to Computer Science Loops Dr. Olivera Grujic USC
Loops?
Loops?
Computer Science Loop Control flow statement for specifying iteration, which allows code to be executed repeatedly. https://en.wikipedia.org/wiki/For_loop
While Loop in C++
While Loop in C++
While Loop in C++ Initialization Condition Update
Problem with While Pattern Hard to see all you need to know how the loop progresses Easy to be so engrossed in writing the body of the loop that you forget to increment
For Loop in C++
For Loop in C++ Condition Update Initialization
You Don’t Have to Count Upward!
You Don’t Have to Update by 1!
DoWhile Loop
DoWhile Loop Initialization Update Condition
What Does This Code Print?
Let’s Rewrite Outer Loop to DoWhile
Let’s Rewrite Outer Loop to DoWhile
Code Examples
Loop Through Strings
Verify Phone Number Has 10 Digits
Calculates x to the power of n
Implements “hotPotato” Function takes 2 strings Returns false if both strings are empty If either or both strings are non-empty, swaps the values of strings