Basic programming course: Lesson #2 - Variables and Types of Data

in devjr-s20w2 •  13 hours ago 

Hello everyone! Today I am here to participate in the basic programming course by @alejos7ven. It is about learning the basics programming. If you want to join then:



Join here: Basic programming course: Lesson #2 - Variables and Types of Data [ESP-ENG]



Variables and Types of Data.png

Designed with Canva

What are variables and what are they used for?

Variables are the containers which are used to hold the values in a program. Variables allow programmers to store the data. They also allow data modification, and the programmers can retrieve data during the execution of the program.

Basic Components of a variable:
There are 3 basic components of a variable which are given below:

  • Name
  • Type
  • Value

These components are used generally as we start programming. So often we discuss the general properties of the variable. Here I have prepared a detail information about all the 3 basic components and properties of a variable.

  • Name: Name is actually a symbol to represent the variable in the program. It is its identifier like people have their identifiers to be called in the world. Similarly the name of the variable is used to access it in the program. There are some conditions for writing the name of the variable which are given below:

    • Variable cannot start with a number.
    • Variable should not contain spaces or special characters expect underscores.
    • Variable cannot be a reserved keyword such as int and if. Each language has some reserve keywords so we cannot use them to declare the name of the variable.
  • Type: Variables have their unique type to store the respective data. We declare the type of the variable when we declare it in the program. Actually according to the data type a specific container to hold the value is provided to the variable.

  • Value: The value in the variable can change with time with the variation according to the condition in the program. We can take example age if at one place the value of the age is 15 but at any other place the value of age can be 20. It is how the value changes over time of the variables. And in the programming it is a good practice to initialize the variables with the value.

For What Purpose Variables Are Used?

Variables are used for different purposes. I have mentioned earlier a little about the usage of the variables. Here is the more specific information about the usage of the variables:

  • Data Storage: Variables are used to store data temporarily while the program is running.And they allow to reuse and manipulate information without entering it again.

  • Reusability: We can use the same variable in different parts of the program without duplication.

  • Dynamic Processing: Variables enable programs to process dynamic inputs from users, files or other data sources.

  • Control Logic: Variables help to control the program logic. For example: Counters in the loops or flags to track conditions.

Assign a type of data to the following variables

Variables: email, phone, working_hours, price_steem, and age

  1. email:
    • Type: Character
    • Explanation: Email addresses consists of a sequence of characters such as letters, numbers and symbols. Therefore the exact type for an email variable is Character. Because this data type can hold alphanumeric and special characters.

In C++ language we use string to store the email because it ca store letters, numbers and symbols which are used in an email.

  1. phone:
    • Type: Character
    • Explanation: Although a phone number may seem like a number. But it is not used for mathematical operations. Phone numbers also often contain symbols like "+" for international dialing codes, dashes, and parentheses. For this reason, it is best to use it as a Character rather than an integer or numeric type.

In C++ language we use string to store the phone.

  1. working_hours:
    • Type: int or real
    • Explanation: This variable represents the number of hours someone works. If working hours are always in whole numbers then int (integer) will be used. However if the working hours include fractions such as 7.5 hours then a double (floating point number) type should be used to handle decimal values.

In C++ language there are two data types to store the decimal values such as float and double. In most of the cases double is used. The only difference is that both have different capacity.

  1. price_steem:
    • Type: real
    • Explanation: The price of Steem is a value that typically includes decimal places (e.g., 1.234 USD per Steem). To accommodate this, the variable should be of type real, which can represent floating point numbers.

In C++ language we use double and float to store the decimal values. So for the price we can use double data type.

  1. age:
    • Type: int
    • Explanation: Age is always expressed as a whole number (e.g., 25, 30, etc.), so an int (integer) type is appropriate for this variable.

In C++ language we use int to store the age as age is a whole number.


Explain how the following code works:

Algorithm names
Define name, last_name as Character;
Print "Enter your name:";
Read name;
Print "Enter your last name:";
Read last_name;
Print "Hello " name " " last_name ", welcome";
EndAlgorithm

This algorithm is a simple program. It gets the input from the user as first name and last name. Then it displays a greeting message and use the inputs provided by the user. Here is a step by step explanation of the above algorithm how it works:

Define Variables:

The algorithm starts by defining variables. It defines twp variables name and last_name. And the data type of both the variables is characterwhich indicates that it will store strings data or text data.
Define name, last_name as Character;

Here:

  • name variable will used to hold the first name of the user.
  • last_name variable will used to hold the last name of the user.

