Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang

Similar presentations


Presentation on theme: "CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang"— Presentation transcript:

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

2 Quick Review What did we learn last week? – Arithmetic Expressions Addition, Subtraction, Multiplication, Division, Modulo Evaluation precedence: left -> right, parenthesis Abbreviation: +=, -=, *=, /=, %= Increment & decrement: ++, -- – Boolean Expressions bool type: true or false >, =, <=, ==, !=, &&, || – IF statement Conditional statements – IF - ELSE statement Can choose one between two alternatives – IF - ELSE IF – ELSE statement Can choose one among multiple alternatives – Nested IF statement Embed IF statements in IF statement – String variables variable for “text” getline (cin, variable); cin.ignore(100000, '\n'); – Variables vs. Constants

3 switch statement Choose one among multiple choices switch (Controlling Expression) { case Constant_1: statement_1; … break; case Constant_2: statement_2; … break; default: statement_default; } cin >> grade; switch (grade) { case 1: cout << “Freshmen” <<endl; break; case 2: cout << “Sophomore” << endl; break; case 3: cout << “Junior” << endl; break; case 4: cout << “Senior” << endl; break; default: cout << “Wrong Input” << endl; }

4 WHILE Loops Repeat statements while condition holds while (Condition) { statements; … } int yourInput = 0; cin >> yourInput; while (yourInput > 0) { cout << yourInput << endl; yourInput--; } cout << “The End” << endl; 5 5 4 3 2 1 The End

5 DO-WHILE Loops Repeat statement while condition holds – Execute the statements once then test condition do { statements; … } while (Condition); int yourInput = 0; cin >> yourInput; do { cout << yourInput << endl; yourInput--; } while (yourInput > 0); cout << “The End” << endl; 5 5 4 3 2 1 The End

6 while vs. do-while int main() { int yourInput = 0, sum = 0; cin >> yourInput; while (yourInput > 0) { yourInput--; sum += yourInput; } cout << sum << endl; } int main() { int yourInput = 0, sum = 0; cin >> yourInput; do { yourInput--; sum += yourInput; } while (yourInput > 0); cout << sum << endl; } 5 0 10 0

