CS31 Discussion 1D Winter19: week 3 TA: Behnam Shahbazi bshahbazi@cs.ucla.edu Credit to former TAs: Chelsea Ju and Bo-Jhang Ho
for ( ; ; ) { } Syntax of for loop Execution flow: Initial value Execution condition Increment value Line 1 Line 2 Line 3 Line 4 Line 5 } Execution flow: Initial Condition Block Increment Condition Block Increment Condition Block Increment Condition Block Increment Condition
Exercise: summation program int main() { int i; int sum = 0; for (i = 1; i <= 5; i++) sum += i; cout << sum << endl; cout << i << endl; return 0; } Initial value Execution condition Increment value Block Result?
while ( ) { } Syntax of while loop Execution flow: Execution condition Line 1 Line 2 Line 3 Line 4 Line 5 } Execution flow: Condition Block Condition Block Condition Block Condition
Exercise: Counting number of trailing zeros in an integer For example: Input 27000, output 3 Input 290, output 1 Input 10000000, output 7 Input 123, output 0
Exercise: Counting number of trailing zeros in an integer int main() { int num; cout << "Enter a number: "; cin >> num; int count = 0; while (num % 10 == 0) { num /= 10; count++; } cout << count << " trailing zeros"; return 0; } Execution condition Block What’s the result when input is 27000? What’s the result when input is 290?
Syntax of do-while loop Line 1 Line 2 Line 3 Line 4 Line 5 } while ( ); Execution condition Important reminder: Don’t forget the semicolon in the end of while clause! Execution flow: Block Condition Block Condition Block Condition
Exercise: Guessing number game int main() { int target = 15; int num; do { cout << "Enter a number: "; cin >> num; } while (num != target); cout << "You guess is correct!"; return 0; } Execution condition Block Discussion: Why do I put != instead of == in the while condition?
Comparison of 3 types of loop for ( ; ; ) { Initial Condition Increment For When the task is sequential A typical usage is for counting Line 1 Line 2 } while ( ) { Condition While We don’t know how many times we have to repeat the block, but know when to repeat (or stop) Line 1 Line 2 } do { Do-while Similar to while, but if we must execute the block at least once Don’t forget the semicolon! Line 1 Line 2 } while ( ); Condition
Is it possible to replace one type of loop to another? Technically we can But it doesn’t make any sense The 3 kinds of loop fit into different scenario
Loop replacement For -> While for ( ; ; ) { while ( ) { } } Initial Condition Increment Line 1 Line 2 } }
Loop replacement For -> While for ( ; ; ) { while ( ) { } } Initial Condition Increment Condition Line 1 Line 2 Line 1 Line 2 } }
Loop replacement For -> While for ( ; ; ) { while ( ) { } } Initial Condition Increment Condition Line 1 Line 2 Line 1 Line 2 } Increment }
Loop replacement While -> For for ( ; ; ) { while ( ) { } } Condition Line 1 Line 2 } }
Loop replacement While -> For for ( ; ; ) { while ( ) { } } Condition Condition Line 1 Line 2 Line 1 Line 2 } }
Nested loops: Printing a rectangle int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; }
Nested loops: Printing a triangle int main() { int size; cout << "Enter size: "; cin >> size; for (int r = 1; r <= size; r++) { for (int c = 1; c <= r; c++) { cout << "*"; } cout << endl; } return 0; }
Take-home exercise: Can you print triangles in different angle? * ** *** **** ***** * ** *** **** ***** Done! ***** **** *** ** * ***** **** *** ** *
String demystify: (1) Get the length int main() { string s = "Hello"; cout << "Length: " << s.size() << endl; return 0; } Result? Length: 5
String demystify: (2) Dump all the characters int main() { string s = "Hello"; for (int i = 0; i < s.size(); i++) cout << "index " << i << ": " << s[i] << endl; return 0; } Result? index 0: H index 1: e index 2: l index 3: l index 4: o Variable s is a string s[<a number>] Get the character with the corresponding index The type is a character
String demystify: (3) Dump all the characters int main() { string s = "Hello"; int count = 0; for (int i = 0; i < s.size(); i++) if (s[i] == 'e') count++; cout << "Number of Es: " << count << endl; return 0; } Result? Number of Es: 1 ‘e’ is a character literal
More exercises Compute factorization, e.g., 7! Compute combination number, e.g., 10 choose 6 List all the prime numbers up to 100 Great common divisor of two numbers
Puzzle time: What does the code print? int main() { int n = 5; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << (i / j) * (j / i); cout << endl; } return 0; }
Puzzle time: What does the code print? int main() { int n = 6; for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) cout << (i / j) * (j / i); cout << endl; } return 0; }
What is a function Take some numbers, produce a result value int double double string Take some numbers, produce a result value
What is a function f(x) = 2x + 3 What is f(5)?
What is a function f(x, y) = 3x + 2y - xy What is f(4, 1)?
What is a function What is the output? int square(int n) { return n * n; } int main() { int a = 3; int b = 6; a = a * b + square(a + b); cout << a << endl; return 0; } What is the output?
What is a function int square(int n); int main() { int a = 3; int b = 6; a = a * b + square(a + b); cout << a << endl; return 0; } int square(int n) { return n * n; } When a function is called, it always look back and try to find the function. If you want to declare it before the callee, you have to declare it first.
Don’t memorize them! ASCII Characters #include <cctype> Some functions in this library: int isalnum(int c); // alphanumeric int isdigit(int c); // decimal digit int islower(int c); // lowercase letter int isupper(int c); // uppercase letter int isspace(int c); // white-space int toupper(int c); // convert to uppercase int tolower(int c); // convert to lowercase Don’t memorize them!
#include <cctype> example
Worksheet 2 Let’s team up and work on worksheet 2