Please enable JavaScript.
Coggle requires JavaScript to display documents.
JS M4, FOR LOOPS, WHILE LOOPS, CONDITIONAL STATEMENTS, MORE ON CONDITIONAL…
JS M4
Computers are capable of making decisions, and help us solve real-world problems because they are very good at following instructions. you'll learn to control the flow of instructions a computer follows, using 3 techniques: sequencing, iteration and selection.
You already know sequencing. It means that the computer runs your code in order, from top to bottom.
-
-
Machines can complete complex tasks for us, but first they need to know how.
An algorithm is a set of step-by-step instructions to complete a task, placed in a certain order.
For example, flowcharts help to visualize algorithms
-
Another way to represent an algorithm is with pseudocode. Pseudocode is a simplified language that is a bit closer to a programming language.
Algorithms can be represented in different ways. If you are new to algorithms, natural language is a good place to start.
Humans use code to communicate with machines. For machines to complete a task, the instructions need to be…
FOR LOOPS
For loops are shorter to write. The syntax for the for loop condenses the following into 1 single line:
-
-
-
for loops are powerful because they can be used when the number of iterations to solve a problem is fixed and known
A for loop is used to execute the same block of code over and over again, a specific number of times.
This code will show 3 alert messages, one after the other Ex : for (let i = 0; i < 3; i++) { alert("Hello");
The counter variable is defined with an initial value. The variable is updated in each iteration to count the number of times the code block is repeated.
A counter variable (i) is defined, initialized and updated inside the parentheses, following the for keyword.
-
Initialization, condition and updating are separated by semicolons(;)
When the condition no longer holds true, we exit the for loop
In the for loop, the counter variable is defined within the scope of the for loop. It can only be used inside the loop.
-
After the computer has finished executing the loop, it'll continue to execute any following statements in sequence.
In general, use for loops when you already know the number of iterations, and while loops when there is a condition that needs to be met.
WHILE LOOPS
Iteration is used to automate tasks that need to be done over and over again. Iteration makes your programs simpler, faster and reduces errors. you’ll learn to use the while loop to build iteration into your code.
-
-
Loops frequently include counters. A counter is a variable that keeps track of the number of iterations.
Counter variables are updated inside the loop, so they change with every iteration.
An initial value is set outside the loop, as the starting point. This is called initialization
The counter keeps track of the number of iterations. The counter is updated inside the loop with each iteration. The counter can be given any initial value.
The condition is included in parentheses. When the condition no longer holds true, we exit the while loop
-
You can use different names for the counter variable. You can update the counter in different ways to solve different problems.
With while loops you can run into what is known as an infinite loop. This is when the condition holds true forever, and the code never stops repeating.
CONDITIONAL STATEMENTS
-
Conditional statements, or if-else statements, allow programs to perform different actions based on the conditions.
The if conditional statements starts with the keyword if followed by the condition inside parentheses and a code block
The code block below the if line is executed when the condition is met (true)
-
The else conditional statement starts with the keyword else followed code block.
The code block below the else line is executed when the condition is not met (false)
-
-
Booleans are used to track if a condition is met. You can also use the status of an input in conditional statements if (isOver18 === true) { x.textContent = "Discount: 10$";
When using boolean values directly in conditions, we can make our code more concise.
-
ADVANCED CONDITIONS
Web developers create code that can make decisions and take actions based on different scenarios. you’ll learn to build any type of condition into your code to solve real-life problems.
-
You can build programs that make more complex decisions if you combine logical and comparison operations.
Remember that you can store boolean values in variables just like you do with other data types.
Websites and apps offer a dark mode