Control Structures

CSCI-UA.0480-008

Control Structures

  • as you might expect, JavaScript programs are generally executed line-by-line from top to bottom
  • but there are ways to control that flow of execution (of course)!
    • conditionals
    • repetition
    • creating a function (to jump to the code in the function definition)
    • (we'll see others as well)

Conditionals and Loops

This stuff should look really familiar

  • we'll go through the following material quickly because…
  • you know all of these concepts already
  • and it looks a lot like Java/C/C++

Again With the Definitions

  • boolean expression - an expression that results in a boolean value
    • 423 === 423
      • JavaScript ❤s coercing values for you; a value will be coaxed into boolean when the need arises
    • !!5
      • logical not, logical not, number 5 … ¯\_(ツ)_/¯
  • block - a sequence of grouped statements bound by curly braces - {'s and }'s
  • iteration - repeated execution of a set of programming statements
  • loop - the construct that allows allows us to repeatedly execute a statement or a group of statements until a terminating condition is satisfied

If and Else

Conditionally execute a block of code (should look familiar):

  • keyword if followed by a boolean expression enclosed in parentheses
  • block of code to be executed if expression is true
  • optional additional else if's to chain conditionals, with corresponding block of code to execute
  • optional else, with corresponding block of code to execute

if (some_boolean_expression) {
	// do stuff here if expression is true
}

if (some_boolean_expression) {
	// do stuff 
} else if (another_boolean_expression) {
	// do other stuff
} else {
	// do other other stuff
}

A For Loop

Again, should look familiar… repeatedly execute a block of code:


//    initialization
//    |        condition
//    |        |       afterthought/increment
//    |        |       |
for(let i = 0; i <= 5; i = i + 1) {
	console.log(i);
}

// (i++ works too, of course)

Speaking JavaScript calls the three parts: initialization, check and update.

Hey - notice that let in front of the loop variable declaration? Do that.

A While Loop

Conditionally repeat a block of code:


while (boolean_expression) {
	// repeat this stuff as long as boolean expression is true
}


Conditionally repeat a block of code and ensure that code is executed at least once:


do {
	// repeat this stuff at least once
} while (boolean_expression)

Break and Continue

The keyword break immediately stops the execution of a loop:


for (let num = 1; num < 30; num++) { 
	if (num % 7 == 0 && num % 3 == 0)
		break; 
	console.log(num);
}

The keyword continue stops the current iteration and immediately skips to the next one:


for(let num = 1; num < 30; num++) { 
	if (num % 7 == 0 && num % 3 == 0)
		continue; 
	console.log(num);
}

What is the output of the above programs?

1 through 20 and 1 through 29 skipping 21 respectively

Switch-Case

Execute code based on value of switch.


var day = "thu";
switch (day) { 
	case "fri":
		console.log("Friday");
		break;
	case "thu":
		console.log("Thursday");
		break;
	case "wed":
		console.log("Wednesday");
		break;
}

What is the output of the above programs?

Thursday

Lastly, a Preview of Functions

One way to define a function in JavaScript is to create a variable, and set it equal to a function:


var f = function(x) {
	return x
}

Some Quick Exercises

Some silly practice programs to get you warmed up

FizzBuzz

From Eloquent JavaScript:

  • write a program that uses console.log to print all the numbers from 1 to 100
  • for numbers divisible by 3, print "Fizz" instead of the number
  • for numbers divisible by 5 (and not 3), print "Buzz" instead
  • for numbers divisible by both 3 and 5, print "FizzBuzz" instead