2. Core Programming Understand computerstorage and data types Understand computerdecision structures Identify the appropriatemethod for handlingrepetition Understand errorhandling
3. Identify the appropriatemethod for handlingrepetition Lesson Overview Students will identify the appropriate method for handling repetition. In this lesson, you will learn: for loops while loops do..while loops Recursion
4. Identify the appropriatemethod for handlingrepetition Iterations in Real Life An iteration is the act of repeating a set of steps to perform a task. For example: Turn the screwdriver until the screw is tight. Rub my hands under the air dryer until they are dry. Iterations are modeled in computers. For example: (C#) for(int i = 0; i < 10; i++) Console.WriteLine(“I repeat ten times”);
5. Identify the appropriatemethod for handlingrepetition The while Loop Allows code to be repeated so long as a Boolean condition is met. The condition is evaluated before the code is executed. Can be used when the number of iterations is not known before executing the loop. inti = 0; // Initialization while (i < 5) // Condition { Console.WriteLine(i); i = i + 1; // Increment }
6. Identify the appropriatemethod for handlingrepetition The for Loop Allows code to be repeated using a loop counter to control how many times it repeats. Used when the number of iterations is known before executing the loop. Initialization ConditionIncrement for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
7. Identify the appropriatemethod for handlingrepetition while Loop vs. for Loop string line = "default"; while (line != "") { Console.Write("Enter a word (while):"); line = Console.ReadLine(); } Console.WriteLine(); for (int i = 0; i < 5; i++) { Console.Write("Enter a word (for) :"); line = Console.ReadLine(); }
8. Identify the appropriatemethod for handlingrepetition The do-while Loop Allows code to be repeated so long as a Boolean condition is met. The condition is evaluated after the code is already executed once. Can be used when the number of iterations is not known before string line = “default”; do { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } while (line != null);
9. Identify the appropriatemethod for handlingrepetition while Loop vs. do-while Loop A do-while loop will execute at least once. A while loop might not execute at all. Console.WriteLine(“Enter a word:”); String line = Console.ReadLine(); while (line != null) { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } do { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } while (line != null)
10. Identify the appropriatemethod for handlingrepetition Counting from 1 to 10 with Different Loops for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } inti = 1; while (i <= 10) { Console.WriteLine(i); i++; } inti = 1; do { Console.WriteLine(i); i++; } while (i <= 10);
11. Identify the appropriatemethod for handlingrepetition Scope Errors for(int i = 0; i < 5; i++) { Console.Writeline(i); } Console.WriteLine(i); // syntax error // i is a local variable
12. Identify the appropriatemethod for handlingrepetition Recursion Recursion occurs when a method calls itself to solve another version of the same problem. With each recursive call, the problem becomes simpler and moves toward a base case. The base case is reached when no other recursive call is required. A base case is the point when no other recursive calls are made.
13. Identify the appropriatemethod for handlingrepetition Factorials public int fact(int n) { if (n == 1) return 1; else return n * fact(n - 1); } fact (4) 24 fact (4) 4 * fact (3) 6 fact (3) 3 * fact (2) 2 fact (2) 2 * fact (1) 1 fact (1)
14. Identify the appropriatemethod for handlingrepetition public int identity(int num) { if(num < 1) return 10; else return num + identity(num - 2); }
15. Assignment 1. Transform the following while loop into a for loop. int num = 1; while (num < 20) { Console.WriteLine(num); num = num + 1; } 2. Transform the following for loop into a while loop. for (int num = 1; num < 20; num = num+1) Console.WriteLine(num); 3. Transform the following while loop into a for loop int num = 10; while (num >= 0) { Console.WriteLine(num); num = num - 1; } 4. Whatoutput is produced by the following code? for (int x = 0; x < 3; x++) Console.WriteLine(x*3); 5. How many lines of output will the following code produce? for (int x = 0; x < 13; x=x+2) Console.WriteLine(“line”);
16. Answer 1. for (int num = 1; num < 20; num = num+1) Console.WriteLine(num); 2. int num = 1; while (num < 10) { Console.WriteLine(num); num = num + 1; } 3. for (int num = 10; num >= 0; num = num-1) Console.WriteLine(num); 4. 0 3 6 5. 7