Skip to content

Programming Fundamentals

hkara edited this page Nov 25, 2015 · 3 revisions

Before we get started, there are a few fundamental topics that we will briefly explain that will greatly improve the functionality of your programs:

If Statements

If statements let you conditionally execute blocks of code.

Example:

if (x == 1) {
  // if x equals 1, then do this
} else if (x < 3) {
  // if x does not equal 1, but is less than 3, do this
} else {
  // if all else fails, do this
}

We have some options as to what we put in the brackets for if, else if and else.

Conditionals

Assignment vs. Equality
= indicates a value is assigned to certain variable
== is a conditional that tests for the equality between two variables or values

> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
!= is does not equal

Combining conditionals

&& means AND

  • Both conditions must be met in order to // do this Example:
if (x == 3 && y == 5) {
  // do this
}

|| means OR

  • At least one of the conditions must be met in order to // do this Example:
if (x == 3 || y == 5) {
  // do this
}

! means NOT

  • The condition must NOT be met in order to // do this Example:
if (!(y == 5)) {
  // do this
}

Nesting If Statements

If statements can be nested within other if, else if, and else statements
Example:

if (m == 4) {
  // if m is equal to 4 and x can be anything, do this
  if (x == 6) {
    // if m is equal to 4 and x is equal to 6, do this
  }
}

Loops

Loops allow you to run blocks of code while a condition is met

While loop

Example:

var x = 0;
while (x < 10) {
  console.log(x);
  x = x + 1;
}

This will log the values 0 through 9. On the 10th loop, the while loop will not execute because x will be equal to 10. The condition x < 10 would not be met.

Do While loop

Example:

var x = 0;
do {
  console.log(x);
  x = x + 1;
}
while (x < 0);

This will log the value 0. It executes the loop once before checking if the condition in the while loop is met. x < 0 wouldn't have been met in a while loop, but because we used a do while loop, the code gets execited at least once.

For loop

Example:

for (i = 0; i < 5; i++) {
    console.log(i);
}

i is initialized as 0, the for loop checks that i is less than 5 before executing, and i increments by 1 each time the loop is executed.
The values 0 through 4 will be logged.

Note: There are other loops that exist.

##Functions

A function perferoms a specific task.
Function names follow the same conventions as variables.
In Java Script, functions are defined by the function keyword followed by the name of the function and ()
For example:

function calculateSum(number1, number2) {
  var sum = number1 + number 2;
  return sum;
}
var x = calculateSum(3,6);

number1, number2 are called parameters to the function while the values it receives, 3, 6 are called arguments.
The return statement indicates the end of the function.
Functions usually compute a value which is returned to the "caller".
In this case, x will have a value of 9.

Functions are a very useful tool in programming.They allow us to basically reuse a block of code to perform a specific task, but also allow us the felxibility to change the parameters around when calling them with arguments. This prevents us from having to rewrite the same code over and over again.

We are also allowed to use function in equations, just like variables. For example, let's take the "calculateSum" function:

var z = 5;
var y = calculateSum(3,6) + calculateSum(1,2) + 1 - z;