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

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

Embed Size (px)

Citation preview

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

Agenda

Exam #1Review ModulusConditionalsBoolean AlgebraReading: Chapter 4.1-4.6Homework #5

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

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 2nd.

Negative numbers may not work the same in all languages

Division by 0 is still an issue.

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

How is the remainder operator useful?1) Maybe you are printing out tabular data and want to highlight

every 2nd row. If the current row's index is divisible by 2, the remainder will be 0. You can do the same for every 3rd row, or every 10th 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

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

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

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

Use Flow Charts to understand Program Flow

DogFlowChart.jpg

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

If statement

In C++, you write an if statement like this:

if (condition) {

// do something

}

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

If-Else statement

if (condition) {

// do something

} else {

// do something else

}

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

Chained conditionalsif (condition) {

// do something

} else if (another condition) {

// do something different

} else if (another condition) {

// do another something

} else {

// do something else

}

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

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.

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

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.

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

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?

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

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.

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

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) {…}

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

Comparing data types

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

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

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

}

}

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

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.

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

User Input Validation

int myNum;

cin >> myNum;

if (cin.good()) {

//Input was valid

} else {

//Input was invalid

}

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

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

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

Homework #5

Posted online