+ All Categories
Home > Documents > CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang [email protected].

CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang [email protected].

Date post: 02-Jan-2016
Category:
Upload: lionel-willis
View: 220 times
Download: 0 times
Share this document with a friend
22
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang [email protected]
Transcript
Page 1: CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang swyang@cs.ucla.edu.

CS31: Introduction to Computer Science I

Discussion 1A 4/9/2010

Sungwon [email protected]

Page 2: 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 • cout function, endl

– Get user input from keyboard: • cin function

– Variables• int and double type• Identifiers

– Comments• //• /* ~~~~~~ */

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

Some tips from the last class

• What if want to use “” inside your text– Hello “Sungwon” !!– cout << “Hello “Sungwon” !!” << endl;– cout << “Hello \”Sungwon\” !!” << endl;

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

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

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

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;

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

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

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

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

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

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 == !

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

Boolean Expressions

• Valid Expressions

• Invalid expressionsx = y

x < 10 && > 5

5 < x < 10

x >= y

x == y

(x – y) < 10

x > y && y < z

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

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

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

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;

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

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;……

}

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

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

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

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

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

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;

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

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

}}

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

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;

}

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

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;

}

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

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;

}

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

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

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

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.

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

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}


Recommended