Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei

Similar presentations


Presentation on theme: "Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei"— Presentation transcript:

1 Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei

2 Loops Repeat a statement, or a block of statements
Repeat a statement or a block of statements until a particular condition is fulfilled for, while, and do-while loops The increment and decrement operators Plays a simple Simon game Beiginning C / Qinpei Zhao 2018/9/19

3 Endless loop Beiginning C / Qinpei Zhao 2018/9/19

4 How loop works Loop: executes a series of statements repeatedly a given number of times or until a particular condition is fulfilled. Game: guessing numbers(lec4_1_GuessNumberGame.c) Beginning C / Qinpei Zhao 2018/9/19

5 The increment and decrement operator
increment operator (++) 自增 Increase the value stored in the integer variable that they apply to by 1. ++number; decrement operator (--) 自减 Decrease the value stored in the integer variable that they apply to by 1. -- number; the variables do change ! If number = 2; then number ++; will modify the number to 3; number = number + 1; number = number - 1; Beginning C / Qinpei Zhao 2018/9/19

6 The for loop display the numbers from 1 to 10
Beginning C / Qinpei Zhao 2018/9/19

7 Executes once, when the loop starts.
The for loop (cont.) Executes once, when the loop starts. Beginning C / Qinpei Zhao 2018/9/19

8 The for loop (cont.) The expression is evaluated at the beginning of each loop cycle. If it is true, the loop continues, otherwise, the loop ends. Beginning C / Qinpei Zhao 2018/9/19

9 The expression is executed at the end of every loop cycle.
The for loop (cont.) The expression is executed at the end of every loop cycle. Beginning C / Qinpei Zhao 2018/9/19

10 Logical expression (true or false)
The for loop (cont.) Logical expression (true or false) Within parentheses; Each expression is separated by semicolon; You can omit any of the control expressions, but semicolon should be there. Beginning C / Qinpei Zhao 2018/9/19

11 The for loop display the numbers from 1 to 10
Beginning C / Qinpei Zhao 2018/9/19

12 Drawing a box (lec4_2_drawbox.c) Beginning C / Qinpei Zhao 2018/9/19

13 The increment operator ++
What is the number of total? 8 Beginning C / Qinpei Zhao 2018/9/19

14 The prefix and postfix forms of ++
Prefix ++i: the incrementing occurs before its value has been used Postfix i++: the incrementing occurs after its value has been used reduce confusion by using parentheses What is the number of total? 7 Beginning C / Qinpei Zhao 2018/9/19

15 The flexible for loop How about this? How about ++i?
Beginning C / Qinpei Zhao 2018/9/19

16 A for loop with No parameters
Is the grammar correct? What will happen? Beginning C / Qinpei Zhao 2018/9/19

17 The break statement in the loop
Beginning C / Qinpei Zhao 2018/9/19

18 A minimal for loop (lec4_4_indefinite.c) Beginning C / Qinpei Zhao
2018/9/19

19 Generating pseudo-random integers
Pseudo-random: truly random numbers can arise only in natural processes and can’t be generated algorithmically. Standard function with seed as a paramter Default seed <stdlib.h> [0, RAND_MAX] Beginning C / Qinpei Zhao 2018/9/19

20 More for loop control options
Too much of this can make your code hard to understand. Do not rely on equality of floating numbers Beginning C / Qinpei Zhao 2018/9/19

21 Equality of floating numbers
A = = B Relative error Absolute error

22 The while loop Beginning C / Qinpei Zhao 2018/9/19

23 Using the while loop Beginning C / Qinpei Zhao 2018/9/19

24 Nested loops Beginning C / Qinpei Zhao 2018/9/19

25 goto statement 10*20*30 is a statement following the nested loops.
Beginning C / Qinpei Zhao 2018/9/19

26 do-while while v.s. do-while? run the statement at least once
Beginning C / Qinpei Zhao 2018/9/19

27 The continue statement
You don’t want to end a loop, but you want to skip the current iteration and continue with the next. continue is a keyword Beginning C / Qinpei Zhao 2018/9/19

28 Designing a program game of Simple Simon
Simple Simon is a memory-test game. The computer displays a sequence of digits on the screen for a short period of time. You then have to memorize them, and when the digits disappear from the screen, you must enter exactly the same sequence of digits. Each time you succeed, you can repeat the process to get a longer list of digits for you to try. The objective is to continue the process for as long as possible. game of Simple Simon lec4_problem.c Beginning C / Qinpei Zhao 2018/9/19

29 Summary three different loops you can use to repeatedly execute a block of statements. The for loop: use for counting loops where the value of a control variable is incremented or decremented by a given amount on each iteration until some final value is reached. The while loop: the loop continues as long as a given condition is true. The do-while loop, works like the while loop except that the loop condition is checked at the end of the loop block. Consequently the loop block is always executed at least once. flow chart understand operator precedence (use parentheses) comment, indentation Beginning C / Qinpei Zhao 2018/9/19


Download ppt "Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei"

Similar presentations


Ads by Google