Control Flow: Loops & Conditionals

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/9

encourage image

There's no tags or description

Looks like no tags are added yet.

10 Terms

1
New cards

What is Control Flow?

•By default, code runs top to bottom once

•Control flow lets a program make decisions (conditionals) and repeat work (loops).

•Two questions it answers:

1.“Should I run this?

2.“How many times?”

•Analogy: IF the oven is hot, bake it; REPEAR stirring five times


2
New cards

Booleans & Comparison Operators

•A Boolean is either true or false, the result of every condition

•Comparison Operators return a Boolean:

•let age = 20

•age >= 18;  // true

•age === 20 // true (checks value AND type)

•“5” == 5      // true (avoid: converts types)

•“5” === 5   // false (strict: prefer this)


Rule of thumb: ALWAYS use === and !==


3
New cards

If / else statements

•Runs a block only when a condition is true.

•Only the first matching block runs; else is the catch-all.


4
New cards

Logical Operators

●Logical operators are symbols or words like AND, OR, and NOT, used to combine or change true/false statements.

●AND (Conjunction): Returns true only if all inputs are true. &&

●OR (Disjunction): Returns true if at least one input is true. ||

●NOT (Negation): Reverses the truth value of the input. !


●They test multiple conditions at the same time to give a final true or false answer.


5
New cards

Switch Statements

●A switch statement is a control flow mechanism used in programming to execute a specific block of code from multiple options based on the value of a single expression.

6
New cards

Expression

●The variable or value evaluated exactly once at the start.

7
New cards

Case

●A specific value compared against the expression. If it matches, its code block runs.

8
New cards

Break

●A keyword that exits the switch block. Omitting it causes "fall-through," executing subsequent cases automatically.

9
New cards

Default

●An optional catch-all block that runs if no case matches the expression.


10
New cards

Loops

A for loop repeats a block of code a specific number of times.

A while loop is a code tool that runs a block of commands repeatedly as long as a certain rule or condition stays true. a code tool that runs a block of commands repeatedly as long as a certain rule or condition stays true. It checks the rule first, runs the code if the rule is true, and stops when the rule becomes false.