Download presentation
Presentation is loading. Please wait.
Published byJacob Simon Modified over 9 years ago
1
1 CS161 Introduction to Computer Science Topic #8
2
CS161 Topic #82 Today in CS161 Review for Midterm –Variable Definitions –Conditional Expressions (if, else) –Loops (while, do while, for) –Sample Questions Switch Statements Questions?
3
CS161 Topic #83 Review for Midterm The midterm is a closed book and closed notes exam There will be questions asking you to determine the output of a program, indicate what is wrong with a program, evaluate conditional expressions, and write program fragments.
4
CS161 Topic #84 Understand these concepts: The difference between designing an algorithm and implementing C++ code. The basic data types: int, float, and char. The difference between dividing integers versus floating point numbers. The if/else control structures. The difference between relational operators, equality operators, and logical operators. The while, do-while, for loops
5
CS161 Topic #85 Understand these concepts: What does a loop allow us to do? Where do we put loops in our program? What is a C++ statement? How do you read information from the keyboard? How do you write information to the screen? Why would you use a while loop rather than a do- while loop?
6
CS161 Topic #86 Sample Questions: The following is supposed to output all positive odd numbers less than 10. It contains some errors. What are they and how can they be corrected? int x = 1; while (x != 10) { x += 2; cout << x << endl; }
7
CS161 Topic #87 Practice Questions.... Write a for loop to output all positive odd numbers less than 10, starting at 1 int i;//loop control variable for (i = 1; i < 10; i = i + 2) cout << i;
8
CS161 Topic #88 Sample Questions: What is the output of the following program fragment? for (i = -1; i <= 5; i = i + 1) cout << 2*i; cout << endl; How would you fix the appearance of the output? cout << 2*i << ‘\n’; or, cout << setw(5) << 2*i;
9
CS161 Topic #89 Sample Questions: Change the following while loop to a do- while loop:int i;cin >> i; while (i < 20) {if (i < 20) cout << i << ‘ ‘;do { i += 5; cout << i << ‘ ‘; } i += 5; } while (i < 20); answer:
10
CS161 Topic #810 Understand these concepts: What data type would you use to store –your age –your gpa –your first name’s initial –a test score (A, B, C) Write C++ code to display each upper case letter of the alphabet
11
CS161 Topic #811 Understand these concepts: What will be the output for the following: if (x >= 0.0) if (x < 1000.0) { y = 2*x; if (x <= 300) x = x/10; } else y = 3*x; else y = x; cout << x << “ and “ << y << endl;
12
CS161 Topic #812 Understand these concepts: What will be the output for the following: for (k = 2; k <= 4; k = k + 1) { for (j = 5; j <= 8; j = j + 1) cout << k+j; cout << endl; }
13
CS161 Topic #813 Understand these concepts: What is the escape sequence that is the same as a carriage return? Where should we include comments? Name some examples of whitespace Write a cout statement to display your name Which of the following are not legal integers: -32.0+2562563,24032000 Explain why it is important to prompt
14
CS161 Topic #814 Understand these concepts: Write a small program to read in two integer values and then display them in numerical order, regardless of the order in which they are entered: int main() { int first, second; cout << “Enter 2 whole numbers: “; cin >> first >> second; if (first <= second) cout << first << “ “ << second << endl; else cout << second << “ “ << first << endl; return 0; }
15
CS161 Topic #815 Switch Statements Another C++ control statement is called the switch statement It allows you to pick the statements you want to execute from a list of possible statements, instead of just two different alternatives (as is available with an if/else) or a set of nested if/elses! It allows for multi-way decisions.
16
CS161 Topic #816 Switch Statements char grade; cout << "Enter the grade..." << endl; cin >> grade; switch (grade) { case 'A': cout << "Excellent" << endl; cout << “Keep up the good work!”; break; case 'B': cout << "Very Good"; break; case 'C': cout << "Passing"; break; case 'D': case 'F': cout << "Too Bad"; break; default : cout << "No match was found...try again"; break; }
17
CS161 Topic #817 Switch Statements C++ provides a "default" clause so that if there isn't a match something is done. If the default is left off...and there is no match...no action takes place at all. When a case statement is executed, the value of Grade is checked and then depending on which of the cases it matches -- the statement following the colon for that case will be executed.
18
CS161 Topic #818 Switch Statements To exit from a switch statement...use break. Unlike Pascal, with C++ once you have a match... It will fall thru (ignoring any additional case or default labels that are encountered and continue executing code until a break is encountered. If you intend to use “fall thru”, then you should have a comment saying that the drop through is intentional
19
CS161 Topic #819 Switch Statements The rule of thumb is that you can use these to switch on integers and characters. It is not permitted to use the switch with floating point types or a string of characters. The type of the expression following a switch keyword must be the same as the expressions following each case keyword....and no two expressions following the case keywords can be the same.
20
CS161 Topic #820 What Fall Thru means... int count; cout << "Please enter the number of asterisks:"; cin >> count; switch (count) {//these { } are mandatory! case 1: cout << "*"; //intentional drop thru case 2: cout << "**"; case 3: cout << "***"; case 4: cout << "****"; default: cout << "!"; } cout << endl;
21
CS161 Topic #821 The CORRECT version.... int count; cout << "Please enter the number of asterisks:"; cin >> count; switch (count) {//these { } are mandatory! case 1: cout << "*";break; case 2: cout << "**";break; case 3: cout << "***";break; case 4: cout << "****";break; default: cout << "!";break; } cout << endl;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.