Coding & programming
Installing CodeBlocs compiler
C language
C++ language
© The scientific sentence. 1998-2016
| Â |
|
Programming with C/C++ Language
Introduction to the C++ Language
Comments and Variables
1. Commenting code within a program
Adding comments to a program; helps explain code.
When we tell the compiler a section of text is a comment, it will ignore
it when running the code.
To create a comment use either //, which tells the compiler that the rest
of the line is a comment, or /* and then */ to block off everything between
as a comment.
Certain compiler environments will change the color of a commented area, but some will not.
2. Variables
So we've learned how to write a simple program to display information, and how
to describe a program with comments.
It is also possible for a program to accept input. The function to use is known
as cin, and is followed by the extraction operator >>.
Before to receive an input, the compiler make a place to store that input.
In programming, input and data are stored in variables. There are several
different types of variables which store different kinds of information, numbers or
letters.
When we tell the compiler we are declaring a variable, we must include
the data type along with the name of the variable.
Several basic types include char, int, and float.
A variable of type char stores a single character,
variables of type int store integers (numbers without decimal places),
and variables of type float store numbers with decimal places.
Each of these variable types - char, int, and float - is the keyword that
we use when we declare a variable.
Using the right variable type is important for making a code readable
and efficient . Some variables require more memory than others.
Declaring Variables in C++
To declare a variable we use the syntax type <name>;. Here are
some variable declaration examples:
int x;
char letter;
float the_float;
It is permissible to declare multiple variables of the same type
on the same line; each one should be separated by a comma. For example:
int a, b, c;
Note that declaration of a variable is always followed by a semicolon.
This is the same procedure used when we call a function.
Note also that, in C++, all language keywords, all functions and all variables
are case sensitive.
3. Other program example
#include <iostream>
using namespace std;
int main()
{
char pseudo[] = "Shamsha";
int age = 33;
cout << "Hi, my name is " << pseudo << ".
I am " << age << "years old." << endl;
cin.get();
}
The keyword int declares age to be an integer.
The function cin>> reads a value into a <name>;
the user must press enter before the number is read by the program.
cin.ignore() is another function that reads and discards a character.
Remember that when you type input into a program, it takes the enter key too.
The next command is cin.get(). This is another function call:
it reads in input and expects the user to hit the return key.
This command cin.get() keeps the window from closing because
the program is not done yet and it waits for us to hit enter.
Including that line gives time to see the program run.
4. Changing and Comparing Variables
Operators such as *, -, +, /, =, ==, >, < are used
with variables. The * multiplies, the - subtracts, the + adds, and the
/ divides..
To modify the value of a variable inside the program, we assign to it
a value by using the equal sign.
Examples
int x ; // declare the variable x
x = 3; // assign 3 to the variable a
x = x - 2; /*replace x: x equal the original value of x with 2 subtracted to it.*/
x = 4 * 5; // change x : that is 24
x == 4; // compare x: checks to see if x equals 4.
The equal sign: == compares the left and right values.
|
  |