CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Conditional Statement
  • Skipping else
  • Logical Expression
  • Logical Operators
  • Short-Circuiting
  • Assertion
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lecture

Lec 04 - Conditionals

PreviousLec 03 - Basic C ProgrammingNextLec 05 - Loops

Last updated 6 months ago

Slides:

Conditional Statement

Skipping else

We do not write an else after a return statement, since it is redundant. For example,

if (<a>)
  return <x>;
if (<b>)
  return <y>;

Logical Expression

Logical Operators

In C, we cannot chain the comparison operators together. For example, the following is prohibitted.

1995 <= birth_year <= 2005

Short-Circuiting

When evaluating the logical expressions that involve && and ||, C uses "short-circuiting". If the program already knows, for sure, that a logical expression is true or false, there is no need to continue the evaluation. The corresponding true or false value will be returned.

The two cases of shorting-circuiting used with logical operators:

  1. <a> || <b>: If <a> is true, <b> won't be evaluated and the expression will return true. Otherwise, it will evaluate <b>.

  2. <a> && <b>: If <a> is false, <b> won't be evaluated and the expression will return false. Otherwise, it will evaluate <b>.

Another reason to keep short-circuiting in mind is that the order of the logical expressions matter: we would want to put the logical expression that involves more work in the second half of the expression. Take the following example:

if (number < 100000 && is_prime(number)) {
    :
}

Checking whether a number is below 100,000 is easier than checking if a number is prime. So, we can skip checking for primality if the number is too big. Compare this to:

if (is_prime(number) && number < 100000) {
    :
}

Suppose number is a gigantic integer, then we would have spent lots of effort checking if number is a prime, only to find out that it is too big anyway!

Short-Circuiting can also be used to deal with the edge cases in array problem! This is done by checking the boundary first then check the content in the array. So, let's say if you are already out of bound, you won't access the content and trigue the "out of bound" warning!

Assertion

An assertion is a logical expression that must always be true for the program to be correct.

CS1010 AY24/25 S1 Lecture 4
Lecture Slides