Upload
lionel-willis
View
221
Download
0
Embed Size (px)
Citation preview
Quick Review• What did we learn last week?
– What is programming• translator
– Basic grammar of C++• #include, int main(), case sensitive, semi-colon, etc…
– Print out on the screen • cout function, endl
– Get user input from keyboard: • cin function
– Variables• int and double type• Identifiers
– Comments• //• /* ~~~~~~ */
Some tips from the last class
• What if want to use “” inside your text– Hello “Sungwon” !!– cout << “Hello “Sungwon” !!” << endl;– cout << “Hello \”Sungwon\” !!” << endl;
Expressions• Arithmetic expressions (x, y, z variables)
– Addition: z = x + y;– Subtraction: z = x – y;– Multiplication: z = x * y;– Division: z = x / y;– Modulo(remainder): z = x % y;
• The evaluation precedence is similar to algebra– Multiplication, division, modulo addition, subtraction
• You can use parenthesis to yield precedence– x + y * z– ( x + y ) * z
• Number(variable) types determine the type of results– If numbers(variables) are int(double) type: the result is int(double)
• cout << 5 / 2 << endl; 2• cout << 5.0 / 2.0 << endl; 2.5
– If at least one number(variable) is double: the result is double• cout << 5 / 2.0 << endl; 2.5
– What if assign the result to a variable?• int x = 5 / 2.0; cout << x << endl; 2
More arithmetic expressions• Equivalent expressions– x = x + 5; x += 5;– x -= 5; x *= 5; x /= 5;– z = z * (x + 2); z *= x + 2;
• Increment & decrement– x = x + 1; x += 1; x++; ++x;– y = y – 1; y -= 1; y--; --y;
• ++ and -- behave differently depending on their locations– x = 5;
y = ++x;cout << y << endl;
x = 5;y = x++;cout << y << endl;
Quick questions
• What will the output be?int x = 5, y = 11, z = 3;cout << x + y / z << endl; 8
int x = 9, y = 5, z = 3;cout << x % y * z << endl; 12
int x = 9, y = 5, z = 3;cout << z * x % y << endl; 2
int x = 1, y = 2, z = 3;cout << x++ * y * --z << endl; 4
int x = 1, y = 2, z = 3;x += (y + x) * z;cout << x << endl;
10
Boolean Expressions
• bool Type Variable– True (1) or False (0)• bool test = true;• bool test = 1;• Bool test = 0;• bool test = 1 < 2;• bool test = x < 5;
– If x is 3, true– If x is 10, false
Boolean Expressions
• Expression that evaluates to true or falseSymbol Meaning
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
&& AND
|| OR
• Wrong symbols: =>, =<, !<• We know the difference between = and == !
Boolean Expressions
• Valid Expressions
• Invalid expressionsx = y
x < 10 && > 5
5 < x < 10
x >= y
x == y
(x – y) < 10
x > y && y < z
Quick questions
• int x = 5, y = 6, z = 7;x >= y false
x == y false
( x – y ) > 10 false
x < y && y < z true
(( x != y) || ( x > y )) && ( y == z) false
( x != y) || (( x > y ) && ( y == z )) true
If statements
• Syntax……if (Boolean Expression){
Yes Statement……
}Statement……
int x=5, y=1;
if ( x > y ){
cout << x << endl;}cout << y << endl;
int x=1, y=5;
if ( x > y ){
cout << x << endl;}cout << y << endl;
If-else statements
• Choose between two alternative statements• Syntax
if (Boolean Expression){
Yes Statement……
}else{
No Statements……
}
if ( x > y ){
cout << x << endl;……
}else{
cout << y << endl;……
}
If-else if-else statements• Choose among multiple alternative statements
• Syntaxif (Boolean Expression){
Statement…
}else if (Boolean Expression){
Statement…
}else if (Boolean Expression){
Statement…
}else{
Statement}
if ( x > y ){
cout << x << endl;}else if ( x == y ){
cout << y << endl;}else if ( x <= y ){
cout << x-y << endl;}else {
cout << x+y << endl;}
Nested if-statement
• If-statement inside if-statementif (Boolean Expression){ Statement …
if (Boolean Expression) { statement … … }}
if ( x > y ){ cout << y << endl;
if ( x > z ) { cout << z << endl; }}
If-statement
• You can omit braces if your statement is single line
if ( x > y ){ cout << y << endl;
if ( x > z ) { cout << z << endl; }}
if ( x > y ){ cout << y << endl;
if ( x > z ) cout << z << endl;}
if ( x > y ) cout << y << endl;
if ( x > z ) cout << z << endl;
Quick questions#include <iostream>using namespace std;
int main(){
int age = 30, height = 170, weight = 70, result = 0;if ( height / weight != 2 )
result += 2;else
result++;if ( age >= 30 ){
++result;if ( height < 150 || weight == 70 ){
result *= 5;if ( result > 10 )
cout << result << endl;else if ( result < 10 )
cout << --result << endl;else
cout << ++result << endl;}
}}
String
• A variable type for “text”• #include < string>
#include <iostream>#include <string>using namespace std;
int main(){
string str = "Hello! Sungwon";
cout << "Hello! Sungwon" << endl;cout << str << endl;
}
Get a line of “text” from user
• getline( cin, variable)• Space, tab, and enter are handled as text
#include <iostream>#include <string>using namespace std;
int main(){
string str = “Hello”;string myName = “”;
cout << “What is your name?" << endl;getline ( cin, myName);cout << str << endl;;cout << myName << endl;
}
Checking string• if ( string == “text” )• If ( string == “” )• If ( sting != “” )
#include <iostream>#include <string>using namespace std;
int main(){
string myName ;
cout << “What is your name?" << endl;getline ( cin, myName);
if ( myName != "" )cout << "Hello " << myName << endl;
elsecout << "Please Enter Your Name" << endl;
}
cin.ignore();• getline ( cin, variable ) after cin >> variable
int main(){
cout << "How many place settings would you like to buy? ";int numberOfSettings;
cin >> numberOfSettings;
cout << "In which style? "; string style; getline(cin, style);}
int main(){
cout << "How many place settings would you like to buy? ";int numberOfSettings;
cin >> numberOfSettings;cin.ignore(100000, '\n');
cout << "In which style? "; string style; getline(cin, style);}
Set the digit after decimal points
#include <iostream>using namespace std;
int main(){
double x = 10.0;
cout.setf(ios::fixed); cout.setf(ios::showpoint);cout.precision(2);
cout << x << endl;}
#include <iostream>using namespace std;
int main(){
double x = 10.0;
cout.setf(ios::fixed); cout.setf(ios::showpoint);cout.precision(0);
cout << x << endl;}
10.00 10.
Variables vs Constants
• We can assign new values into variables– Values of variable can be changed at any time– But, we can declare values cannot be changed
int main(){
int x = 0;cout << x << endl;
x = 10;cout << x << endl;
x *= 5;cout << x << endl;
}
int main(){
const double PI = 3.14159;cout << PI << endl;
PI = 3.5; // this is error}