Welcome to Lesson 7! By the end of today's class, you should know...
Announcements
ReviewWith a partner, answer the following questions:
char letter1 = 67; char letter2 = 111; char letter3 = 108; char punctuation = 33; cout << letter1 << letter2 << letter2 << letter3 << punctuation;
cout << "39" + 2; Now, alter the above statement to make it legal (several possible changes you could make).
Thank you for your order, Shinshin! (Note: pay careful attention to the two punctuation marks in the previous sentence) string grateful = "Thank you for your order"; string name = "Shinshin"; string punctuation = "!"; string message = ???????? Strings ContinuedString Functions
Some Commonly-Used Functions
Example Using String Functions
Activity 7.1: My Name 2 (10pts)
Please enter your first and last name:
cout << "The length of your first name is " << first_name.length() << " letters\n";
string initials;
initials = ??????? + ???????; //How do you call the substr() function to get the initials
cout << "Your initials are: " << initials << endl;
The output of your program should look like this: Hi! I want to learn your name. Please enter your first and last name: Jennifer Parrish Nice to meet you Jennifer Parrish! The length of your first name is 8 letters The length of your last name is 7 letters The length of your full name is 16 letters //Please fix this line before you submit Your initials are: JP Output of Hard-to-Print Characters
Escape Sequences
Introducing If Statements
Teaching Computers to Make Decisions
Video: Bill Gates explains If statementsThe Value of a Relationship
Relational Operators
Using
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
using namespace std;
int main() {
int guess = 0;
cout << "I'm thinking of a number between"
<< " 1 and 10.\nCan you guess it?\n\n"
<< "Enter your guess: ";
cin >> guess;
if (7 == guess) {
cout << "*** Correct! ***" << endl;
}
return 0;
}
|
About those Curly Braces
- Technically, the
if
statement affects only the single statement that follows - We use curly braces to make that one statement into a block of statements
- This allows us to put any number of statements within the body
- Curly braces are not always required, but the best practice is to always include them
Incorrect curly braces:
int main()
{
int x = 3;
if(x < 5)
{
cout << x; //NO!! You need to indent twice. Statement inside 2 sets of curly braces.
}
}
{
int x = 3;
if(x < 5)
{
cout << x; //NO!! You need to indent twice. Statement inside 2 sets of curly braces.
}
}
Correct curly braces:
int main()
{
int x = 3;
if(x < 5)
{
cout << x; //Yes!! Indent twice. Statement inside 2 sets of curly braces
}
}
Which type of programmer are you?int main()
{
int x = 3;
if(x < 5)
{
cout << x; //Yes!! Indent twice. Statement inside 2 sets of curly braces
}
}
Group Activity: Is Your Number Even?
- One of the principle uses of the modulus operator is determine whether a number is even or odd.
- What is the difference between an even and odd number?
- How can we use division to figure out if a number is even or odd?
- Let's write a program to test a number to determine whether it is even or odd.
- Open up Eclipse and create a new project called Even with a file called even.cpp.
- At the top of your program, add a block comment with your names and section information:
/*
* Name 1
* Name 2
* Section Info
*/
- Create your main function, and include any libraries and the standard namespace.
- Now, between the curly braces of main declare an integer variable named testNum. Then declare a second integer variable to store the remainder. Call this variable remainder.
int testNum;
int remainder;
- Next, let's add a cout statement to welcome the user to our program and explain its objective.
cout
<< "Welcome! Enter a number and I will tell you whether it is
even\n\n";
- Run your program to verify it is working properly. If not, ask the instructor or another student for help.
- Let's prompt the user to enter a whole number.
cout << "Please enter a whole number: ";
- Next, we need to capture the user input and store it in the testNum variable. Add a cin statement to your program.
cin >> testNum;
- Now, we will get a chance to try out the modulus operator! Add the following statement to your program.
remainder = testNum % 2; //pronounced testNum "mod" two
- What does the above statement do?
- Below
the line where you calculate the remainder, add the following if
statement to determine whether the number is even, and, if so, report
your result to the user.
if (remainder == 0 ) {
cout << testNum << " is even.\n";
}
- Run your code and test your program. What do you think? Is this program satisfactory?
- Hold onto this program. We will improve upon it in a second.
Using if-else
Statements
- Sometimes we want to choose between two actions
- If a condition is true
- then do this
- Otherwise it is false
- so do something else
- To make this type of selection we use an
if...else
statement - Syntax:
if (test) { statements1 } else { statements2 }
- Where:
- test: the test condition to evaluate
- statementsX: the statements to execute depending on the test
(from http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm)
- For example:
if (7 == guess) { cout << "*** Correct! ***\n"; } else { cout << "Sorry, that is not correct.\n"; cout << "Try again.\n"; }
- ****Note that there is no test condition for the
else
clause**** - The decision on which set of statements to use depends on only one condition
- Note that you could write an if-else as a pair of complementary if statements instead, like:
if (7 == guess) { cout << "*** Correct! ***\n"; } if (7 != guess) { cout << "Sorry, that is not correct.\n"; cout << "Try again.\n"; }
- However, it is easier and clearer to write an
if-else
statement instead - For clarity, write the
if
andelse
parts on different lines than the other statements - Also, indent the other statements
- You can see an example of an
if-else
statement in the following example
Example Program With an if-else
Statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #include <iostream>
using namespace std;
int main() {
int guess = 0;
cout << "I'm thinking of a number between"
<< " 1 and 10.\nCan you guess it?\n\n"
<< "Enter your guess: ";
cin >> guess;
if (7 == guess) {
cout << "*** Correct! ***\n";
} else {
cout << "Sorry, that is not correct.\n";
cout << "Try again.\n";
}
return 0;
}
|
- Open up your program from the prior activity: Is Your Number Even?
- Now add an else clause to display when the number is odd.
- When your program is able to display both options (even and odd), submit it to Canvas.
- Both partners need to submit for full credit.
Wrap Up
With a partner, answer the following questions:
- Write a cout statement to print the following message to the console: Sure! I'll do it. <tab> I "love" boring, repetitive tasks!
- Find the mistake in the statements below. What will happen when the code is run?
number = 2;
if (number = 7)
if (number = 7)
cout << number;
- Assignment 7 due Thursday at 9:20pm on Canvas
- Lab 4 due Friday at midnight
- Quiz 3 next class
~See You Thursday!~