Skip to content

Latest commit

 

History

History
106 lines (80 loc) · 2.82 KB

loops.md

File metadata and controls

106 lines (80 loc) · 2.82 KB

Comparison opertors

SS

  1. == is equal to
  2. != is not equal to
  3. === strict equal to
  4. !== strict not equal to
  5. > greater than
  6. < less than
  7. >= greater than or equal to
  8. <= less than or equal to

USING COMPARISON OPERATORS

At the most basic level, you can evaluate two variables using a comparison operator to return a t rue or f al se value. In this example, a user is taking a test, and the script te lls the user whether they have passed this round of the test. The example starts by setting two variables:

  1. pass to hold the pass mark
  2. score to hold the users score To see if the user has passed, a comparison operator checks whether score is greater than or equal to pass. The result will be true or false, and is stored in a variable called has Passed. On the next line, the result is written to the screen. The last two lines select the element whose id attribute has a value of answer, and then updates its contents. You will learn more about this technique in the next chapter. DECISIONS

COMPARING TWO EXPRESSIONS

AA

  1. In this example, there are two rounds to the test and the code will check if the user has achieved a new high score, beating the previous record.

  2. The script starts by storing the user's scores for each round in variables. Then the highest scores for each round are stored in two more va riables.

  3. The comparison operator checks if the user's total score is greater than the highest score for the test and stores the result in a variable called comparison.

  4. In the comparison operator, the operand on the left calculates the user's total score. The operand on the right adds together the highest scores for each round. The result is then added to the page.

  5. When you assign the result of the comparison to a variable, you do not strictly need the containing parentheses (shown in white on the left-hand page).

  6. Some programmers use them anyway to indicate that the code evaluates into a single value. Others only use containing parentheses when they form part of a condition.

loop counters

a for loop uses a counter as a condition this instructs the code to run a specified number of times. here you can see the condition is made up of three statements: bb

  1. initialization create a variable and set it to 0, this variable commonly called i, and it acts as the counter.

  2. condition the loop should contine to run until the counter reaches a specified number.

  3. Update every time the loop has run the statements in the curly braces, it adds one to the counter.