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

in devjr-s20w2 •  3 days ago 

Hello, my fellow programmers, it is the second week of the Steemit Engagement Challenge for season 20 and I'm delighted to be part of this week's contest organized by @alejos7ven. I wasn't able to join last week's due to my engagement offline.

OIG3.R45f_qFhKi8GFEumA.jpeg
Source

Seeing this course Basic programming course: Lesson #2 - Variables and Types of Data, I remembered when I got to the university newly to study computer sciences. I had no idea of programming when I got to school and the first thing I came across was what I'm seeing here today. Without much talk let's go ahead to start the week's work.

mine.PNG

What are variables and what are they used for?

mine.PNG

Variable is a commonly used word when it comes to programming. When we talk about the word variable we are referring to a container where a specific type of data is stored and this data can be called and used thereafter.

A variable serves as a storage of information that we use at the cause of our activities or in the process of writing our codes. The word variable is a common name given to these containers of information but the information in the different containers may differ and that is why a variable name may be the same but will have different content and that leads us to the different data types we have in programming.

There are four fundamental data types in programming namely, integer, real, character, and logic. I will explain them one after the other below with examples for more clarification.

Integer: This is a variable type whose main purpose is to store integer numbers be they positive or negative but not a decimal number. This means that numbers such as 10, 2, -3, -8, etc can be stored using this variable type but numbers such as 0.8, -3.5, etc are not recommended to be stored using this data type.

Real: Another variable type is the real variable type which is used to store decimal numbers be they positive or negative such as 0.8, -3.5, etc. So when dealing with a value that has a decimal point the best data type to use is the real data type.

Character: Another important data type that is most used in programming is the character data type. This data type is used to store characters such as alphabet and numbers combined. A good example is name, address, etc.

Logic: Another important data type I want us to look at today is the logic data type which accepts only two values such as true/false, yes/no, etc.

mine.PNG

Assign a type of data to the following variables and explain why: email, phone, working hours, price_steem, and age.

mine.PNG

Email: An email address always contains characters and numbers possibly. So we can say it contains an alphabet and numbers or alphanumeric values hence the data type that will be suitable for email is Character data type and can be assigned in the format below.

Define email As Character;

Phone: Every phone number is made up of an integer value as it contains numbers. So the data type for the phone is an integer and can be assigned in the format below.

Define phone As Integer;

Working hours: Talks about time and it is also in the form of numbers which an integers as well hence the data type for working hours is an integer and can be assigned in the format below.

Define working_hours As Integer;

Price_steem: The price of Steem is a number but it has a decimal and for that reason, the data type suitable for it is the Real data type and these can be assigned in the format below.

Define price_steem As Real;

Age: This is an integer data type since it is made up of a number. We don't have points attached to any age hence you can assign this in the format below.

Define age As Integer;

mine.PNG

Explain how the following code works:

mine.PNG

ale3.PNG

From the screenshot above, we want to explain the given algorithm so I will pick each line of code one after the other and explain below follow me carefully as we progress.

Algorithm names: Here what the line of code tells us is that the algorithm is called a names.

Define name, last_name as Character;

Here we see the declaration of two variables whose data type is Character. name and last_name were declared in the above line of code as Character.

Print "Enter your name:";

The line of code requests the user to enter the name. So it displays the instructions Enter your name.

Read name; This line of code displays the typed name that was initially input based on the previous request.

Print "Enter your last_name:";

The line of code requests the user to enter the last_name. So it displays the instructions Enter your last_name.

Read last_name; This line of code displays the typed name that was initially input based on the previous request.

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

Here this line of code displays Hello followed by the name you entered when you were required to enter the name then the last name as well when you were asked to enter it and then it displays it alongside the welcome.

EndAlgorithm: This tells you that the algorithm has come to an end.

mine.PNG

Develop a pseudo-code to calculate the value in USD of X STEEM. For this, you can rely on this algorithm:
  • Declare variables price_steem, total and amount_steem.
  • Request the user for the necessary data.
  • Calculate as follows: total = amount_steem*price_steem.
  • Show the result on screen.

mine.PNG

Based on the algorithm given above I would like to develop my pseudo-code below.

ale4.PNG

Algorithm USD of STEEM

//1. Create variables

Define price_steem, total, amount_steem as Real;

//2. Collect data

Print "Enter the price_steem:";

Read price_steem;

Print "Enter the amount_steem:";

Read amount_steem;

//3. Calc total

total = amount_steem*price_steem;

//4. Show results

Print " The value in USD of Steem is " total;

EndAlgorithm

ale1.PNG
Code in the compiler

ale2.PNG
Output of the code

mine.PNG

Conclusion

mine.PNG

This is indeed a nice course as it reminds me a lot. I want to finally invite a few friends to also join the contest @josepha, @suboohi, and @solaymann.

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:  

Upvoted! Thank you for supporting witness @jswit.

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

CONGRATULATIONS!!

Your post has been supported by TEAM SHINING STARS. We support quality posts, good comments anywhere, and any tags.


1000152665.gif

Curated by : @josepha
Loading...

Hola amigo, gusto en saludarte.

Has hecho una buena teoria acerca de los tipos de variables. La tarea practica la has realizado muy bien y muy entendible la explicación.

La ejecución del pseudocódigo hace más amigable la actividad.

Te deseo mucho éxito en tu participación.