111/18/2015cs150 introduction to computer science 1 announcements i have not graded your exam yet
TRANSCRIPT
![Page 1: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/1.jpg)
04/21/23 CS150 Introduction to Computer Science 1
1
Announcements
I have not graded your exam yet
![Page 2: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/2.jpg)
04/21/23 CS150 Introduction to Computer Science 1
2
Multiple Alternative Ifs
if (condition1)
statement1;
else if (condition2)
statement2;
…
else
defaultstatement;
![Page 3: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/3.jpg)
04/21/23 CS150 Introduction to Computer Science 1
3
Program
Write a program that displays a letter grade corresponding to an exam score90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
0-59 F
![Page 4: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/4.jpg)
04/21/23 CS150 Introduction to Computer Science 1
4
Example
if (salary < 0.00)
tax = -1;
else if (salary < 15000.00)
tax = 0.15 * salary;
else if (salary < 30000.00)
tax = (salary - 15000.00)*0.16 + 2250.00;
else
tax = salary * 0.26;
![Page 5: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/5.jpg)
04/21/23 CS150 Introduction to Computer Science 1
5
What’s the difference?
if (x >= 0)
x = x + 1;
else if (x >= 1)
x = x + 2;
if (x >= 0)
x = x + 1;
if (x >= 1)
x = x + 2;
![Page 6: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/6.jpg)
04/21/23 CS150 Introduction to Computer Science 1
6
Selection Structure Review
Write a program to solve the following problem:A police department needs to calculate fees for speeding tickets.
The fees are as follows:Speed Fee
75 or greater $60.00
51 - 74 $40.00
36 - 50 $20.00
![Page 7: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/7.jpg)
04/21/23 CS150 Introduction to Computer Science 1
7
Switch Statements
Another form of selection statement
Similar to if’s
Useful for lots of alternatives
![Page 8: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/8.jpg)
04/21/23 CS150 Introduction to Computer Science 1
8
Example
switch (watts){
case 25: life = 2500;
break;case 40:case 60:
life = 1000;break;
case 75:case 100:
life = 750;break;
default:life = 0;
}
![Page 9: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/9.jpg)
04/21/23 CS150 Introduction to Computer Science 1
9
Form
switch (selector){case label1: statements1;
break;case label2: statements2;
break;…case labeln: statementsn;
break;default: statements;
}
![Page 10: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/10.jpg)
04/21/23 CS150 Introduction to Computer Science 1
10
Example
switch (musical_note)
{
case ‘c’:
cout << “do” << endl;
break;
case ‘d’:
cout << “re” << endl;
break;
case ‘e’:
cout << “mi” << endl;
break;
case ‘f’:
cout << “fa” << endl;
break;
case ‘g’:
cout << “sol” << endl;
break;
case ‘a’:
cout << “la” << endl;
break;
case ‘b’:
cout << “ti” << endl;
break;
default:
cout << “An invalid note was read.”;
}
![Page 11: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/11.jpg)
04/21/23 CS150 Introduction to Computer Science 1
11
Important!
Selector must be ordinal type
Each possible value is a separate case
break stops statements for case, otherwise continue with statements for next case
![Page 12: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/12.jpg)
04/21/23 CS150 Introduction to Computer Science 1
12
Example
switch (color){case ‘R’: case ‘r’:
cout << “red” << endl;case ‘B’: case ‘b’:
cout << “blue” << endl; case ‘Y’: case ‘y’:
cout << “yellow” << endl;}
What happens when color is ‘r’? ‘B’? ‘Y’?
![Page 13: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/13.jpg)
04/21/23 CS150 Introduction to Computer Science 1
13
Example
switch (x > y){case 1:
cout << “x greater” << endl;break;
case 0:cout << “y greater or equal” << endl;break;
}
Write as if statement
![Page 14: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/14.jpg)
04/21/23 CS150 Introduction to Computer Science 1
14
Questions
Can you write any switch statement as an if?
Can you write any if statement as a switch?
![Page 15: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/15.jpg)
04/21/23 CS150 Introduction to Computer Science 1
15
Problem
Write a switch statement to convert a character digit to an integer
![Page 16: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/16.jpg)
04/21/23 CS150 Introduction to Computer Science 1
16
Change to switch
if (speed > 35)
fee = 20.00;
else if (speed > 50)
fee = 40.00;
else if (speed > 75)
fee = 60.00;
![Page 17: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/17.jpg)
04/21/23 CS150 Introduction to Computer Science 1
17
Examples
Write an if statement that prints out the level of schooling. (0, none; 1 through 6, elementary; 7 through 8, middle school; 9 through 12, high school; > 12, college)
Write a switch statement to do the same
![Page 18: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/18.jpg)
04/21/23 CS150 Introduction to Computer Science 1
18
Write a Program
Input an integer
If the integer is positive, increment a variable poscount by 1.
If the integer is negative, increment a variable negcount by 1.
If neither, increment zerocount by 1.
![Page 19: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/19.jpg)
04/21/23 CS150 Introduction to Computer Science 1
19
Unary Operators ++ and --
++ is increment operatorx++;
is the same as x = x + 1;
-- is decrement operatorx--;
is the same as x = x - 1;
![Page 20: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/20.jpg)
04/21/23 CS150 Introduction to Computer Science 1
20
Prefix and PostfixUnary ++ and --Prefix Postfix
k = --x; k =x--;
k = ++x; k = x++;
Increment/ Assign value of x to
decrement x k, then increment
then assign or decrement x
value of x to k
![Page 21: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/21.jpg)
04/21/23 CS150 Introduction to Computer Science 1
21
Example
cout << “Value of i is” << i;
cout << “Value of i++ is” << i++;
cout << “Value of ++i is” << ++i;
cout << “Value of --i is” << --i;
cout << “Value of i-- is” << i--;
![Page 22: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/22.jpg)
04/21/23 CS150 Introduction to Computer Science 1
22
Program
Write a program that outputs the following:
*****
*****
*****
*****
*****
![Page 23: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/23.jpg)
04/21/23 CS150 Introduction to Computer Science 1
23
count = 0;
while (count < 5)
{
cout << “*****” << endl;
count++;
}
How many times (iterations) does loop run?
Loops
Loop Control Variable
Initialize LCV
Change the value of count
![Page 24: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/24.jpg)
04/21/23 CS150 Introduction to Computer Science 1
24
While loopswhile (logical expression is true)
statement;
while (logical expression is true)
{
statement1;
statement2;
…
}
![Page 25: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/25.jpg)
04/21/23 CS150 Introduction to Computer Science 1
25
Key ingredients Initialize
MUST initialize loop control variable
TestThe value of loop control variable is tested during each
iteration of loop
UpdateLoop control variable is changed during each loop iteration
If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely.
![Page 26: 111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet](https://reader035.vdocuments.us/reader035/viewer/2022070411/56649f2e5503460f94c483e4/html5/thumbnails/26.jpg)
04/21/23 CS150 Introduction to Computer Science 1
26
Examples
Write a while loop that outputs each integer from 1 to 5 Write a program that inputs the following data for 5
students: name, id# and grade Write a program that inputs the following data for a user
specified number of students: name, id# and grade