CS205-2020 Spring - Lecture 5 PDF
CS205-2020 Spring - Lecture 5 PDF
Language
CS205 Spring
Feng Zheng
2020.03.19
Content
• Brief Review
• Loops
• Branching Statements
• Logical Expressions
• Summary
Brief Review
Content of Last Class
• Pointers
➢ Address of array
➢ new and delete operations
➢&tell
Loops and Relational
Expressions
Introducing for Loops
• Why needs loop operations?
➢ Perform repetitive tasks
➢ Most tasks have the same process
• Parts of a for Loop
➢ Setting a value initially
➢ Testing whether the loop should
continue
➢ Executing the loop actions - body
➢ Updating value(s) used for the test
for (initialization; test-expression; update-expression)
body;
Introducing for Loops
• Loops
➢ The loop performs initialization just once
➢ Test expression is a relational expression
➢ Test expression is evaluated before each loop cycle
➢ Update expression is evaluated at the end of the loop
• See program example 1
➢ Increment operator: ++ operator (i = i + 1;)
• See program example 2
➢ Decrement operator: -- operator (i = i - 1;)
More Examples
• See program example 3
➢ Factorial definition
✓ Zero factorial, written as 0!, is defined to be 1 (exclamation marks!)
✓ The factorial of each integer being the product of that integer with the
preceding factorial
• See program example 4
➢ Changing the step size
➢ Use the ofstream object in the same manner you would use cout