CS31: Introduction to Computer Science I
Discussion 1A 4/16/2010
Sungwon [email protected]
www.cs.ucla.edu/~swyang
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
WHILE Loops
• Repeat statements while condition holdswhile (Condition){ statements; statements; statements; …}
int yourInput = 0; cin >> yourInput;while (yourInput > 0){ cout << yourInput << endl; yourInput--;}cout << “The End” << endl;
5
54321The End
DO-WHILE Loops• Repeat statement while condition holds– Execute the statements once then test conditiondo{ statements; statements; statements; …} while (Condition);
int yourInput = 0; cin >> yourInput;do{ cout << yourInput << endl; yourInput--;} while (yourInput > 0);cout << “The End” << endl;
5
54321The End
while vs. do-whileint 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 10
0 -1
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;
}}
FOR Loopsfor ( initialization ; condition ; update ){ statements; statements;
…}
The order of execution1.Initialization2.Check the conditionIf true
3. Run the body (statements)4. Do the update Go back to 2
If falseExit the for loop
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;
012345
012340
Nested loop• Embed loops into loopsint 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,
FOR to WHILE Conversion
• while loop <-> for loop
for ( initialization ; condition ; update ){ statements; statements;
…}
initializationWhile ( condition ){ statements; statements;
… update }
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
Functions
• Sub-programs in your program• Simplify multiple operations which are basically same
Calculator
Addition
Subtraction
Multiplication
Division
Your Program
Functions
Functions• 3 operations, but basically same jobint 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;
}
Functions
• Define a functionreturn_type function_name (argument_type_1 argument_name_1, argument_type_2 argumrnt_name_2, … ){
function_statements;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;;
}
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;
}
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;
}
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!SungwonNice to meet you!
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!SungwonNice to meet you!
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;
elsereturn 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;
}
Functions of string (Chap 9 in textbook)Operation What it does Examples
string s = “Hello”;string s2 = “!!!”;
Declares strings s and s2
s.length() or s.size()
Returns the length of s cout << s.size(); 5
s.at(i) Returns i-th characteri must be an interger between 0 and size-1(inclusive)
cout << s.at(0); Hcout << s.at(4); o
s.empty() Returns true if s is empty If (s.empty()) cout << “It is empty”;
s + s2 Concaternates two strings cout << 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); llcout << 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
Functions of character(Chap 9 in textbook)• You need to include <cctype> library: #include <cctype>
Operations What 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
Quick question
#include <iostream>#include <string>#include <cctype>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
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
Top Related