1 / 22

CS31: Introduction to Computer Science I

CS31: Introduction to Computer Science I. Discussion 1A 4/9/2010 Sungwon Yang swyang@cs.ucla.edu. 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

kris
Download Presentation

CS31: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang swyang@cs.ucla.edu

  2. 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 • // • /* ~~~~~~ */

  3. Some tips from the last class • What if want to use “” inside your text • Hello “Sungwon” !! • cout << “Hello “Sungwon” !!” << endl; • cout << “Hello \”Sungwon\” !!” << endl;

  4. 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

  5. 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;

  6. 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

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

  8. Boolean Expressions • Expression that evaluates to true or false • Wrong symbols: =>, =<, !< • We know the difference between = and == !

  9. Boolean Expressions • Valid Expressions • Invalid expressions x >= y x == y (x – y) < 10 x > y && y < z x = y x < 10 && > 5 5 < x < 10

  10. Quick questions • int x = 5, y = 6, z = 7; false x >= y false x == y false ( x – y ) > 10 x < y && y < z true (( x != y) || ( x > y )) && ( y == z) false true ( x != y) || (( x > y ) && ( y == z ))

  11. If statements • Syntax int x=5, y=1; if ( x > y ) { cout << x << endl; } cout << y << endl; … … if (Boolean Expression) { Yes Statement … … } Statement … … int x=1, y=5; if ( x > y ) { cout << x << endl; } cout << y << endl;

  12. 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; … … }

  13. If-else if-else statements • Choose among multiple alternative statements • Syntax 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; } if (Boolean Expression) { Statement … } else if (Boolean Expression) { Statement … } else if (Boolean Expression) { Statement … } else { Statement }

  14. Nested if-statement • If-statement inside if-statement if (Boolean Expression) { Statement … if (Boolean Expression) { statement … … } } if ( x > y ) { cout << y << endl; if ( x > z ) { cout << z << endl; } }

  15. 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;

  16. 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; } } }

  17. 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; }

  18. 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; }

  19. 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; else cout << "Please Enter Your Name" << endl; }

  20. 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); }

  21. 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. 10.00

  22. 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 }

More Related