csci 127: introduction to computer science › files › csci127s20lecture13.pdf · csci 127:...

113
CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12 May 2020 1 / 39

Upload: others

Post on 30-May-2020

22 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

CSci 127: Introduction to Computer Science

hunter.cuny.edu/csci

CSci 127 (Hunter) Lecture 13 12 May 2020 1 / 39

Page 2: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected] Discussion Board: on Blackboard, link on purple menu barI Drop-in tutoring (11am-6:30pm):

sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 3: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected] Discussion Board: on Blackboard, link on purple menu barI Drop-in tutoring (11am-6:30pm):

sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 4: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected] Discussion Board: on Blackboard, link on purple menu barI Drop-in tutoring (11am-6:30pm):

sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 5: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected]

I Discussion Board: on Blackboard, link on purple menu barI Drop-in tutoring (11am-6:30pm):

sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 6: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected] Discussion Board: on Blackboard, link on purple menu bar

I Drop-in tutoring (11am-6:30pm):sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 7: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected] Discussion Board: on Blackboard, link on purple menu barI Drop-in tutoring (11am-6:30pm):

sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 8: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Announcements

Final Exam next Monday 18 May (in 6 days!)

The day before the final exam make sure you have accessto the exam course on Gradescope (single exam assignment)Contact us right away if you don’t!

No Code Review this week

Online help available until Thursday May 14I Email: [email protected] Discussion Board: on Blackboard, link on purple menu barI Drop-in tutoring (11am-6:30pm):

sign in here: https://bit.ly/csci127Tutoringthen join the session here: https://bit.ly/csci127TutoringSession

CSci 127 (Hunter) Lecture 13 12 May 2020 2 / 39

Page 9: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?

The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 10: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.

Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 11: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?

First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 12: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.

Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 13: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.

Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 14: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.

Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 15: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 16: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Frequently Asked Questions

What’s the best way to study for the final exam?The final exam problems are variations on the homework, quizzes, lectureexamples, and lecture previews.Past exams (and answer keys) are on-line. Do 7-10 previous exams: allow 1 hourand work through, grade yourself, update note sheet, and repeat.

Why do you care about cheating?First: it gives unfair advantage & is immoral.Second: it degrades the quality of our students.Third: it’s a standard question on faculty references.Industry & graduate schools hate it: don’t want someone who falsifies work.

CSci 127 (Hunter) Lecture 13 12 May 2020 3 / 39

Page 17: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Today’s Topics

Recap: I/O & Definite Loops in C++

Conditionals in C++

Indefinite Loops in C++

Recap: C++ & Python

CSci 127 (Hunter) Lecture 13 12 May 2020 4 / 39

Page 18: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Today’s Topics

Recap: I/O & Definite Loops in C++

Conditionals in C++

Indefinite Loops in C++

Recap: C++ & Python

CSci 127 (Hunter) Lecture 13 12 May 2020 5 / 39

Page 19: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

CSci 127 (Hunter) Lecture 13 12 May 2020 6 / 39

Page 20: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 21: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 22: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables:

int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 23: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 24: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:

int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 25: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 26: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print:

cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 27: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 28: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input:

cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 29: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 30: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:

#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 31: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 32: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:

for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 33: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}

Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 34: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 35: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: Basic Form & I/O in C++

Efficient for systems programming.

Programs are organized in functions.

Must declare variables: int num;

Many types available:int, float, char, ...

To print: cout << "Hello!!";

To get input: cin >> num;

To use those I/O functions:#include <iostream>using namespace std;

Definite loops:for (i = 0; i < 10; i++) {...}Blocks of code uses ‘{’ and ’}’.

Commands generally end in ‘;’.

CSci 127 (Hunter) Lecture 13 12 May 2020 7 / 39

Page 36: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Today’s Topics

Recap: I/O & Definite Loops in C++

Conditionals in C++

Indefinite Loops in C++

Recap: C++ & Python

CSci 127 (Hunter) Lecture 13 12 May 2020 8 / 39

Page 37: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge::Predict what the following pieces of code will do:

CSci 127 (Hunter) Lecture 13 12 May 2020 9 / 39

Page 38: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

C++ Demo

(Demo with onlinegdb)

CSci 127 (Hunter) Lecture 13 12 May 2020 10 / 39

Page 39: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Conditionals

General format:

if ( logical expression ){

command1;...

}else if ( logical expression ){

command1;...

}else

{command1;...

}

CSci 127 (Hunter) Lecture 13 12 May 2020 11 / 39

