aboutsummaryrefslogtreecommitdiff
path: root/files/pt-pt/web/javascript/reference/statements/for/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/pt-pt/web/javascript/reference/statements/for/index.html')
-rw-r--r--files/pt-pt/web/javascript/reference/statements/for/index.html146
1 files changed, 146 insertions, 0 deletions
diff --git a/files/pt-pt/web/javascript/reference/statements/for/index.html b/files/pt-pt/web/javascript/reference/statements/for/index.html
new file mode 100644
index 0000000000..687cd23cdc
--- /dev/null
+++ b/files/pt-pt/web/javascript/reference/statements/for/index.html
@@ -0,0 +1,146 @@
+---
+title: for
+slug: Web/JavaScript/Reference/Statements/for
+tags:
+ - Declaração
+ - Funcionalidade de Linguagem
+ - JavaScript
+ - Loop
+ - Referencia
+ - Repetição
+ - for
+translation_of: Web/JavaScript/Reference/Statements/for
+original_slug: Web/JavaScript/Reference/Extratos_e_declarações/for
+---
+<div>{{jsSidebar("Statements")}}</div>
+
+<p>A <strong>declaração "for"</strong> cria uma repetição (<em>loop</em>) que consiste de três expressões opcionais, entre parênteses e separados por ponto e vírgula, seguido de uma declaração (normalmente <a href="/pt-PT/docs/Web/JavaScript/Reference/Extratos_e_declarações/bloco">bdeclaraçãod e bloco (<em>block</em>)</a>) para ser executada na repetição.</p>
+
+<div>{{EmbedInteractiveExample("pages/js/statement-for.html")}}</div>
+
+
+
+<h2 id="Sintaxe">Sintaxe</h2>
+
+<pre class="syntaxbox">for ([<em>initialization</em>]; [<em>condition</em>]; [<em>final-expression</em>])
+ <em>statement</em></pre>
+
+<dl>
+ <dt><code>initialization</code></dt>
+ <dd>An expression (including assignment expressions) or variable declaration evaluated once before the loop begins. Typically used to initialize a counter variable. This expression may optionally declare new variables with <code>var</code> or <code>let</code> keywords. Variables declared with <code>var</code> are not local to the loop, i.e. they are in the same scope the <code>for</code> loop is in. Variables declared with let are local to the statement.</dd>
+ <dd>The result of this expression is discarded.</dd>
+ <dt><code>condition</code></dt>
+ <dd>An expression to be evaluated before each loop iteration. If this expression evaluates to true, <code>statement</code> is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the <code>for</code> construct.</dd>
+ <dt><code>final-expression</code></dt>
+ <dd>An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of <code>condition</code>. Generally used to update or increment the counter variable.</dd>
+ <dt><code>statement</code></dt>
+ <dd>A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a {{jsxref("Statements/block", "block", "", 0)}} statement (<code>{ ... }</code>) to group those statements. To execute no statement within the loop, use an {{jsxref("Statements/empty", "empty", "", 0)}} statement (<code>;</code>).</dd>
+</dl>
+
+<h2 id="Exemplos">Exemplos</h2>
+
+<h3 id="Using_for">Using <code>for</code></h3>
+
+<p>The following <code>for</code> statement starts by declaring the variable <code>i</code> and initializing it to <code>0</code>. It checks that <code>i</code> is less than nine, performs the two succeeding statements, and increments <code>i</code> by 1 after each pass through the loop.</p>
+
+<pre class="brush: js">for (let i = 0; i &lt; 9; i++) {
+ console.log(i);
+ // more statements
+}
+</pre>
+
+<h3 id="Optional_for_expressions">Optional <code>for</code> expressions</h3>
+
+<p>All three expressions in the head of the <code>for</code> loop are optional.</p>
+
+<p>For example, in the <em>initialization</em> block it is not required to initialize variables:</p>
+
+<pre class="brush: js">var i = 0;
+for (; i &lt; 9; i++) {
+ console.log(i);
+ // more statements
+}
+</pre>
+
+<p>Like the <em>initialization</em> block, the <em>condition</em> block is also optional. If you are omitting this expression, you must make sure to break the loop in the body in order to not create an infinite loop.</p>
+
+<pre class="brush: js">for (let i = 0;; i++) {
+ console.log(i);
+ if (i &gt; 3) break;
+ // more statements
+}</pre>
+
+<p>You can also omit all three blocks. Again, make sure to use a {{jsxref("Statements/break", "break")}} statement to end the loop and also modify (increase) a variable, so that the condition for the break statement is true at some point.</p>
+
+<pre class="brush: js">var i = 0;
+
+for (;;) {
+ if (i &gt; 3) break;
+ console.log(i);
+ i++;
+}
+</pre>
+
+<h3 id="Using_for_without_a_statement">Using <code>for</code> without a statement</h3>
+
+<p>The following <code>for</code> cycle calculates the offset position of a node in the <em>final-expression</em> section, and therefore it does not require the use of a <code>statement</code> section, a semicolon is used instead.</p>
+
+<pre class="brush: js">function showOffsetPos(sId) {
+
+ var nLeft = 0, nTop = 0;
+
+ for (
+
+ var oItNode = document.getElementById(sId); /* initialization */
+
+ oItNode; /* condition */
+
+ nLeft += oItNode.offsetLeft, nTop += oItNode.offsetTop, oItNode = oItNode.offsetParent /* final-expression */
+
+ ); /* semicolon */
+
+ console.log('Offset position of \'' + sId + '\' element:\n left: ' + nLeft + 'px;\n top: ' + nTop + 'px;');
+
+}
+
+/* Example call: */
+
+showOffsetPos('content');
+
+// Output:
+// "Offset position of "content" element:
+// left: 0px;
+// top: 153px;"</pre>
+
+<div class="note"><strong>Note:</strong> This is one of the few cases in JavaScript where <strong>the semicolon is mandatory</strong>. Indeed, without the semicolon the line that follows the cycle declaration will be considered a statement.</div>
+
+<h2 id="Especificações">Especificações</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Especificação</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-for-statement', 'for statement')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Compatibilidade_de_navegador">Compatibilidade de navegador</h2>
+
+
+
+<p>{{Compat("javascript.statements.for")}}</p>
+
+<h2 id="Consulte_também">Consulte também</h2>
+
+<ul>
+ <li>{{jsxref("Statements/empty", "empty statement", "", 0)}}</li>
+ <li>{{jsxref("Statements/break", "break")}}</li>
+ <li>{{jsxref("Statements/continue", "continue")}}</li>
+ <li>{{jsxref("Statements/while", "while")}}</li>
+ <li>{{jsxref("Statements/do...while", "do...while")}}</li>
+ <li>{{jsxref("Statements/for...in", "for...in")}}</li>
+ <li>{{jsxref("Statements/for...of", "for...of")}}</li>
+</ul>