Download presentation
Presentation is loading. Please wait.
1
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang swyang@cs.ucla.edu
2
Programming Language Here are examples: – In English: Hey Computer, say “Hello!” – In a programming language, C++: cout << “Hello!” << endl; – In a machine language: 010001011010011100100100100101 (or something similar to this)
3
Programing Language Programmers – Translate human language into programing language Writing C++ code Compilers – Translate programing language into machine language Making executable file
4
Example 1 Print out a text on the screen
5
Some Basic Rules #include – Contains libraries related to input & output using namespace; – Just write this statement at this point int main() – Your program start from this function Every statement ends with a semicolon – Missing semicolon will cause compile error Case sensitive – C++ distinguishes between upper and lower case Text needed to be double-quoted – Otherwise, C++ will recognize it as something else White spaces
6
What can we learn from the example cout object – Print out “text” on the screen – Basic syntax cout << “what you like to write”; – endl New line cout << “what you like to write” << endl; – << Careful with the direction Not >> – You can use more “text” cout << “what you like to write” << endl << “more words”;
7
Quick question What will be displayed on the screen? #include using namespace std; int main() { cout << "Hello!" << "My na"; cout << "me is Sung"; cout << endl << "won. What is" << endl; cout << "your " << "name" << endl << "?" << endl; }
8
Answer Hello!My name is Sung won. What is your name ?
9
Quick Question Can you find any mistakes? Hint: 7 mistakes include Using name space std; int main() { cout << "Hello!" << "My na"; cout << 'me is Sung'; cout << endl << "won. What is" << endl cout > "name" << endl << "?" << endl;
10
Answer include Using name space std; int main() { cout << "Hello!" << "My na"; cout << 'me is Sung'; cout << endl << "won. What is" << endl cout > "name" << endl << "?" << endl; Double quote! Should be lower case No space! ; is missing # is missing Wrong direction } is missing
11
Example 2 Input from keyboard #include using namespace std; int main() { int age ; age = 0; cout << "How old are you?" << endl; cin >> age; cout << "I am " << age << " years old." << endl; }
12
What can we learn from the example Variables – “data storage” – int age; Declaration: prepare space for ‘age’ int: integer age: identifier, name of variable – age = 0; Assign 0 to age
13
Data types int : integer – 4 bytes long – ranging from -2,147,483,647 to 2,147,483,647 double : real numbers – 8 bytes long – ranging from −1.7 × 10 308 to 1.7 × 10 308 bool : boolean – 1 byte long – either true or false (IGNORE FOR NOW) char : character, – 1 byte long, holds an ASCII character (IGNORE FOR NOW) string : string – let’s talk about this later
14
Identifiers – An identifier must begin with an alphabetic character (a-z or A-Z) or an underscore(‘_’), which may be followed by alphabetic/numeric (0-9) characters and underscores. – Cannot use keywords (reserved words) using, int, double, etc… Quick questions!
15
What can we learn from the example cin >> age; – Get user’s input from keyboard – Assign the input to age – Be careful with the direction Opposite direction of the case of cout cout << "I am " << age << " years old." << endl; – we can use variable in cout – No quotes!!
16
Quick question age is integer type variable – What if input numbers with a decimal point? The range of int is from -2,147,483,647 to 2,147,483,647 – What if input numbers out of range?
17
Comment Compiler will ignore comments We can insert comments in a C++ program – Use double-slash: // Comment for single line – Use /* and */ pair Comments for multiple consecutive lines
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.