Page 40: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Logical Operators in C++

Very similar, just different names: &&, ||, and !:

and (&&)

in1 in2 returns:False && False False

False && True False

True && False False

True && True True

or (||)

in1 in2 returns:False || False False

False || True True

True || False True

True || True True

not (!)

in1 returns:! False True

! True False

CSci 127 (Hunter) Lecture 13 12 May 2020 12 / 39

Page 41: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Logical Operators in C++

Very similar, just different names: &&, ||, and !:

and (&&)

in1 in2 returns:False && False False

False && True False

True && False False

True && True True

or (||)

in1 in2 returns:False || False False

False || True True

True || False True

True || True True

not (!)

in1 returns:! False True

! True False

CSci 127 (Hunter) Lecture 13 12 May 2020 12 / 39

Page 42: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Logical Operators in C++

Very similar, just different names: &&, ||, and !:

and (&&)

in1 in2 returns:False && False False

False && True False

True && False False

True && True True

or (||)

in1 in2 returns:False || False False

False || True True

True || False True

True || True True

not (!)

in1 returns:! False True

! True False

CSci 127 (Hunter) Lecture 13 12 May 2020 12 / 39

Page 43: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Logical Operators in C++

Very similar, just different names: &&, ||, and !:

and (&&)

in1 in2 returns:False && False False

False && True False

True && False False

True && True True

or (||)

in1 in2 returns:False || False False

False || True True

True || False True

True || True True

not (!)

in1 returns:! False True

! True False

CSci 127 (Hunter) Lecture 13 12 May 2020 12 / 39

Page 44: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Today’s Topics

Recap: I/O & Definite Loops in C++

Conditionals in C++

Indefinite Loops in C++

Recap: C++ & Python

CSci 127 (Hunter) Lecture 13 12 May 2020 13 / 39

Page 45: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:Predict what the following pieces of code will do:

CSci 127 (Hunter) Lecture 13 12 May 2020 14 / 39

Page 46: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

C++ Demo

(Demo with onlinegdb)

CSci 127 (Hunter) Lecture 13 12 May 2020 15 / 39

Page 47: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Indefinite Loops: while

General format:

while ( logical expression ){

command1;command2;command3;...

}

CSci 127 (Hunter) Lecture 13 12 May 2020 16 / 39

Page 48: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:Predict what the following piece of code will do:

CSci 127 (Hunter) Lecture 13 12 May 2020 17 / 39

Page 49: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

C++ Demo

(Demo with onlinegdb)

CSci 127 (Hunter) Lecture 13 12 May 2020 18 / 39

Page 50: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Indefinite Loops: while

General format:

while ( logical expression ){

command1;command2;command3;...

}

CSci 127 (Hunter) Lecture 13 12 May 2020 19 / 39

Page 51: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:Predict what the following pieces of code will do:

CSci 127 (Hunter) Lecture 13 12 May 2020 20 / 39

Page 52: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

C++ Demo

(Demo with onlinegdb)

CSci 127 (Hunter) Lecture 13 12 May 2020 21 / 39

Page 53: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Indefinite Loops: do-while

General format:

do

{command1;command2;command3;...

} while ( logical expression );

CSci 127 (Hunter) Lecture 13 12 May 2020 22 / 39

Page 54: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Today’s Topics

Recap: I/O & Definite Loops in C++

Conditionals in C++

Indefinite Loops in C++

Recap: C++ & Python

CSci 127 (Hunter) Lecture 13 12 May 2020 23 / 39

Page 55: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O:

cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 56: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...;

& cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 57: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 58: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:

for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 59: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}

Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 60: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:

if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 61: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}

Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 62: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:

while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 63: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Recap: C++ Control StructuresI/O: cin >> ...; & cout << ...;

Definite loops:for (i = 0; i < 10; i++)

{...

}Conditionals:if (logical expression){

...}else

{...

}Indefinite loops:while (logical expression){

...}

CSci 127 (Hunter) Lecture 13 12 May 2020 24 / 39

Page 64: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

Rewrite this program in Python:

#include <iostream>using namespace std;

int main()

