ItzEdInYourBed

69 - C++ VARIABLES

Jul 12th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. [SIZE=7][B]Introduction to Variables[/B][/SIZE]
  2. The [ICODE]"Hello World![/ICODE]" program simply writes to the screen. It does not read anything, calculate anything, or allow for user input. That’s no fun!
  3. Real programs tend to produce results based on some input that the user of the program gives, rather than just outputting the same thing every time.
  4. To read something from the keyboard, we first need somewhere in the computer’s memory to [I]store data[/I]. That is where variables come in.
  5. A [B]variable[/B] is simply a name that represents a particular piece of your computer’s memory that has been set aside for you to store, retrieve, and use data.
  6.  
  7. In this lesson, we will learn about some of the basic data types:
  8.  
  9. [LIST]
  10. [*][ICODE]int[/ICODE]: integer numbers
  11. [*][ICODE]double[/ICODE]: floating-point numbers
  12. [*][ICODE]char[/ICODE]: individual characters
  13. [*][ICODE]string[/ICODE]: a sequence of characters
  14. [*][ICODE]bool[/ICODE]: true/false values
  15. [/LIST]
  16. Every variable has a [B]type[/B], which represents the kind of information you can store inside of it. It tells your compiler how much memory to set aside for the variable, and it defines what you can do with the variable.
  17.  
  18. [IMG]https://i.imgur.com/h0koH5h.gif[/IMG]
  19.  
  20. [SIZE=7][B]Step 1: Declare a Variable[/B][/SIZE]
  21. [CODE]"Every variable in C++ must be declared before it can be used!"[/CODE]
  22.  
  23. score that goes from 0 to 10. We need a variable!
  24.  
  25. Before we can use a variable, we must [I]declare[/I], or create, it. To declare a variable, we need to provide two things:
  26.  
  27. [LIST]
  28. [*]A type for the variable.
  29. [*]A name for the variable.
  30. [/LIST]
  31. So to declare an integer variable called [ICODE]score[/ICODE], we need to write:
  32. [CODE]int score;[/CODE]
  33. [LIST]
  34. [*]The int is the type of the variable.
  35. [*]The score is the name of the variable.
  36. [*]The ; is how we end a statement.
  37. [/LIST]
  38. In C++, variable names consist only of upper/lower case letters, digits, and/or underscores.
  39.  
  40. [B]Note:[/B] C++ is known as a [I]strongly typed[/I] language. If you try to give an integer value a decimal number, you are going to get unexpected results, warnings, or errors.
  41.  
  42. [SIZE=7][B]Step 2: Initialize a Variable[/B][/SIZE]
  43. After we declare a variable, we can give it a value!
  44. Suppose that we have declared an [ICODE]int[/ICODE] variable called [ICODE]score[/ICODE], to set it to 0, we can simply write:
  45. [CODE]score = 0;[/CODE]
  46. [LIST]
  47. [*]The score is the name of the variable.
  48. [*]The = indicates assignment.
  49. [*]The 0 is the value you want to store inside the variable.
  50. [/LIST]
  51. [B]Note:[/B] In C++, a single equal sign [ICODE]=[/ICODE] does not really mean “equal”. It means “assign”. In the code above, we are assigning the [ICODE]score[/ICODE] variable a value of 0.
  52.  
  53. [SIZE=7][B]Combining Step 1 and Step 2[/B][/SIZE]
  54. We can both declare and assign a value to a variable in a single initialization statement.
  55.  
  56. Suppose we have these two lines:
  57.  
  58. [CODE]// Declare a variable
  59. int score;
  60.  
  61. // Initialize a variable
  62. score = 0;[/CODE]
  63. We can actually combine these two lines into a single line of code:
  64. [CODE]int score = 0;[/CODE]
  65. This means we are declaring an integer called score and setting it equal to 0.
  66. [IMG]https://i.imgur.com/6dIaVQC.png[/IMG]
  67. [B]Note:[/B] We only need to declare a variable one time! And it is highly suggested to initialize a variable before using it later.
  68.  
  69. [SIZE=7][B]Arithmetic Operators[/B][/SIZE]
  70. Computers are incredible at doing calculations. Now that we have declared variables, let’s use them with [I]arithmetic operators[/I] to calculate things!
  71.  
  72. Here are some arithmetic operators:
  73.  
  74. [LIST]
  75. [*][ICODE]+[/ICODE] addition
  76. [*][ICODE]-[/ICODE] subtraction
  77. [*][ICODE]*[/ICODE] multiplication
  78. [*][ICODE]/[/ICODE] division
  79. [*][ICODE]%[/ICODE] modulo (divides and gives the remainder)
  80. [/LIST]
  81. For example:
  82. [CODE]int score = 0;
  83. // score is 0
  84.  
  85. score = 4 + 2;
  86. // it is now 6
  87.  
  88. score = 4 - 2;
  89. // it is now 2
  90.  
  91. score = 4 * 2;
  92. // it is now 8
  93.  
  94. score = 4 / 2;
  95. // and now 2
  96.  
  97. score = 5 % 2;
  98. // and now 1[/CODE]
  99.  
  100. [B]Note:[/B] The order of operations can be specified using parentheses. For example, the use of parentheses in [ICODE]score = 5 * (4 + 3)[/ICODE] sets [ICODE]score[/ICODE] equal to [ICODE]5 * 7[/ICODE] rather than [ICODE]20 + 3[/ICODE].
  101.  
  102. [SIZE=7][B]Chaining[/B][/SIZE]
  103. Now that we have outputted a variable and have also outputted things using multiple [ICODE]couts[/ICODE]. Let’s take a closer look at [ICODE]cout [/ICODE]again.
  104. If we have the code below:
  105. [CODE]int age = 28;
  106.  
  107. std::cout << "Hello, I am ";
  108. std::cout << age;
  109. std::cout << " years old\n";[/CODE]
  110. It will output:
  111. [CODE]Hello, I am 28 years old[/CODE]
  112.  
  113. Notice how we use quotes around the characters in [ICODE]"Hello, I am "[/ICODE] but not in [ICODE]age[/ICODE].
  114.  
  115. [LIST]
  116. [*]We use quotes when we want a literal string.
  117. [*]We don’t use quotes when we refer to the value of something with a name (like a variable).
  118. [/LIST]
  119. So now, is it possible to write the [ICODE]cout [/ICODE]statements within a single line?
  120.  
  121. Yep! You can use multiple [ICODE]<<[/ICODE] operators to [I]chain[/I] the things you want to output.
  122.  
  123. For the same code above you can also do:
  124. [CODE]int age = 28;
  125.  
  126. std::cout << "Hello, I am " << age << " years old\n";[/CODE]
  127. This is called [B]chaining[/B].
  128.  
  129. [SIZE=7][B]User Input[/B][/SIZE]
  130. Like we mentioned in the introduction, another way to assign a value to a variable is through user input. A lot of times, we want the user of the program to enter information for the program.
  131. We have [ICODE]cout [/ICODE]for output, and there is something called [ICODE]cin [/ICODE]that’s used for input!
  132.  
  133. [CODE]std::cout << "Enter your password: ";
  134. std::cin >> password;[/CODE]
  135. The name [ICODE]cin [/ICODE]refers to the standard input stream (pronounced “see-in”, for [B]c[/B]haracter [B]in[/B]put). The second operand of the [ICODE]>>[/ICODE] operator (“get from”) specifies where that input goes.
  136.  
  137. [SIZE=7][B]Review[/B][/SIZE]
  138. You made it to the end of the lesson! High five. 🙌
  139. Here is a review of the lesson:
  140. [LIST]
  141. [*]A variable represents a particular piece of your computer’s memory that has been set aside for you to use to store, retrieve, and manipulate data.
  142. [*]C++ basic data types include:
  143. [LIST]
  144. [*][ICODE]int[/ICODE]: integers
  145. [*][ICODE]double[/ICODE]: floating-point numbers
  146. [*][ICODE]char[/ICODE]: individual characters
  147. [*][ICODE]string[/ICODE]: sequence of characters
  148. [*][ICODE]bool[/ICODE]: true/false
  149. [/LIST]
  150. [*]Single equal sign [ICODE]=[/ICODE] indicates assignment, not equality in the mathematical sense.
  151. [*][ICODE]cin[/ICODE] is how to receive input from the user.
  152. [/LIST]
Add Comment
Please, Sign In to add comment