170 likes | 255 Views
CS31: Introduction to Computer Science I. Discussion 1A 4/2/2010 Sungwon Yang swyang@cs.ucla.edu. Programming Language. Here are examples: In English: Hey Computer, say “Hello!” In a programming language, C++: cout << “Hello!” << endl; In a machine language:
E N D
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang swyang@cs.ucla.edu
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)
Programing Language • Programmers • Translate human language into programing language • Writing C++ code • Compilers • Translate programing language into machine language • Making executable file
Example 1 • Print out a text on the screen
Some Basic Rules • #include <iostream> • 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
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”;
Quick question • What will be displayed on the screen? #include <iostream> using namespace std; int main() { cout << "Hello!" << "My na"; cout << "me is Sung"; cout << endl << "won. What is" << endl; cout << "your " << "name" << endl << "?" << endl; }
Answer Hello!My name is Sung won. What is your name ?
Quick Question • Can you find any mistakes? Hint: 7 mistakes include <iostream> Using name space std; int main() { cout << "Hello!" << "My na"; cout << 'me is Sung'; cout << endl << "won. What is" << endl cout << "your " >> "name" << endl << "?" << endl;
Answer # is missing Should be lower case include <iostream> Using name space std; int main() { cout << "Hello!" << "My na"; cout << 'me is Sung'; cout << endl << "won. What is" << endl cout << "your " >> "name" << endl << "?" << endl; No space! Double quote! ; is missing Wrong direction } is missing
Example 2 • Input from keyboard #include <iostream> using namespace std; int main() { int age ; age = 0; cout << "How old are you?" << endl; cin >> age; cout << "I am " << age << " years old." << endl; }
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
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 × 10308to 1.7 × 10308 • 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
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!
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!!
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?
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