Overview Loop while Statement dowhile Statement for Statement

  • Slides: 27
Download presentation

Overview ������� (Loop) ������ while Statement ������ do-while Statement ������ for Statement ������ break

Overview ������� (Loop) ������ while Statement ������ do-while Statement ������ for Statement ������ break ��� continue 2

����� Iteration Statement �������������� Loop ������ C ������ 4 ������� while statement do-while for

����� Iteration Statement �������������� Loop ������ C ������ 4 ������� while statement do-while for statement break and continue statement 5

while statement flowchart Entry Statement 2 ex True Statement 1 pr False Exit 7

while statement flowchart Entry Statement 2 ex True Statement 1 pr False Exit 7

Example 1: ���� 10 ������ while loop Print count from 1 to 10 1

Example 1: ���� 10 ������ while loop Print count from 1 to 10 1 2 3 4 5 6 7 8 9 10 8

Example 2: Output 1. Withdraw 2. Show Balance 3. Transfer Enter your choice (1

Example 2: Output 1. Withdraw 2. Show Balance 3. Transfer Enter your choice (1 -3): 5 Please input your choice correctly. 1. Withdraw 2. Show Balance 3. Transfer Enter your choice (1 -3): 1 Withdraw 10

do while statement flowchart Entry Statement 1 Statement 2 True ex pr False Exit

do while statement flowchart Entry Statement 1 Statement 2 True ex pr False Exit 12

Example 3: ���� 10 ������ do while loop Print count from 1 to 10

Example 3: ���� 10 ������ do while loop Print count from 1 to 10 1 2 3 4 5 6 7 8 9 10 13

Example 4: Output 1. Withdraw 2. Show Balance 3. Transfer Enter your choice (1

Example 4: Output 1. Withdraw 2. Show Balance 3. Transfer Enter your choice (1 -3): 4 Please input your choice correctly. 1. Withdraw 2. Show Balance 3. Transfer Enter your choice (1 -3): 2 Show Balance 15

for statement syntax [cont. ] สามารถเปรยบเทยบกบการใช ไดดงน initial; while (condition) { statement 1; …

for statement syntax [cont. ] สามารถเปรยบเทยบกบการใช ไดดงน initial; while (condition) { statement 1; … statementn; incrementing; } while 18

for statement flowchart ������ for (counter = 1; counter <=10; counter = counter+1) {

for statement flowchart ������ for (counter = 1; counter <=10; counter = counter+1) { statement; } Entry counter = 1 counter <= 10 True statement counter = counter +1 Exit 19 False

Example 5: ���� 10 ������ for loop Print count from 1 to 10 1

Example 5: ���� 10 ������ for loop Print count from 1 to 10 1 2 3 4 5 6 7 8 9 10 20