Hour of code

presented at Code Akron

by Rachel Krantz

Function

  • You control things through code with functions
  • Coding languages have built-in functions
  • You can also create your own
moveForward();
moveForward();

Syntax

  • Every language has it -- it's like grammar
  • JavaScript -- the language we're using -- ends every function with a semi-colon ;
  • Fun fact: "Syntax" makes other languages easier to learn once you know one
moveForward();
moveForward();
turnRight();
moveForward();

MOre syntax!

  • Parentheses: some languages use them for functions, some don't
  • camelCase: makes it easier to read
moveForward();
turnLeft();
moveForward();
turnRight();
moveForward();

challenge

  • That's a lot of dragging and dropping. What would you do to make it easier?
turnRight();
moveForward();
turnLeft();
moveForward();
moveForward();
moveForward();
turnLeft();
moveForward();

loops

  • They make code repeat itself until you're done using it
for (var count = 0; count < 5; count++) {
  moveForward();
}
turnRight();
moveForward();
turnLeft();
moveForward();
moveForward();
moveForward();
turnLeft();
moveForward();

loops

  • "var count = 0" starts your counting at zero
  • "count < 5" tells your loop to repeat only 5 times
  • "count ++" tells your loop to add 1 to "count" every time you moveForward();
turnRight();
for (var count = 0; count < 5; count++) {
  moveForward();
}

loops

  • How are these two loops different?
for (var count = 0; count < 4; count++) {
  moveForward();
}
turnLeft();
for (var count2 = 0; count2 < 5; count2++) {
  moveForward();
}

loops

  • You can have loops inside loops!
for (var count2 = 0; count2 < 3; count2++) {
  for (var count = 0; count < 2; count++) {
      moveForward();
  }
  turnRight();
}

"while" loops

  • Repeats itself as long as -- or "while" -- it matches the rule you create
  • They are conditional
while (notFinished()) {
  moveForward();
}

"while" loops

while (notFinished()) {
  moveForward();
  moveForward();
  turnLeft();
}

"While" loop

while (notFinished()) {
  moveForward();
  turnLeft();
  moveForward();
  turnRight();
}

"while" loops

while (notFinished()) {
  turnRight();
  moveForward();
  turnLeft();
  moveForward();
}

"if" statements

  • "If" something is true, your function will do an action
while (notFinished()) {
  moveForward();
  if (isPathLeft()) {
    turnLeft();
  }
}

"nesting"

  • When you put one function inside of another function
while (notFinished()) {
  moveForward();
  if (isPathRight()) {
    turnRight();
  }
}

"while" loop + "if" statement

while (notFinished()) {
  moveForward();
  if (isPathLeft()) {
    turnLeft();
  }
}

"while" loop + "if" statement

while (notFinished()) {
  moveForward();
  if (isPathRight()) {
    turnRight();
  }
}

"if...else" statements

while (notFinished()) {
  if (isPathForward()) {
    moveForward();
  } else {
    turnLeft();
  }
}

"if...else" + nesting

while (notFinished()) {
  if (isPathForward()) {
    moveForward();
  } else {
    if (isPathRight()) {
      turnRight();
    } else {
      turnLeft();
    }
  }
}

more nesting

while (notFinished()) {
  if (isPathForward()) {
    moveForward();
  } else {
    if (isPathRight()) {
      turnRight();
    } else {
      turnLeft();
    }
  }
}

congratulations!

Questions?

Made with Slides.com