Upload
bennett-andrews
View
220
Download
3
Embed Size (px)
Citation preview
Agenda
Exam #1Review ModulusConditionalsBoolean AlgebraReading: Chapter 4.1-4.6Homework #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.
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
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
Use Flow Charts to understand Program Flow
DogFlowChart.jpg
If statement
In C++, you write an if statement like this:
if (condition) {
// do something
}
If-Else statement
if (condition) {
// do something
} else {
// do something else
}
Chained conditionalsif (condition) {
// do something
} else if (another condition) {
// do something different
} else if (another condition) {
// do another something
} else {
// do something else
}
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.
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.
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?
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.
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) {…}
Comparing data types
You can only compare an expression to another expression with the same data type.
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
}
}
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.
User Input Validation
int myNum;
cin >> myNum;
if (cin.good()) {
//Input was valid
} else {
//Input was invalid
}
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
Homework #5
Posted online