class: middle, center # Lecture 4 ### 30 August 2021 **Admin Matters**
Unit 8: **Conditionals**
Unit 9: **Logical Expression**
Unit 10: **Assertion**
--- class: middle ## Exercises - Exercise 0 released last week - Exercise 1 after this class - Exercise 2 this Thursday (during lab) - 13 problems in total --- class: middle ## Assignment 1 - Release this Thursday by noon - Due next Tuesday 23:59 - 3% of your grade --- class: middle ## Please read - [How NOT to Go About a Programming Assignment](http://people.irisa.fr/Martin.Quinson/Teaching/how-not-to-code.pdf) - [About Assignments in CS1010](assignments.md) - [General CS1010 Policies](policy.md) --- class: middle ## Midterm and PE1 Preparation - [General SoC eExam Instructions](https://mysoc.nus.edu.sg/academic/e-exam-sop-for-students/) - Specific CS1010 instructions will be given next week --- class: middle - Mock exam will be conducted - Midterm will be done using LumiNUS Quiz - PE1 will be on CS1010 PE hosts (with special account) - Arrangement will be made for on-campus venues --- class: middle
Problem Solving
decomposition
recursion
flowchart
C Language/Syntax
types
functions
arithmetic ops
Behavioural/Mental Model
machine code
data in memory
types
Tools/Good Practices
clang
vim
bash
--- class: middle
Problem Solving
decomposition
recursion
flowchart
conditionals
assertion
C Language/Syntax
types
functions
arithmetic ops
if-else
logical expression
Behavioural/Mental Model
machine code
data in memory
types
Tools/Good Practices
clang
vim
bash
--- class: middle,wide .fit[![max](figures/max-flowchart-dark/max-flowchart-dark.001.png)] --- class: middle,wide .fit[![factorial](figures/max-flowchart-dark/max-flowchart-dark.005.png)] --- class: middle | Score | Letter Grade | | --------------------------- | ------------ | | 8 or higher | A | | Less than 8 but 5 or higher | B | | Less than 5 but 3 or higher | C | | Less than 3 | D | --- class: middle | Score | Letter Grade | | ----------- | ------------ | | 8 or higher | A | | Less than 8 | See Table 1 | --- class: middle Table 1 (less than 8) | Score | Letter Grade | | ----------- | ------------ | | 5 or higher | B | | Less than 5 | See Table 2 | --- class: middle Table 2 (less than 5) | Score | Letter Grade | | ----------- | ------------ | | 3 or higher | C | | Less than 3 | D | --- class: middle,wide What will get printed if `score` is 5? .small[ ```C if (score >= 8) if (late_penalty != 0) cs1010_println_string("late submission"); else cs1010_println_string("you can do better!"); ``` ] --- class: middle,center .fit[![meme](figures/meme/bad-good-curly.png)] --- class: middle,center .fit[![meme](figures/meme/bad-good-return-else.png)] --- class: middle,center .fit[![meme](figures/meme/bad-good-tertiary.png)] --- class: middle,center .fit[![meme](figures/meme/bad-good-fp-equal.png)] --- class: middle,center .fit[![meme](figures/meme/bad-good-bool.png)] --- class: middle - Comparison operations : `>`, `<`, `>=`, `<=`, `==`, `!=` - Logical operators : `&&`, `||`, `!` --- class: middle,center ## De Morgan's Law `!(e1 && e2)` is the same as `(!e1) || (!e2)` --- class: middle,center ## De Morgan's Law `!(e1 || e2)` is the same as `(!e1) && (!e2)` --- class: middle,center ## Short Circuiting ### `a && b` ### `a || b` --- class: middle ### Which one is better? .small[ ```C if (number < 100000 && is_prime(number)) { : } ``` ] .small[ ```C if (is_prime(number) && number < 100000) { : } ``` ] --- class: middle,center ## Assertion ``` // { ... } ``` A logical expression that must always be true
at a given point in a program. --- class: middle ```C x = 1; // { x == 1 } ``` --- class: middle ```C if (x > y) { max = x; } else { max = y; } ``` how can we be sure that `max` is now the larger of the two? --- class: middle ```C if (x > y) { max = x; } else { max = y; } ``` --- class: middle .tiny[ ```C if (score >= 8) { cs1010_println_string("A"); } else { if (score >= 5) { cs1010_println_string("B"); } else { if (score >= 3) { cs1010_println_string("C"); } else { cs1010_println_string("D"); } } } ``` ] When will `C` gets printed? --- class: middle .tiny[ ```C if (score >= 8) { cs1010_println_string("A"); } else { if (score >= 5) { cs1010_println_string("B"); } else { if (score >= 3) { cs1010_println_string("C"); } else { cs1010_println_string("D"); } } } ``` ] When will `C` gets printed? --- class: middle ## Homework - **Tutorial 2**: Problem Sets 3 and 8 - **Lab 2**: Problem Set 5 - **Quiz** as usual - **Exercises 0 and 1** --- class: bottom .tiny[ Version: 1.1 Last Updated: Mon Aug 30 11:05:03 +08 2021 ]