Prompt for First Name:

This program prints a message to ask the user to input the first name.
Print "Enter your name:";

Here:

  • Print command is used to print the text Enter your name: on the screen and and directs the user to type the first name.

Read User Input (First Name):

The next line reads the user input. And then it stores the user input in the name variable.
Read name;

Prompt for Last Name:

After that the program asks the user to enter their last name.

Print "Enter your last name:";

Here:

  • The Print command is used to print the text Enter your last name: on the screen and directs the user to type the last name.

Read User Input (Last Name):

At this line of code the program reads the user input. And then it stores the user input in the variable last_name.

Read last_name;

Here

  • The Read command waits for the user to enter their last name and then assigns this last name to the last_name variable.

Display Greeting Message:

Finally the algorithm prints a greeting message that includes the first and last name of the user.

Print "Hello " name " " last_name ", welcome";

Here:

  • The Print command display the formatted String. It combines the words Hello, the name variable, a space " ", the last_name variable and the text welcome to create a full sentence like Hello John Doe, welcome.

End of Algorithm:

  • The algorithm ends after printing the greeting message on the screen with the code EndAlgorithm.

In the summary we can say that the program asks for the first name and last name from the user as an input. Then it stores the user inputs in the respective defined variables. And then by using the values stored in the variables it prints a welcome message.

C++ Program for the above Algorithm

Because of my first love with C++ I also want to run this program using C++ language. C++ was the first programming language which I started learning from my teacher in my first semester. So here is the C++ code for the above given Algorithm.

#include < iostream >
using namespace std;
int main()
{
string name, last_name;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your last name: ";
cin >> last_name;
cout << "Hello " << name << " " << last_name << ", welcome!" << endl;
return 0;
}

image.png

C++ Program of Algorithm - Compiled By Programiz

There are 2 data types in C++ which are used to store the character. One is char and the other is string.

char stores only one character
string can store multiple characters

So seeing the requirement of name which contains more than one characters I have used string data type for the variables.


Develop a pseudo-code to calculate the value in USD of X STEEM.

Algorithm steem_usd
//Declaration of the required variables
Define price_steem, amount_steem, total as Real;
// Request for the user input to get the STEEM price in USD
Print "Enter the current market price of STEEM in USD:";
//Storing the current price of STEEM in price_steem variable
Read price_steem;
// Request for the user input to get the amount STEEM
Print "Enter the amount of STEEM tokens:";
//Storing the amont of STEEM in amount_steem variable
Read amount_steem;
// Formula to calculate the total value of the provided STEEM in USD
total = price_steem * amount_steem;
// To show the final result
Print "The total value of provided STEEM in USD is: " total;
EndAlgorithm

algo.PNGalgo_output.PNG
InputOutput

It was my first time to use PSeInt and I was unaware of this compiler which compiles the algorithms only But getting curios I decided to use it to complete the task. But I used it online with the help of the RollApp on their cloud service. It was hard for me to understand because of change of language but seeing the Icons I got it. And you can see I have successfully run my algorithm.

C++ Program for the above Algorithm

Here is the C++ code for calculating the value of the STEEM to USD.

#include < iostream >
using namespace std;
int main() {
// Declaration of required variables
double price_steem, amount_steem, total;
// Request for the user input to get the STEEM price in USD
cout << "Enter the current market price of STEEM in USD: ";
cin >> price_steem;
// Request for the user input to get the amount of STEEM
cout << "Enter the amount of STEEM tokens: ";
cin >> amount_steem;
// Formula to calculate the total value of the provided STEEM in USD
total = price_steem * amount_steem;
// To show the final result
cout << "The total value of the provided STEEM in USD is: $" << total << endl;
return 0;
}

image.pngimage.png
C++ Program InputOutput

I have used an online compiler Programiz. I often use this compiler for running the code because it supports a number of different languages online.

You can see that I have added current price of STEEM tokens and then I added the amount of STEEM tokens and the program has successfully calculated the total value of the provided STEEM in USD.


Conclusion

Variables and the data types are the foundation of the programming. Variables are the building blocks of programming languages. If we have basic knowledge of the variables that how they work and how they are used in the programming then we can write the programs efficiently. And there are different data types which are used according to our requirement and data. Each data type stores specific data with specific length.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@tipu curate

Your post has been rewarded by the Seven Team.

Support partner witnesses

@seven.wit
@cotina
@xpilar.witness

We are the hope!

Thank you so much Seven Team.