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
  • Loops
  • for loop
  • while loop
  • do-while loop
  • Loop Invariant
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lecture

Lec 05 - Loops

PreviousLec 04 - ConditionalsNextLec 06 - Call Stacks, Arrays

Last updated 6 months ago

Slides:

Loops

for loop

The for loop in C has the following syntax:

for (<initialize>; <condition>; <update>) {
    <body>
}

while loop

The while loop looks like this:

<initialize>
while (<condition>) {
    <body>
    <update>
}

Note that there is no restriction <update> must execute after <body> in a while loop. But in a for loop, <udpate> is always executed after <body>.

do-while loop

The do-while loop looks similar to the while loop, except that the body of the loop is guaranteed to be executed at least once.

<initialize>
do {
    <body>
    <update>
} while (<condition>);

Similar to the while loop, the <body> component and <update> componenet in the loop do not have to be in order.

When doing paper questions about the loop, always think about whether it will become an infinite loop or not.

Loop Invariant

A loop invariant is an assertion that is true before the loop, after each iteration of the loop, and after the loop.

Argue that an invariant is true

  • it is true before entering the loop.

  • it is true at the end of the first iteration of the loop

  • if it is true at the end of the kkk-th iteration of the loop, then it is true at the end of the k+1k+1k+1-th iteration.

  • it is true when we exit the loop.

Loop invariant, however, is not unique. We can write down infinitely many loop invariants. A good invariant, however, will lead us to an assertion that we want to see (e.g., relating product to n). We can derive other invariants in our code (such as { n != 0 } below) that do not contribute to the reasoning of the behavior of our loop. Such invariants should be avoided.

In the past year final papers, you may see that the loop invariant doesn't need to contain exact expression, it can contain some formative words, like "m is the minimum element in the list", etc.

CS1010 AY24/25 S1 Lecture 5
Lecture Slides