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

in devjr-s20w2 •  3 days ago  (edited)

steemit-engagement-challenge-cover.jpeg

Greetings Steemit friends

What are variables and what are they used for?

Variables can be seen as a memory location (bucket, container, store )that holds information that can be manipulated by a computer program. It is another means of naming data in a readable form and giving it a more descriptive property. The three things to remember when declaring a variable are label/name, Type, and end of declaration.

Note:

  • Variable names must start with a letter.
  • The naming of variables also depends on the language you are working with.
  • Some languages do not permit developers to specify the data type, but others do.
  • For no reason are you to separate numbers with commas.
  • Variables value must respect the data type declared.
  • An equal (=) sign is used to assign value to a variable.

There are five common variable types.

Constant Variable: These are variables that hold values that will not be changed in the course of the manipulation throughout the computer program. Neither the name nor the type can be modified.

Global variable: These are variables similar to constants but declared out of a function. The values can be accessed anywhere in a computer program like a constant. To clear any doubt, the value of constant variables can not be modified. However, values of global variables can be modified for any data type.

Static variables: These are variables declared in a class, and can only be one copy.

Instance variable: This variable is found inside a class but not in a function.

Local variable: These are the most common variables, which can be declared to be used with a block of code.

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

Email: String/Character, can store different forms of characters (letters, numbers, and symbols

Examples:
email= [email protected];

Phone: Integer, this is because this variable type can store only whole numbers.

Examples:
phone= 00023457889655;

working_hours: Time, this is because this variable type can store time, and make it easy for calculations. It always holds a date representation of hour, minute, second, and microsecond.

Examples:
working_hours= 11:30:52;

price_steem: Floating, this is because this variable type can store only whole numbers and fractions. Float type permits a variable to hold numbers with decimal points.

Examples:
price_steem = 0.17;

Age: Integer, this is because this variable type can store only whole numbers.

Examples:
Age= 19;

Explain how the following code works:

My understanding, the algorithm is to print out the user name. All data types to be displayed are characters because the variable name and last name will hold single characters, which in this case are letters.

Screenshot 2024-09-16 185006.jpg

The first line

Algorithm names

Is used to initiate the algorithm Names

The second line is used to declare the variable used in the program. In our case, we have name and last_name.
Define name, last_name as Character;

The next line prints out the text in quotes, prompting the user to enter the first input which is the name.
Print "Enter your name:";
Read name;

The next line is similar to the previous one, but this time asking for the last_name
Print "Enter your last name:";
Read last_name;

The next line is putting together the two variables and concatenating them to print out.
Print "Hello " name " " last_name ", welcome";

The last line is used to close the program and tell the computer that nothing is left to do but to run the program.

EndAlgorithm

Note, every statement ends with a semicolon to tell the computer we are done with the statement.

Calculate steem value

Okay for the last section, I had a language challenge. It took me a couple of hours to notice I have to translate words to be able to use the PSeInt applications. That is the reason why the code on the screenshot will be slightly different, but same concept. For fun sake, I added the variable username.

Screenshot 2024-09-16 194444.jpg


Algorithm calculate_steem

        Define username as Character;
        Define price_steem, total, amount_steem como float;

        Print  "Enter Your steemit username";
        Read  username;

        Print  "Enter Current Steem Value (USD):";
        Read  price_steem;   

        Print  "Enter Current Amount Steem Hold:";
        Read  amount_steem;

        total = amount_steem * price_steem;

        Print  "The value in USD of " amount_steem " STEEM, is  " total " USD";

EndAlgorithm



Cheers
Thanks for dropping by
@fombae

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:  

Wow this is amazing. I didn't know something like this could be going on on Steem. The language I'm learning is Python, following my path of artificial intelligence studies. Is this language you're using C?
Please if you can fill me in a little on what you're doing, I would really appreciate cause it looks like it's a contest going on, so maybe I can participate 🙏

Loading...

!upvote 25


💯⚜2️⃣0️⃣2️⃣4️⃣ Participate in the "Seven Network" Community2️⃣0️⃣2️⃣4️⃣ ⚜💯.
This post was manually selected to be voted on by "Seven Network Project". (Manual Curation of Steem Seven).

the post has been upvoted successfully! Remaining bandwidth: 115%

Your post has been rewarded by the Seven Team.

Support partner witnesses

@seven.wit
@cotina
@xpilar.witness

We are the hope!

This post has been upvoted/supported by Team 7 via @philhughes. Our team supports content that adds to the community.

image.png