7 Infinite loops Your loop will never end – Make sure that it ends at some point int main () { int x = 0, y = 0, sum = 0; while ( sum != 100 ) { sum = x + y; x += 1; y += 2; cout << sum << endl; } int main () { int x = 0, y = 0, sum = 0; while ( sum < 100 ) { sum = x + y; x += 1; y += 2; cout << sum << endl; }

8 FOR Loops for ( initialization ; condition ; update ) { statements; … } The order of execution 1.Initialization 2.Check the condition If true 3. Run the body (statements) 4. Do the update Go back to 2 If false Exit the for loop

9 Initialization in for loop Can declare initialization variable inside for loop – It is different from variables declared elsewhere int i = 0; for ( i = 0 ; i < 5 ; i++ ) { cout << i << endl; } cout << i << endl; int i = 0; for (int i = 0 ; i < 5 ; i++ ) { cout << i << end; } cout << i << endl; 012345012345 012340012340

10 Nested loop Embed loops into loops int main() { for (int i=0 ; i < 10 ; i++) { cout << i << ':'; for (int j=0 ; j < 10 ; j++) { cout << j << ','; } cout << endl; } 0:0,1,2,3,4,5,6,7,8,9, 1:0,1,2,3,4,5,6,7,8,9, 2:0,1,2,3,4,5,6,7,8,9, 3:0,1,2,3,4,5,6,7,8,9, 4:0,1,2,3,4,5,6,7,8,9, 5:0,1,2,3,4,5,6,7,8,9, 6:0,1,2,3,4,5,6,7,8,9, 7:0,1,2,3,4,5,6,7,8,9, 8:0,1,2,3,4,5,6,7,8,9, 9:0,1,2,3,4,5,6,7,8,9,

11 FOR to WHILE Conversion while loop for loop for ( initialization ; condition ; update ) { statements; … } initialization While ( condition ) { statements; … update }

12 Quick question int main() { int result = 0; for ( int i = 10; i >= 0; i--) { result += i; } cout << result << endl; } 55 int main() { int result = 0; for ( int i = 32; i > 1; i /= 2) { result += i; } cout << result << endl; } 62

13 Functions Sub-programs in your program Simplify multiple operations which are basically same Calculator Addition Subtraction Multiplication Division Your Program Functions

14 3 operations, but basically same job int main() { int result = 0; for ( int i = 1; i <= 10; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 50; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 100; i++) { result += i; } cout << result << endl; } int main() { int result = 0; result = sum ( 1, 10 ); cout << result << endl; result = 0; result = sum ( 1, 50 ); cout << result << endl; result = 0; result = sum ( 1, 100 ); cout << result << endl; }

15 Functions Define a function return_type function_name (argument_type_1 argument_name_1, argument_type_2 argumrnt_name_2, … ) { function_statements; … return return_value; } int sum (int begin, int end ) { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result;; }

16 Function definition int main() { int result = 0; for ( int i = 1; i <= 10; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 50; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 100; i++) { result += i; } cout << result << endl; } int sum (int begin, int end ) { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result; } int main() { int result = 0; cout << sum(1,10) << endl; result = sum(1,50); cout << result << endl; result = 0; cout << sum(1,100) << endl; }

17 Function declaration int sum (int begin, int end ) { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result;; } int main() { int result = 0; cout << sum(1,10) << endl; result = sum(1,50); cout << result << endl; result = 0; cout << sum(1,100) << endl; } int sum (int begin, int end); //proto type int main() { int result = 0; cout << sum(1,10) << endl; result = sum(1,50); cout << result << endl; result = 0; cout << sum(1,100) << endl; } int sum (int begin, int end ) //definition { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result; }

18 Function without arguments Functions can have no arguments string askName () { string name; cout << "What's your name?" << endl; getline (cin, name); return name; } int main() { string name; name = askName(); cout << "Hello!" << endl; cout << name << endl; cout << "Nice to meet you!" << endl; } What’s your name? Sungwon Hello! Sungwon Nice to meet you!

19 Void functions Functions can have no return values void greeting (string name ) { cout << “Hello!” << endl; cout << name << endl; cout << “Nice to meet you!” << endl; } int main() { string name; cout << “What’s your name?” << endl; getline (cin, name); greeting (name); } What’s your name? Sungwon Hello! Sungwon Nice to meet you!

20 End point of function return ends function In the case of void function, the last statement int larger (int arg1, int arg2 ) { if ( arg1 >= arg2 ) return arg1; else return arg2; } int main() { int first = 0, second = 0; cout << “Enter first number” << endl; cin >> first; cout << “Enter second number” << endl; cin >> second; cout << larger(first,second) << “is larger” << endl; }

21 Functions of string (Chap 9 in textbook) OperationWhat it doesExamples string s = “Hello”; string s2 = “!!!”; Declares strings s and s2 s.length() or s.size() Returns the length of scout << s.size();  5 s.at(i)Returns i-th character i must be an interger between 0 and size-1(inclusive) cout << s.at(0);  H cout << s.at(4);  o s.empty()Returns true if s is emptyIf (s.empty()) cout << “It is empty”; s + s2Concaternates two stringscout << s + s2;  Hello!!! s.substr(i,n) s.substr(i) Takes a substring of length n, starting from the i-th character cout << s.substr(2,2);  ll cout << s.substr(2);  llo s.replace( i, n, s2 )Replace a substring of length n starting from at i with another string s2, and set s with a new string s.replace (2,2,s2); cout << s;  He!!!o

22 Functions of character(Chap 9 in textbook) You need to include library: #include OperationsWhat it does char c;declare a character variable c isspace(c)true if c is a white space character (space, tab, newline) isalpha(c)true if c is a letter isalnum(c)true if c is a letter or digit islower(c)true if c is a lowercase letter isupper(c)true if c is a uppercase letter tolower(c)returns the lowercase version of c, if c is a letter toupper(c)returns the uppercase version of c, if c is a letter

23 Quick question #include using namespace std; int main() { string s = "hello"; char c; for (int i=0 ; i< s.length() ; i++) { c = s.at(i); c = toupper(c); cout << c; } cout << endl; } HELLO

24 ASCII and character Everything is represented in 0s and 1s in computers – computers do not understand ‘a’, ‘B’, ‘3’,… – characters are stored as binary numbers ASCII defines the mapping between characters and integers ‘0’  48, ‘A’  65 int main() { int a = ‘A’; cout << a << endl; } 65 int main() { char a = 65; cout << a << endl; } A


Download ppt "CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang"

Similar presentations


Ads by Google