CS31 Discussion 1D Winter19: week 3

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS150 Introduction to Computer Science 1
Chapter 5: Control Structures II (Repetition)
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4 Program Control Statements
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Control of flow We learned that default flow of instructions is sequential. Then, we learned how to control the flow using "if" and "switch." Now, we will.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
COMP Loop Statements Yi Hong May 21, 2015.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Repetitive Structures
Characters, Strings, and the cstring Library
Topic 4: Looping Statements
Review 1.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
CS161 Introduction to Computer Science
Characters, Strings, and the cstring Library
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27.
Announcements First Midterm
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
While Loops.
For & do/while Loops.
Counting Loops.
Module 4 Loops.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
C++ Programming Lecture 3 C++ Basics – Part I
Chapter 5: Control Structures II (Repetition)
Fundamental Programming
Reading from and Writing to Files
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1H Fall18: week 6
CS31 Discussion 1D Fall18: week 2
CS31 Discussion 1H Fall18: week 3
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Reading from and Writing to Files
Programming Fundamental
Presentation transcript:

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