Early Start First Project

Learn


Datatypes | Variables | Boolean Operators | If..Else | For Loops

Data Types:

 Loading...

What is a Datatype?

In computer science, we use programming to tell a computer what to do and how to perform certain tasks. In order to perform various tasks, programming languages support different data representations(data types) to store different kinds of values.

For example, if we want to store a whole number such as 45, we can represent that as an integer. Integers are one type of data type that store numbers that do not contain any decimals. Another data type is a double which stores numbers that contain a decimal or fraction in decimal form. If you want to keep track of 2 dollars and 50 cents, you can store that as a double as 2.50. Boolean is a data type that store a true or false value. You can only store the values "true" or "false" with a boolean data type representation. Those are just three main data types, there are many more. The more computer science you learn, the more data types you will discover so that you can accommodate many different types of data that you want to be presented in a computer.

Examples:


Data Type Example
Integer: whole numbers 6
Double: decimal or fraction numbers 3.14
Boolean: true or false statements true
char: characters/letters 'H'
String: many characters(phrases) "HelloWorld"
float: decimal or fraction values with only 6-7 digits 6.99
byte: whole numbers from -128 to 127 112

Variables:

 Loading...

Variables

Now that we have data, we can store the data in a name that the data is identified with along with the type of data it is. Variables are the containers that store the data.

For example, if you want to store how much money is currently in your bank you would put the number in side of a variable. This variable can be called anything as long as its proper datatype is declared right before the variable name. To store your bank balance you can write this as "double myBankBalance = 436.67;". By storing your data under proper names and data types, you can access them in a program very easily and efficiently. If your data is being manipulated or changed in a certain patter, you can call your variable by its name and use it in your lines of code. This way your storage container updates the new variable's value to its current one.

Booleans:

 Loading...

Boolean Operators

Boolean operators examine the relationship of two or more values and return a value. We can let certain events happen if the relationship between variable are a certain. For example, it the weather says its raining, you will bring a raincoat.

In a similar way, we can figure out the relationship between variables by using boolean operator. A boolean operators will always produce true or false (0 or 1). Below is a table with some common boolean operators that can be used for different types of relationships.


The Boolean Operators

Boolean Operators Example
Equal to: examines if variable A and variable are the same. If this is true, it will return true and false is the other around. A==B
Not Equal to: examines if the variable A is not equal to variable B A != B
Greater than: examines if value A larger than value B A>B
Less than: checks if variable A is less than variable B A < B
Greater than or equal to: checks if variable A is the same as B or if A is larger than b in numerical value A>=B
Less than or equal to: examines if A is smaller than B or if they are the same A<=B
And: If we want to check a attribute of A and B or multiples attributes about a single variable, we use the "and" operator A==1 && B == 1
Or: This operator will return true if any condition is true. For example, if we want to check if at least one variable is 10 we can say this || or that. A || B
Not: As mentioned ealier, not is used to say things are not equal to each other. But they can also be ussed to reverse a logic statment. If you want to say false you can use the not operator in front of a true statment which makes it false. !A

If.....Else?

 Loading...

If Else Statements

Now that we have learned different fata types, variables and operators, we can use all of these concepts and apply them into if else statements.

Earlier, we said if its raining, then we wear a rain coat. This is is an if statement because it has a condition that its raining. And based on the condition, we perform an action which was putting on a raincoat. In an if else statement, we put an if statement header and put the action to be run if the case is true. Then below we add an action under the else statement. The diagram below can help you visually see what is happening.
logo

For Loops:

 Loading...

For Loops

For loops are great tools that allow you to perform certain task as along as a certain condition is meet. For loops allow you run lines of codes for a certain number of times. .

For example, if you want to print "Happy Birthday" 10 times you could have a for loop that runs the print statment 10 times. For loops save a lot of time in your code because if you want to repeat an action several times, then you need to declare your actions however many times.

But with the for loop, you can compress that code inside of a for loop that will do the same job as manually repeating an action.



logo