reading-notes

Class 8

Back to home page

Today we learned about Operators and Loops!

For and While statements

A For statement is a command that will keep doing the specified command until a condition results to false.

for ([initialExpression]; [conditionExpression];

[incrementExpression])

statement

The following occures during a for loop:

Examples for html and javascript:

HTMLexample JavaScriptexample

A While statement will execute the statement as long as the condition results to are true.

while (condition)

statement

The condition will be checked before the statement. If the statement results to true the statement is executed and checked again. If the condition results to false, control is passed over to the statement following the loop. You can again use multiple statements using the block statement ({}) command.

whileexample

In this example, the loop increments n and adds that value to x.

What NOT to do:

whatnottodo

In this example, the user made the loop infinite, meaning there is not way for the statement to become false.

Assignment and Comparison operators

An assignment operator will assign a value to its left operand based on the value of the right operand. A good example is x = f(). in this expression f() assigns a value to x. Here is a full list of all the assignment expressions:

assignmentexample

link to website

A comparison operator compares its two operand and give a logical value if the comparison is true. These statements use == and !== operators to perform equality or inequality comparions. Here is a table of all the comparison expressions:

comparisonexample

Link to website