CS 105

Week 2

Activity 1

Due a few minutes ago.

Lots of late adds, so we will allow submissions for a few more days; get it done if you have not done it yet.

MP0

Build your own, original Scratch program!

Available on CS 105 Website
Due: Tomorrow, Feb. 3, 8:00pm

Statements

Statements instruct the computer to DO something.

Statements

moveForward(10);

Statements

moveForward(10);
  • Statements instruct the computer to DO something.
  • Statements always end in semicolons.
  • The computer must understand the statement
  • Lets try it!

Statement #1

console.log("Anything you want");

Variables

Variables allow a program to REMEMBER a value for later use.

Variables

The first time you use a variable, you must tell the computer about it by using var:

var temp = 20;

Variables

After the computer knows about it, you should never use var again for that variable:

var temp = 20;
temp = temp + 10;  // Adding 10 to temp
temp = temp - 10;  // Subtracting 10 from temp
temp = temp * 2;   // Multiplying temp by 2
temp = temp / 2;   // Dividing temp by 2

Variables

Lets print some variables!

Conditionals

Conditionals CONTROL the path of execution.

Conditionals

if ( ____________ ) {

}
  • Conditionals CONTROL the path of execution.
  • Conditionals are NOT statements -- no semicolons!
  • Instead, conditionals wrap statements inside of them with curly braces.
  • Conditionals require a boolean expression.

Conditionals

var temp = 20;
if ( temp < 32 ) {
   console.log("Burrr!");
}

Conditionals

var temp = 20;
if ( temp < 32 ) {
   console.log("Burrr!");
} else {
   console.log("Warmth!!");
}
Lets print some variables!

Putting It Together

var birthMonth = you.birthMonth;
var birthDay = you.birthYear;

Putting It Together

var birthMonth = 11;  // Yours will probably be different
var birthDay = 27;    // Yours will probably be different
if (birthMonth < 4) { moveToRightSideOfRoom(); }
else if (birthMonth > 9) { moveToLeftSideOfRoom(); }
else { moveToCenterOfRoom(); }
if (birthMonth < 4) { moveToRightSideOfRoom(); }
else if (birthMonth > 9) { moveToLeftSideOfRoom(); }
else { moveToCenterOfRoom(); }

while ( running == "yes" ) {
   var otherPerson = findAnotherPerson();
   if (you.birthMonth < otherPerson.birthMonth) {
      you.moveForward();
   } else if (you.birthMonth > otherPerson.birthMonth) {
      you.moveBackwards();
   } else {
      if (you.birthDay < otherPerson.birthDay) {
         you.moveForward();
      } else {
         you.moveBackwards();
      }
   }
}

i>Clickers

We'll begin using them on Wednesday!

We will randomly give +1 points throughout the semester for answering questions.
(At least +5 by end of semester.)