Control Flow
What we’ll cover
- Block Scope
- Conditional Statements
- Loops
Block Scope
- A block or compound statement consists of a number of statements, surrounded by a pair of braces.
- Blocks define the scope of your variables.
- Blocks can be nested
- Redeclaring identically named variables in two nested blocks will re-assign the variable to the most recently executed assignment statement value
Conditional Statements
If Statements General Syntax
- The argument of the
ifclause must be abooleanvalue.- the value of
conditionin the example below must be of typeboolean.
- the value of
- Execution of an
ifconditionis called a check or evaluation. - The body of an
ifstatement only executes if the value of theconditionhas a value oftrue. - Control structure code created
branches, or different paths that the code can take.
if(condition){ // this is a check
statement1
statement2
}
If Statements Example
if(5 > 10){ // this is a check
print("5 is greater than 10!")
}
If/Else Statements General Syntax
If/Elsestatements are used when you would like to execute one block or the other, but never both blocks.Ifstatements do not have to be followed with anelsestatementElsestatements have to be preceded with anifstatement.
if (condition){
statement1
} else {
statement2
}
If/Else Statements Example
if/else ifstatements are used when you would like to execute one of several blocks, given that at least one condition prior to theelse ifcheck isfalse.ifstatements do not have to be paired with anelse ifstatemenstelse ifstatements have to be preceded by anifstatements orelse ifstatements.else ifblocks can only execute if the value of allconditions have a value offalse.
if (condition1){
statement1
} else if(condition2) {
statement2
}
If/Else If Statements General Syntax
if (condition){
statement1
} else if(condition2) {
statement2
} else {
statement3
}
Loops
The while loop executes a statement (which may be a block statement) while a condition is true
while(condition){
statement
}
The while loop will never execute if the condition is false at the outset
If you want to make sure a block is executed at least once, ensure the initial condition is set to True, then internally changed to False to break out of the loop
continueCondition = true
while(continueCondition){
if(breakCondition){
continueCondition = False
}
}
Determinate Loops
The for loop is a general construct to support iteration controlled by a counter or similar variable that is updated after every iteration.
for(let i=0; i<10; i++) {
console.log(i);
}
Nested Loops
You can have loops within loops, but be aware of variable scoping:
for(let i=0; i<10; i++) {
console.log(i);
for(let j=0; j<10; j++) {
console.log(j);
}
}
Statements That Break Control Flow
The same break statement that you use to exit a switch can also be used to break out of a loop
paymentPerYear = 10000;
interestRate = 0.05;
currentYear = 0;
balance = 0;
while (currentYear <= 100) {
balance += paymentPerYear;
interest = balance * interestRate / 100;
balance += interest;
if (balance >= goal) {
break;
}
currentYear += 1;
}
The continue statement transfers control to the header of the innermost enclosing loop
while (sum < goal){
userInputAsString = prompt("Enter a number: ")
userInputAsInteger = parseInt(userInputAsString)
if (userInputAsInteger < 0){
continue
}
sum = sum + n // not executed if n < 0W
}