1st variable & operations practice code with comments:
#include <iostream> //Initializing necessary information
using namespace std; //See above
int main() { //Initializing main function
cout << "hello world" << endl; //Prints "hello world" and carries on to the next line.
int i=2; //Initializes an integer type variable "i" with value=2
cout << "i = " <<i<<endl; //Prints the characters "i = " followed by the value of the variable "i" and carries on to the next line
double a=3.3; //Initializes a double type variable "a"
cout << "a = " <<a<<endl; //Prints characters "a = " followed by the value of the variable "a" and carries on to the next line
int j = a*i; //Initializes integer type variable "j" with value "a*i" or the product of the values of variables "i" and "a."
cout << "a*i = "<<j<<endl; //Prints characters "a*i = " followed by the value of variable "j" and carries on to the next line
return 0; //Main function ends
} //See above
Creating, compiling, and running the code:
[cms-opendata@localhost Programs]$ nano variables.cpp
[cms-opendata@localhost Programs]$ g++ -Wall variables.cpp -o variables
[cms-opendata@localhost Programs]$ ./variables
hello world
i = 2
a = 3.3
a*i = 6
As a note, the value of a*i is not printed as 6.6 because the variable "j," where the values of variables "a" and "i" are calculated, is of integer type and cannot have a non-integer value.
Second variable & operations code with comments:
#include <iostream> //Initializes c++ utilities
using namespace std; //See above
int main() { //Initializes main function
int n=10; //Initializes integer "n" with value=10
cout << "n is "<<n<<endl; //Prints characters "n is " followed by the value of variable "n" and ends the line.
n--; //Subtracts 1 from the value of variable "n"
cout<<"n is now "<<n<<endl; //Prints characters "n is now " followed by the value of variable "n" and ends the line.
n++; //Adds 1 to the value of variable "n"
cout<<"n is now "<<n<<endl; //Prints characters "n is now " followed by the value of variable "n" and ends the line.
return 0; //Ends main function
} //See above
Compiling and running code:
[cms-opendata@localhost Programs]$ g++ -Wall variables2.cpp -o variables2
[cms-opendata@localhost Programs]$ ./variables2n is 10
n is now 9
n is now 10
The "--" function subtracts 1 from the value of the variable, while "++" adds 1. It is funny that "++" is a function in the language "c++," but the function is present in the language's predecessor "c" as well.
Third variable & operations code with comments:
#include <iostream> //Initializes utilities
using namespace std; //^
int main() { //Initializes main function
bool prop; //Initializes bool type variable "prop"
prop = (5>1); //Sets the condition of bool type variable "prop" to true (value of 1) if "5>1" is true or false (value of 0) if "5>1" is not true
cout<<"prop is "<<prop<<endl; //Prints characters "prop is " followed by the value of variable "prop" and ends the line
prop = (1>5); //Sets the condition of bool type variable "prop" to true (value of 1) if "1>5" is true or false (value of 0) if "1>5" is not true
cout<<"prop is "<<prop<<endl; //Prints characters "prop is " followed by the value of variable "prop" and ends the line,
prop = (1 != 5); //Sets the condition of bool type variable "prop" to true (value of 1) if "1!=5" (1 does not equal 5) is true or false (value of 0) if "1!=5" is not true
cout << "prop is " <<prop<<endl; //Prints characters "prop is " followed by the value of variable "prop" and ends the line
return 0; //Ends main function
} //^
Compiling and running code:
[cms-opendata@localhost Programs]$ g++ -Wall variables3.cpp -o variables3
[cms-opendata@localhost Programs]$ ./variables3prop is 1
prop is 0
prop is 1