Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS150 Introduction to Computer Science 1

Similar presentations


Presentation on theme: "CS150 Introduction to Computer Science 1"— Presentation transcript:

1 CS150 Introduction to Computer Science 1
Announcements Midterm on Friday, October 17th, 2003 Quiz some time next week Assignment 3 is due on Wednesday, October 15th, 2003 I am almost done with your grading. I will hand it back to you today during the lab 4/17/2019 CS150 Introduction to Computer Science 1

2 CS150 Introduction to Computer Science 1
Do While Loops do { cout << “Enter a year:” << endl; cin >> year; } while (year < 0); 4/17/2019 CS150 Introduction to Computer Science 1

3 CS150 Introduction to Computer Science 1
When to use do while? When loop must execute at least once Perfect for data validation! Post-tested loop General format: do { statements; } while (condition is true); 4/17/2019 CS150 Introduction to Computer Science 1

4 CS150 Introduction to Computer Science 1
Example Write a program segment that takes as input a number between 5 and 10. Error proof the segment. 4/17/2019 CS150 Introduction to Computer Science 1

5 CS150 Introduction to Computer Science 1
What’s the output? m = 10; do { cout << m << endl; m = m - 3; } while (m > 0) 4/17/2019 CS150 Introduction to Computer Science 1

6 CS150 Introduction to Computer Science 1
Rewrite num = 10; while (num <= 100) { cout << num << endl; num += 10; } 4/17/2019 CS150 Introduction to Computer Science 1

7 CS150 Introduction to Computer Science 1
Rewrite for (n = 3; n > 0; n--) cout << n << “ squared is” << n*n << endl; 4/17/2019 CS150 Introduction to Computer Science 1

8 CS150 Introduction to Computer Science 1
Nested Loops A loop within a loop Can repeat multiple things within a loop Example: Read in 10 grades for each of 20 students 4/17/2019 CS150 Introduction to Computer Science 1

9 CS150 Introduction to Computer Science 1
Example cout << setw(12) << “i” << setw(6) << “j” << endl; for (int i = 0; i < 4; i++) { cout << “Outer” << setw(7)<< i; cout << endl; for (int j = 0; j < i; j++) cout << “ Inner” << setw(10); cout << j << endl; } 4/17/2019 CS150 Introduction to Computer Science 1

10 CS150 Introduction to Computer Science 1
Example cout << " "; for (int colhead=0; colhead <10; colhead++) cout << setw(3) << colhead; cout << endl; cout << " " << endl; for (int row = 0; row < 10; row++) { cout << setw(3) << row ; for (int col = 0; col < 10; col++) cout << setw(3) << row*col; } 4/17/2019 CS150 Introduction to Computer Science 1

11 CS150 Introduction to Computer Science 1
4/17/2019 CS150 Introduction to Computer Science 1

12 CS150 Introduction to Computer Science 1
4/17/2019 CS150 Introduction to Computer Science 1

13 CS150 Introduction to Computer Science 1
What’s the Output for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) cout << “*”; cout << endl; } 4/17/2019 CS150 Introduction to Computer Science 1

14 CS150 Introduction to Computer Science 1
* ** *** **** 4/17/2019 CS150 Introduction to Computer Science 1

15 CS150 Introduction to Computer Science 1
What’s the Output for (int i = m; i >0; i--) { for (int j = n; j>0; j--) cout << “*”; cout << endl; } 4/17/2019 CS150 Introduction to Computer Science 1

16 CS150 Introduction to Computer Science 1
***** 4/17/2019 CS150 Introduction to Computer Science 1

17 CS150 Introduction to Computer Science 1
Problem Write a program that outputs a rectangle of stars for any inputted width and height 4/17/2019 CS150 Introduction to Computer Science 1


Download ppt "CS150 Introduction to Computer Science 1"

Similar presentations


Ads by Google