Download presentation
Presentation is loading. Please wait.
Published byElwin McDowell Modified over 9 years ago
1
Tutorial 2 Control Flow: Loops NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
2
Quick Summary 2 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
3
Q1: Program Tracing 3 Trace the program shown on the left for i = 0 to i = 30; What is the output if n = 321; CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
4
Q1: Program Tracing 4 count 2 count 3 count 5 i n ? 0 0 0 0 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
5
Q1: Program Tracing 5 count 2 count 3 count 5 i n ? 0 0 0 0 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
6
Q1: Program Tracing 6 count 2 count 3 count 5 i n ? 0 0 0 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
7
Q1: Program Tracing 7 count 2 count 3 count 5 i n ? 0 0 0 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
8
Q1: Program Tracing 8 count 2 count 3 count 5 i n ? 0 0 1 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
9
Q1: Program Tracing 9 count 2 count 3 count 5 i n ? 1 0 1 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
10
Q1: Program Tracing 10 We then observe that: count5: numbers divisible by 5 count3: numbers divisible by 15 count2: numbers divisible by 2 BUT not divisible by 10! CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO We observe that: count5 counts for:0,5,…,320 count3 counts for:0,15,…,315 count2 counts for:2,4,6,8,12,…,
11
Q2: Program Tracing 11 Given the program on the right, what do you think will happen? What will actually happen? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
12
Q2: Program Tracing 12 We observe that: The value of i will change in the following pattern: 1, 2, 3, …, …, …, All values satisfy i>0, the program will end in infinite loop. However… CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
13
Q2: Infinite Loop & Data Type Range 1.The program ends up in value -2147483648 after a short while. 2.Integer data type has a finite range on your computer, from 2147483648 to -2147483648. 3.Incrementing from 2147483648 causes overflow and it will immediately jump to the negative extreme. 13 -21474836482147483648 0 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
14
Q3: Sum of Sequence and Series 14 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
15
Q3: Sum of Sequence and Series 15 Sum = 1 + 2 + 3 + … + nSum = n + (n-1) + … + 3 + 2 + 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
16
Q3: Sum of Sequence and Series 16 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO while(n--){…} This structure is frequently used as a simple loop to repeat n times. Important: you can only use this method if you value of n is not used again later on.
17
Q3: Sum of Sequence and Series 17 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO Note: Value of “i”: 1, 3, 5, …, Value of “j”: 1, -1, 1, -1, …, Value of “i*j”: 1, -3, 5, -7, …,
18
Q3: Sum of Sequence and Series 18 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO 4.0*j/i: Differentiate between integer division and floating point division: 4/5 = 0; 4.0/5 = 0.800000;
19
Q3: Sum of Sequence and Series 19 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
20
Q4: Printing 2D Matrix 20 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
21
Q4: Printing 2D Matrix 21 ij j j j j CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
22
Q4: Printing 2D Matrix 22 %4d: The output number will take up altogether 4 spaces. If the number need more than 4 spaces, just print normally. If the number need less than 4, add blank spaces in front. Challenge: How to print inverted triangle? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
23
Q5: Prime Number Test 23 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
24
Q5: Prime Number Test 24 Exhaustively test from 2 to k Check end configuration, if “i <= k”, it means it did not pass through all tests. CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.