{for (int i = 1; i < 50; i++)

{cout << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 25 / 39

Page 65: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 66: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 67: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 68: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{

for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 69: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 70: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 71: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in C++:

for i in range(2017, 2000, -2):

print("Year is", i)

#include <iostream>using namespace std;

int main()

{for (int i = 2017; i >= 2000; i=i-2)

{cout << "Year is" << i << endl;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 26 / 39

Page 72: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in Python:

#include <iostream>using namespace std;

int main()

{for (int i = 1; i < 50; i++)

{cout << i << endl;

}return 0;

}

for i in range(1, 50):

print(i)

CSci 127 (Hunter) Lecture 13 12 May 2020 27 / 39

Page 73: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in Python:

#include <iostream>using namespace std;

int main()

{for (int i = 1; i < 50; i++)

{cout << i << endl;

}return 0;

}

for i in range(1, 50):

print(i)

CSci 127 (Hunter) Lecture 13 12 May 2020 27 / 39

Page 74: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Definite Loops in Python & C++

Rewrite this program in Python:

#include <iostream>using namespace std;

int main()

{for (int i = 1; i < 50; i++)

{cout << i << endl;

}return 0;

}

for i in range(1, 50):

print(i)

CSci 127 (Hunter) Lecture 13 12 May 2020 27 / 39

Page 75: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

Write a C++ program that asks the user the number of times they plan toride transit this week. Your program should then print if it is cheaper to buysingle ride metro cards or 7-day unlimited card.(The 7-day card is $33.00, and the cost of single ride, with bonus, is $2.75).

CSci 127 (Hunter) Lecture 13 12 May 2020 28 / 39

Page 76: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 29 / 39

Page 77: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year") year = 2016

if TRUE and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 29 / 39

Page 78: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(not FALSE or (year % 400 == 0)):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 30 / 39

Page 79: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(not FALSE or (year % 400 == 0)):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 30 / 39

Page 80: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(TRUE or (year % 400 == 0)):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 31 / 39

Page 81: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(TRUE or (year % 400 == 0)):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 31 / 39

Page 82: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(TRUE or FALSE):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 32 / 39

Page 83: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(TRUE or FALSE):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 32 / 39

Page 84: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(TRUE or FALSE):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 33 / 39

Page 85: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE and \(TRUE):

print("Leap!!")

print("Year")

CSci 127 (Hunter) Lecture 13 12 May 2020 34 / 39

Page 86: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE:

print("Leap!!")

print("Year")

Prints: Leap!

Year

CSci 127 (Hunter) Lecture 13 12 May 2020 35 / 39

Page 87: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++

Python: what is the output?year = 2016

if year % 4 == 0 and \(not (year % 100 == 0) or (year % 400 == 0)):

print("Leap!!")

print("Year")

year = 2016

if TRUE:

print("Leap!!")

print("Year")

Prints: Leap!

Year

CSci 127 (Hunter) Lecture 13 12 May 2020 35 / 39

Page 88: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 89: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 90: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 91: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 92: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 93: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 94: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}

else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 95: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 96: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}

return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 97: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Conditionals in Python & C++Your program should then print if it is cheaper to buy single ride metro cards($2.75 per ride) or 7-day unlimited card ($33.00).

#include <iostream>using namespace std;

int main()

{int rides;

cout << "Enter number of rides:";

cin >> rides;

if (2.75 * rides < 33.00)

{cout << "Cheaper to buy single ride metro cards.\n";

}else

{cout << "Cheaper to buy 7-day unlimited card.\n";

}return 0;

}CSci 127 (Hunter) Lecture 13 12 May 2020 36 / 39

Page 98: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++

Write Python code that repeatedly prompts for a non-empty string.

Write C++ code that repeatedly prompts until an odd number is entered.

CSci 127 (Hunter) Lecture 13 12 May 2020 37 / 39

Page 99: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 100: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 101: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 102: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 103: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 104: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 105: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 106: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 107: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 108: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 109: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 110: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 111: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}

return 0;

}

CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 112: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Challenge:: Indefinite Loops in Python & C++Write Python code that repeatedly prompts for a non-empty string.

s = ""

while s == "":

s = input("Enter a non-empty string: ")

print("You entered: ", s)

Write C++ code that repeatedly prompts until an odd number is entered.

#include <iostream>using namespace std;

int main()

{int num = 0;

while (num % 2 == 0)

{cout << "Enter an odd number:";

cin >> num;

}return 0;

}CSci 127 (Hunter) Lecture 13 12 May 2020 38 / 39

Page 113: CSci 127: Introduction to Computer Science › files › csci127S20Lecture13.pdf · CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 13 12

Educational Psychology Study

If you have consented to participate in theEducational Psychology study,please fill in the 3-question survey

Clickable link also below the video.

Thank you for your participation!!!

CSci 127 (Hunter) Lecture 13 12 May 2020 39 / 39