= Assigns the variable. Variable equals 7.
== Comparison between two objects. Are these two items equal to each other
< Less Than. Is this item less then that item. 7 < 10 = True;
> Greater Than. Is this item more then that item. 10 > 7 = True;
+ - / * Basic arthritic Operators. Addition, subtraction, division and multiplication.
% Remainder. Returns the remainder when dividing a number. 11 % 2 = 1 (remainder)
Boolean True or False
A variable stores a piece of information in the computers memory against a symbolic name. Variables can be different data types including:
String - A line of characters (letters and symbols that are assigned using "quotations")
Numbers
Boolean - True or False
Declaring Assigning a Variable
First rule is that each variable must have a unique identifying name. It should be meaningful so that you know what type of data it will be storing.
Second Rule for some languages - each new variable must be declared using the keyword var. Example: var name. Most coding languages require you to declare the type of variable as well. Var name:string
When declaring a variable you may also assign a value. var name:string = "My name"; but this is not mandatory.
Each time you now refer to the variable you may just call its name. name
When writing any code its good practice to include comments about your code that it becomes easier for you and others to follow. The comments only need to be short and can be included above a specific section of code. Its common practice t include code comments throughout your program.
// We use double slash to record a comment on a single line
/*We use a slash and a star for a multi-line comment */
This is most used statements that test if a specific condition is true or not.
Do I have 5 apples? Yes or No
(true) Yes -> execute the code
(false) -> do nothing
IF Statements
If (condition) {
do if true;
}
These are an extension of an if statement by allowing you to test two things.
If the condition is TRUE, do the first command otherwise if NOT TRUE, complete the second command.
Curly brackets are used around each option in some programming languages (e.g. JavaScript). A semi-colon is used after each command call.
IF / Else Statements
if (condition) {
do if true;
else {
do ONLY if NOT true;
}
}
These statements allow a number of conditions to be used within the selection statement. If any condition is true, it completes the code in the block and skips the remainder of the if statement options.
IF / Else IF Statements
If (Condition) {
do if true;
else if (Condition){
do if true;
}
}
While Loops
These loops continue while the condition remains true.
These types of loops are good for timing and keeping track of lives or incorrect answers.
while(condition is true){
do this command
}
while (score <=10){
score+=1
print score;
}
For Loops
For loops use a counter variable to keep track of how many times the code completes it self. Once the condition becomes false, the loop ends and the code continues to the next step.
for(i=0, i<=length of list, i++){
print array item i
}