CSE100Lab02 - Lab Section: _ Name: _ Lab 2 Arithmetic...

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

View Full Document Right Arrow Icon
Lab Section: _____ Name: _________________________________ Lab 2 Arithmetic operations, cin/cout Statements Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. Variables and Statements: Note: If you already know what variables are, how to declare and initialize them, and what makes a valid statement, then just look at the bold words and skip the rest of this section. Variables are place holders for data used in programs. Variables have two parts: a data type and a variable name . The data type tells the computer what kind of data should go into the variable (and what size the data is). Common data types are: bool (stores true or false ), int (stores integers), double (stores numbers with decimal parts), char (stores a single character, like 'Q' ), and string (stores many characters, like "She sells seashells by the seashore." ). The second part is the variable name. A variable's name is what we use to access the data stored inside. All variables must be unique in C++. Variable names can be made up from letters (upper- and lower- case), numbers (0-9), and the underscore symbol ("_"). However, variables can't begin with a number. No other characters are allowed as part of a variable name, including spaces . C++ is case-sensitive, which means that gasmileage , gasMileage , and GasMileage all count as different variable names. Try not to use variables with similar names, though, as it's easy to get them confused. So when declaring a variable, remember two parts are needed. Here are some valid declarations: int hours; double cost_of_item, tax_on_item; char letterOfTheAlphabet; bool ToBeOrNot2Be; In addition to declaring variables (telling C++ their names and what data they'll hold), you have to initialize , or define , them. Initializing a variable is giving it an initial value. It's good habit to give variables an initial value right after you declare them, otherwise it opens the door to all kinds of cryptic problems. Here are some examples of initializing variables (modifying the example above): double cost_of_item, tax_on_item; cost_of_item = 9.95; tax_on_item = 0.87; char letterOfTheAlphabet = 'Q'; bool ToBeOrNot2Be = true; Note that you can give a variable an initial value after declaring it (as with hours
Background image of page 1

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

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

Page1 / 6

CSE100Lab02 - Lab Section: _ Name: _ Lab 2 Arithmetic...

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

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