Tutorial 2 Control Flow: Loops NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Quick Summary 2 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
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
Q1: Program Tracing 4 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q1: Program Tracing 5 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q1: Program Tracing 6 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q1: Program Tracing 7 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q1: Program Tracing 8 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q1: Program Tracing 9 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
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,…,
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
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
Q2: Infinite Loop & Data Type Range 1.The program ends up in value after a short while. 2.Integer data type has a finite range on your computer, from to Incrementing from causes overflow and it will immediately jump to the negative extreme CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q3: Sum of Sequence and Series 14 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q3: Sum of Sequence and Series 15 Sum = … + nSum = n + (n-1) + … CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
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.
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, …,
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 = ;
Q3: Sum of Sequence and Series 19 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q4: Printing 2D Matrix 20 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
Q4: Printing 2D Matrix 21 ij j j j j CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
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
Q5: Prime Number Test 23 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO
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