Monday, November 10, 2014

Lesson 5: condtions and loops

Table without borders:
See http://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_collapse

It takes us to lesson 5 to begin introducing the essence of programming... loops and conditions. They are fundamental in programming. Without these there is only very little things we can do and with loops you can really tell your computer to tirelessly do some work.

Javascript knows how to compare things (especially numbers), and you can combine conditions with logical operators: AND, OR and NOT. See details here: http://www.w3schools.com/js/js_comparisons.asp.

Most of the (rather unusual) syntax is borrowed from Java (which borrowed from C). Especially note there are two equal signs for equal. One equal sign is for assignment, such as var x=10;

Comparing Operators

OperatorDescription
==Equals
>=Greater Than Or Equals
<=Less Than Or Equals
>Greater Than
<Less Than
!=Not

Logical Operators

OperatorDescription
&&And
||Or
!Not

Examples

Enter your age:
Enter money amount: (shows blue if zero or positive, or red if negative)

Loops - this is where real power of programming comes from. It will do your steps as long as your specified condition holds.

The while loop.

while (condition) {
  // statements;
}
Remember to specify change of condition within the block or you get infinite loop.

Example:


var i=1;
while (i < 5) {
  alert("hi "+i);
  i = i + 1 // (or i++)
}

There is also the do-while (which I have not ever seen many times in 20 years of professional programming career). The block will be run at least once. The block within the while loop above may not run at all.

var i=1;
do {
  alert("hi "+i);
  i = i + 1;
}
while (i < 5)
Now try change the condition to (i > 100) (which is false)

The pattern of while loop with a init statement, condition, and last increment statement is used so often that a short-cut is made: the for-loop. It has 2 semi-colons in the form: for (init statement; condition; increment statement)

The following is exactly the same as the while loop above:

for (var i=1; i < 5; i++) {
  alert("hi "+i);
}

The for-loop is especially useful to "loop through" a list of items.

Now with loops you can really tell your program to do work.

Punishment line:
How many times?

Array

Another powerful element of programming is the array. It is one name, with an index to represent a table, and index starts with 0.

// number of students attended class so far, sad isn't it:
var students = [4,4,2,1]
var sum = 0
for (var i=0; i < students.length; i++) {
   sum = sum + students[i];
}
var average = sum/students.length;
alert("average:"+average)

Now, let's say you have a little function f(x) = x + 3.

Can you write a program to fill up the following:

xf(x)
-3 
-2 
-1 
0 
1 
2 
3 
We are almost ready to do the function plotter!

One of the greatest mathematicians of all times is Carl Gauss. There was a story about he had a elementary school teacher who wants to keep students all day by telling them to add from 1 to 100. Can you help by writing a little program?

Perhaps you can amaze your friends if you can prove:

4 comments:

  1. Sorry! Mr.Mak
    I got a appointment about straightening my teeth. I need to go to ALL CARE with my Dad.. I think i couldn't go to PUITAK today..
    I'm so sorry!!
    ——waifung

    ReplyDelete
  2. So today u don't have to go to puitak.. I hope i could see u next Monday

    ReplyDelete
  3. See you next Monday!

    ReplyDelete
  4. See you next week. You can still follow along in the things above. More examples of loops later

    ReplyDelete