Overblog
Edit post Follow this blog Administration + Create my blog
Learn to Code with KC

Variables

The first concept that I will introduce is the variable. Variable serve as a huge and important part in programs. A variable can be a string (words,letters,and even paragraphs) or a number. You have to declare a variable before you use it. In other words, you let the computer know that you are going to use it later on. It has to be declared before its use.

One way to use a variable is to say in the main function what the variable is equivalent to.

To do this you simply use an equal sign. To declare a variable of a number value put int in front of the variable and to declare a variable of a string value put string before it. Also, remember to put a semi-colon after a statement.

#include <iostream>

using namespace std;

int a;

string b;

int main()

{

a = 10;

cout << a << endl;

b = "Hello World";

cout << b << endl;

return 0;

}

10

Hello World

If you need to you can change the value of a variable. Just set it equal to something else the same way.

#include <iostream>

using namespace std;

int a;

int main()

{

a = 10;

a = 11;

cout << a << endl;

return 0;

}

11

If you want to you can make one variable equal to another.

#include <iostream>

using namespace std;

int a;

int b;

int main()

{

a = 10;

b = a;

cout << b << endl;

return 0;

}

10

It is important you say b = a and not a = b. It will not work that second way. Also, b = a has to come after a = 10. The computer will not understand a = b when it does not know the value of at least one. Saying a = b first lets the computer forget the value of a.

Share this post
Repost0
To be informed of the latest articles, subscribe:
Comment on this post
C
This will be a great support for the computer students but when consider the huge writing on complete research is really tough so we can choose the extra ordinary article work support from a top essay writing services provider.
Reply