Upcoming SlideShare
×

# 1.3 core programming [identify the appropriate method for handling repetition]

813

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
813
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
57
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 1.3 core programming [identify the appropriate method for handling repetition]

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

Clipping is a handy way to collect important slides you want to go back to later.