While Iteration in Apps Script
This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author
Table of Contents
Summary
In this blog post, we will explore the usage of the while loop in Apps Script. We will learn how to create a while loop, control the loop execution with a condition, and increment the loop counter. The while loop allows us to repeat a set of statements as long as a certain condition is true.
Code
1 2 3 4 5 6 7 |
function whileIteration() { let i = 1 while (i < 11) { Logger.log(i) i++ } } |
Code Explanation
The code defines a function whileIteration
which demonstrates the usage of a while loop in Apps Script. Inside Before the while loop, a variable i
is initialized to 1. The loop will continue executing as long as i
is less than 11.
Within each iteration of the loop, the current value of i
is logged using the Logger.log
statement. The loop counter i
is then incremented by 1 using the i++
statement.
Example
When you execute the whileIteration
function, the numbers from 1 to 10 will be logged in the Apps Script logger.
AI Prompt
Write a function that uses a while loop to log the numbers from 1 to 10. Make sure to initialize the loop counter variable, set the condition for the loop execution, and increment the counter within the loop.
A function named whileIteration
should be created to implement the above requirements.
The for statement and the while statement are both types of loop statements, and are used to repeatedly execute a block of code while satisfying a specific condition. However, there are differences in how the two loops work and when they are used.
1. for statement
– The for statement is mainly used when the number of iterations is fixed.
– It consists of an initialization, conditional expression, incremental expression, and each part is separated by a semicolon.
– Initialization: Initialize the variable at the beginning of the loop.
– Conditional expression: Checks the condition under which the loop statement will be executed. A repeating block of code is executed only if the conditional expression is true.
– Incremental: increments or decrements a variable after a repeating code block is executed.
– The for statement is often used when the number of iterations is fixed or when iterating through each element of a data structure such as an array.
2. while statement
– while statement repeats as long as the conditional expression is true.
– Used when the number of iterations is not fixed or needs to be repeated until a certain condition is met.
– If the conditional expression is false from the beginning, the repeating code block may not be executed.
Therefore, the for statement is used when the number of iterations is fixed, and the while statement is used when the number of repetitions is not fixed or needs to be repeated according to a condition. You can write more efficient code by choosing and using the right loop in the right situation.