Pre Javascript Part 2

Programmer Terms: Arrays, Conditionals, Loops

Arrays 1

Variable

  • An object that can store a value for later use.
  • Question: Where are variables stored?
  • On the RAM (specifically the stack or heap)
  • Question: What are some things we can store?
  • Numbers, letters, strings, True or false.
  • Question: What are a few ways to store numbers?
  • Integer = Whole numbers (1, 2, 3)
  • Float = Real numbers(1.3, 3.12, 0.23)

Arrays 2

Arrays

  • Arrays can store multiple bits of data in one variable.
  • Question: What data types can be stored?
  • In most languages - Any
  • Question: Can an Array hold different types of data?
  • Lots of Languages can but some like C can not.
  • Question: What does an array look like in JS?
  • Var integerArray = [1, 2, 4, 2, 58];
  • Question: How would I access the number 4 from an array?
  • console.log(integerArray[2]);

Conditionals

A conditional is a branch in the code

Question: name any conditional types you know

If, Case, Switch, ifelse, ifelseif

All conditionals break down to...

Either true or false

  • Question: in an if else if conditional if the first conditional is true will it still check the following conditionals?

 

  • A conditional you might use.
If length of name is 0 {
    reply "You gave me no name
} else {
    reply "Hello name"
}

NO

Loops

Computers do three things much faster or better then humans:

Math, Memory, and Repetition

 

Looping allows a computer to repeat a segment of code as long
 as the developer wishes depending on the need.

Incoming geek joke...

Question: What are some kinds of Loops?

 

For, While, Do While

Javascript

C

function punishmentCheat(sentence) {
    for (var count = 0; count < 500; count++) {
        console.log(sentence);
    }
}

punishmentCheat("I will not throw paper airplanes in class.");
    

For Loop

A for loop runs until a conditional is met

Notes:

  • will check conditional before running once
  • after each pass will run a segment of code
  • Most common infinite loop:
    • need to count down to 0 code segment goes up
function punishmentCheat(sentence) {
    for (var i = 0; count < 500; count++) {
        console.log(sentence);
    }
}

punishmentCheat("I will not throw paper airplanes in class.");
    

While Loop

A while loop runs while a conditional is true

Notes:

  • will check conditional before running code
  • Does not run any code after each loop
    • conditional must be met within loop segment
  • Most common infinite loop:
    • forget to match conditional in code segment
function punishmentCheat(sentence) {
    count = 0;
    while (count < 500) {
        console.log(sentence);
        count++;
    }
}

punishmentCheat("I will not throw paper airplanes in class.");
    

Infinite While Loop

Do While Loop

A do while loop runs while a conditional is true

Notes:

  • Will check conditional at end of loop segment
  • Does not run any code after each loop
    • conditional must be met within loop segment
  • Most common infinite loop:
    • forget to match conditional in code segment
function punishmentCheat(sentence) {
    count = 0;
    do {
        console.log(sentence);
        count++;
    } while (count < 500) 
}

punishmentCheat("I will not throw paper airplanes in class.");
    

Workshop challenge

https://www.hackerrank.com/contests/master/challenges/c-tutorial-conditional-if-else?

Made with Slides.com