Get started with outputting content to the terminal and receiving input from it as well.
1. Learn about Fundamental Data Types such as: int, and double
a. How to write arithmetic expressions
b. What variables are.
2. How to output to terminal
3. How to accept input from terminal.
4. How to use comments in a program
5. Programming Excercise
How to initialize a data type
A variable is mainly a container that holds a certain data type. When you initialize a variable you must give it a data type. A data type is simply a specific container. I’ll explain it later, I’ll show you how you can use them below. Also, it doesn’t need to be assigned to a value right away.
Syntax for initializing variables:
type_name name_of_variable = some_value;
//OR
type_name name_of_variable;
name_of_variable = some_value;
Example:
double gravity = 9.8;
/*OR*/
double gravity;
gravity = 9.8;
In other words, we ASSIGN the value of 9.8 to gravity. Remember, 9.8 does NOT equal gravity.
Also /* THIS IS A COMMENT */ which is not read by your compiler. It is mainly for the user to communicate with the person looking at the code. It’s good to insert snippets of comments to explain what is going on.
Example:
/* THIS IS
a comment
BLOCK */
//THIS IS ALSO A COMMENT
How to output data types
To output a data type to the terminal simply use the following syntax.
Syntax:
cout << expression0 << expression1 << expression2 << ... << expressionn;
In other words, we can output as many expressions as we want as long as we use the << insertion operator.
Example:
double gravity = 9.8;
cout << "Gravity: " << gravity << endl;
Output:
Gravity: 9.8
We simply output the string "Gravity: " to the terminal, along with the value of gravity.
cout << expression;
Corresponds to the standard output stream which outputs text to the console with the use of the << insertion operator.
cout << endl;
Inserts a new line character into your terminal which makes every other statement written to the terminal in the next line.
How to receive input from the terminal
As we outputted data types to the terminal, we can receive input from the terminal, as in the user.
Syntax:
cin >> expression1 >> expression2 >> expression3;
Example:
int month;
int day;
int year;
cin >> month >> day >> year;
The extraction operator (>>) reads data from the terminal. If an inappropriate value was inputted, then the stream fails.
What should be put in terminal to accept the previous values.
01 25 2008 //Followed by an enter
/* OR */
01
25
2008
All input is put into a buffer and is taken out when next input statement is reached.
Fundamental Data Types
Some examples on how to declare and use ints and doubles.
// Declares an int which holds an integer value.
int number = 5;
//Declares multiple ints for future use
int number, day, month, year;
//Declares a double which holds a floating point value.
double gravity = 9.8;
//You can use arithmetic expressions such as
int total = number*5;
int total = total + number;
//You can increment values
number = number + 1;
//This has the same effect as above
number++;
//Decrements value by 1
number--;
Remember, if you have something such as a - b * c it will evaluate it using math rules. b*c first!
More examples:
//Divide an int by a double results in a double. Below all result to 3.0
12.0 / 4.0
12.0 / 4
12 / 4.0
//Both integers, produce integer. Below result to 1.
6/4
//To get remainder use the modulus operator. Below result to 1
5 % 2
Programming Excercise
Excercise 1.2.1 Ask the user to input how many quarters, dimes, nickels, and pennies he currently owns. Add up the money, and output the correct values to the terminal. Ask the user in the following fashion.
How many quarters do you have:
How many nickels do you have:
...
You have this amount of dollars: $
