cs31s11dis2 - Discussion 1H Notes (Week 2, 4/8) TA: Brian...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Variables You have to instruct your computer every little thing it needs to do even what it needs to remember. We create data storages for the computer and explicitly tells it what to store in which storage. We call these storages variables . Output: int x; This is how you declare a variable. int indicates this variable must store an integer (called data type ), and labels it x (called identifer ). You can think of it as the process of creating an integer box for later use. See the textbook for other data types. Among these data types, we mainly use the following types in this course: 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, lets talk about this later today ( Note: We use /* ... */ and // for making notes in the code for ourselves. Text within /* and */ (which can span over multiple lines), and everything after // and before the end of the line are not considered to be part of the code. We call such text a comment and say that part of the code is commented out . It is an important practice to get your codes properly commented.) CS31: Introduction to Computer Science I Spring 2011 Copyright Brian Choi 2011. All Rights Reserved. Week 2, Page 1/8 /* A simple program demonstrating the usage of variables. */ #include <iostream> using namespace std; int main() { int x; // create an integer variable x int y; // create an integer variable y x = 5; // store 5 in x y = 6; // store 6 in y cout << "x = " << x << ", y = " << y << endl; // print them out return 0; } Identifers There are variable naming rules. An identifer must begin with an alphabetic character ( a- z or A- Z ) or an underscore ( _ ), which may be Followed by alphabetic/numeric (- 9 ) characters and underscores. Violating any oF these rules will result in a compile error. A commonly used convention is that you use lowercase letters except For the frst letter oF the second word (e.g. moneyPaid , milesPerHour ). Later youll see some special variables called constants (actually, constants are constants because they are not variables, but we will explain this later), which we name using uppercase letters (e.g. GRAVITY ). Be descriptive with the varible names -- countApple is better than ca . Variable Declaration Youve seen examples already. Here are more examples oF variable declarations: int x; // integer x double milesPerHour; // double milesPerHour int countApple, countOrange; // multiple declarations (same type) at once You can use these variables only aFter you declare them. Otherwise, the compiler will throw an unknown variable error. Also note that these variables initially store some random values (that is, you cannot assume variable error....
View Full Document

Page1 / 8

cs31s11dis2 - Discussion 1H Notes (Week 2, 4/8) TA: Brian...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online