Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter 4.1-4.6 Homework #5.

Similar presentations


Presentation on theme: "Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter 4.1-4.6 Homework #5."— Presentation transcript:

1 Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter 4.1-4.6 Homework #5

2 Modulus or Remainder: % You can think of this as the remainder of integer division 5 % 2 = 1 5 / 2 = 2 If the remainder of dividing two numbers is 0, the first number is divisible by the 2 nd. Negative numbers may not work the same in all languages Division by 0 is still an issue.

3 How is the remainder operator useful? 1) Maybe you are printing out tabular data and want to highlight every 2 nd row. If the current row's index is divisible by 2, the remainder will be 0. You can do the same for every 3 rd row, or every 10 th row...whatever 2) Extract the rightmost digit(s) from a number. 142 % 10 = 2 142 % 100 = 42 3) Time. If you add 4 hours to 9:00, you get 1:00. (9 + 4) % 12 = 1

4 Conditional / If statement Often you need to make decisions in a program based on user input, calculated values, etc. To do this, we use an if statement. Conceptually, that is: If some condition is met Then do this Else do something else

5 Use Flow Charts to understand Program Flow DogFlowChart.jpg

6 If statement In C++, you write an if statement like this: if (condition) { // do something }

7 If-Else statement if (condition) { // do something } else { // do something else }

8 Chained conditionals if (condition) { // do something } else if (another condition) { // do something different } else if (another condition) { // do another something } else { // do something else }

9 Chained conditionals As soon as a condition is met, program flow moves into the corresponding { … }. Once the statements inside the curly brackets are complete, program flow moves to the line after the entire if-elseif-else block.

10 Boolean Conditions Remember, boolean is true or false. Is 1 equal to 1? True. Is 1+1 equal to 2? True. Is 2 greater than 3? False.

11 Boolean Conditions These are C++ comparison operators that give you boolean values x == y // Does x equal y? x != y // Does x not equal y? x > y // Is x greater than y? x < y // Is x less than y? x >= y // Is x greater than or equal to y? x <= y // Is x less than or equal to y?

12 Equals vs Assignment = is for assignment == is for testing if two expressions are equal These are easy to confuse or typo. It is also VERY difficult to debug. If your program compiles but is not working the way you expect, this is one of the first things you should check.

13 Expressions in conditionals You are not limited to variables in conditions. You can use expressions. if ( sin(x) > 1.0) {…} if (x + 5 > 10) {…} if (x + 5 > y + 2) {…}

14 Comparing data types You can only compare an expression to another expression with the same data type.

15 Nested if statements Inside an if statement, you can put more code. You can also put if statements inside if statements! if (userSelection == 1) { if (option == 1) { // do stuff }

16 User Input Validation If you have asked your user to enter a number, they might not follow your instructions. They might enter letters instead. To confirm that the user has entered a valid number, there is a function you can use. cin.good() will return true if the input is valid and false if the input is invalid.

17 User Input Validation int myNum; cin >> myNum; if (cin.good()) { //Input was valid } else { //Input was invalid }

18 Practice 1) Write a program that gives the user options to choose from 2) Edit a program you already wrote to validate user input 3) Make a text based adventure game 4) Decide what to do based on the weather outside 5) Make a flow chart for anything you like and then write the code to fit it

19 Homework #5 Posted online


Download ppt "Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter 4.1-4.6 Homework #5."

Similar presentations


Ads by Google