Coding - practical 2 Flashcards
Give an example of a variable declaration.
int year = 2019;
How many parts does a variable declaration involve? Describe them.
Usually involves 4 parts:
- The first part is the data type (e.g. int)
- The second part is the variable name (e.g. year)
- The third part is the assignment operator (=, used to give variables a value)
- The fourth part is the value we wish to assign (e.g. 2019)
What can you use after the variable declaration?
std::cout to print the value of the variable year.
Describe the int data type.
Store whole numbers, i.e. 1670, 5, 123985718275
Describe the float data type.
Store numbers with a decimal part. i.e. 0.76, 440.56
Describe the std::string data type.
Store strings of characters, i.e. “IAP” , “I Love programming”
Describe the bool data type.
Stores true or false values only
Describe the char data type.
Store single characters, i.e. ‘a’, ‘b’, ‘5’
When would you use std::cin?
When you want to change the values of such variables by user input.
How do you get the users input?
Use std::cin, followed by the»_space; symbol (note the direction of flow, we want to put data from the input into age). We then specify the variable that we want to write into.
What does ‘while (true)’ do?
This is a loop and it enables any code that is between the { and } to be repeated. The true part of the while loop makes this section of code repeat forever.