Variables

1,228 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,228
On SlideShare
0
From Embeds
0
Number of Embeds
1,026
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Variables

  1. 1. CS111 Lab Variables Instructor: Michael Gordon
  2. 2. What is a variable?  Variables are a means of storing data for the program.  Variables come in different types and store different kinds of information:     Integers (whole numbers) Decimal numbers Characters And more…
  3. 3. Common Types  int – a positive or negative integer  double – a positive or negative decimal  char – a single character  bool – a true or false value  string – a string of text  The above names are case sensitive  Int, Double, Bool, or Char won’t work
  4. 4. Declaring a variable  To declare a variable, you write its type and its name (which you give it). Examples:    int students; could be an integer variable that stores the number of students in a class string name; could store the class title bool full; could store whether the class is full or not
  5. 5. Assigning values  We use the equal sign to assign values to a variable. Examples:      students = 30; name = “CS111”; full = false; Strings must be contained within double quotes. char variables must be contained within single quotes.
  6. 6. Initializing  Sometimes we declare variables and then later assign their values, but sometimes we want to give an initial value right away. This is called initializing. Examples:    int students = 30; string name = “CS111”; bool full = false;
  7. 7. More on variable names  C++     rules for variable names: Must start with a letter, not a number Must have NO space in it No special characters (only letters, numbers, and underscore). Must not be a C++ keyword, such as: return, if, else, int, double, etc.
  8. 8. Variable initializations Illegal examples String name = 45; int count = 2.5; char grade = B; double = ‘3.14’; bool = “False”; Legal examples string name = “45”; int count = 2; char grade = ‘B’; double = 3.14; bool = False;

×