Prehľad Pokračovať v JavaScripte
Nasledujúci článok poskytuje prehľad programu Pokračovať v jazyku JavaScript. Príkaz Pokračovanie ukončí vykonanie aktuálnej iterácie v označenej slučke. Preskočí iteráciu v slučke. Príkaz Pokračovať riadi tok slučky. Používa sa v slučkách while Loop, Do while Loop a For Loop. Keď sa vykonáva program, pri kompilácii, ak je prítomný príkaz na pokračovanie, slučka sa zastaví pri súčasnej iterácii a začne novú iteráciu.
syntax
Continue;
Iná metóda používajúca odkaz na štítok
Continue labelname;
Vývojový diagram
Ako funguje príkaz Continue Statement v JScript?
Príkaz Pokračovať preskočí iteráciu. Ak sa vyskytne podmienka uvedená v cykle, táto podmienka preruší a pokračuje s nasledujúcou iteráciou. Príkaz Pokračovať bez alebo s referenčným štítkom preskočí iba jednu iteráciu slučky.
Keď dáme konkrétne podmienky pre cyklus, ak je to podmienka, zatiaľ čo a urobíme, keď je slučka. Príkaz Continue vynechá podmienku uvedenú v slučke a vykoná ďalšiu iteráciu.
1. Použitie pre a pre slučky
Príklad č. 1
Výkon:
Príklad č. 2
Continue statement
Continue Statement
var j;
for(j = 1; j <= 10; j++)
(
if (j % 2 !== 0)
(
document.write("
Odd Numbers = " + j +"(Continue stmt skipped)");
continue;
)
document.write("
Even Numbers = " + j);
)
Continue statement
Continue Statement
var j;
for(j = 1; j <= 10; j++)
(
if (j % 2 !== 0)
(
document.write("
Odd Numbers = " + j +"(Continue stmt skipped)");
continue;
)
document.write("
Even Numbers = " + j);
)
Continue statement
Continue Statement
var j;
for(j = 1; j <= 10; j++)
(
if (j % 2 !== 0)
(
document.write("
Odd Numbers = " + j +"(Continue stmt skipped)");
continue;
)
document.write("
Even Numbers = " + j);
)
Continue statement
Continue Statement
var j;
for(j = 1; j <= 10; j++)
(
if (j % 2 !== 0)
(
document.write("
Odd Numbers = " + j +"(Continue stmt skipped)");
continue;
)
document.write("
Even Numbers = " + j);
)
Výkon:
V uvedenom príklade sme napísali program, ktorý vytlačí iba párne číslo pomocou príkazu pokračovať. Vyššie uvedený kód bude ignorovať nepárne čísla pomocou príkazu pokračovať a vytlačí iba párne čísla.
2. Použitie nepárnych čísel počas sledovania slučky
príklad
JavaScript while statement: Example-1
while statement
JavaScript while statement: Example-1
while statement
JavaScript while statement: Example-1
while statement
JavaScript while statement: Example-1
while statement
Zoznam nepárnych čísel
var x = 1;
var y = 0;
var z = 0;
document.getElementById ("result"). internalHTML = "Zoznam nepárnych:";
zatiaľ čo (x <= 10)
(
z = x% 2;
ak (z! == 0)
(
var newParagraph1 = document.createElement ("p");
var newText1 = document.createTextNode (x);
newParagraph1.appendChild (newText1);
document.body.appendChild (newParagraph1);
y = y + x;
)
x ++;
)
Výkon:
3. Pokračujte vo výpise do slučky
príklad
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
continue statement in Loop
While Loop
var text = "";
var i = 1;
while (i < 6) (
text += "educba " + i+"
";
i++;
)
document.getElementById("ghr").innerHTML
= text;
Continue Do While Loop
var text = ""
var i = 1;
do (
text += "educba " + i+"
";
i++;
)
while (i < 6);
document.getElementById("GF1").innerHTML = text;
Výkon:
4. Použitie v prípade stavu
príklad
If condition
var text = "";
var j;
for(j = 0; j < 10; j++)
(
if(j === 5)
(
continue;
)
text += + j + "
";
)
document.getElementById("continue if condition").innerHTML = text;
Výkon:
Vo vyššie uvedenom príklade sme použili príkaz Pokračovať v prípade, že je v stave. Ak spustíme program z 0 na 9, vynechá sa iba 5. Vo výstupe budú vytlačené všetky čísla.
5. JavaScript Pokračujte vo vyhlásení
príklad
Continue Label Statement
for(var j = 1; j < 5; j++)(
inner:
for (var k = 0; k < 5; k++)(
if (k == 2)(
document.writeln("skip");
continue inner;
)
document.writeln("j : " + j + ", k :" + k);
)
document.writeln();
)
Výkon:
6. Pokračujte bez vyhlásenia
príklad
Example for continue
Resulting value y
document.getElementById("Example for continue").innerHTML =
'var j; var x = 0;
' +
'for (j=1; j<=4; j++) (
' +
' if (j===3) ( continue )
' +
' x = x + j;
' +
')';
var j;
var y = 0;
for (j=1; j<=4; j++) (
if (j===3)
( continue
)
y = y + i;
)
document.getElementById("Results").innerHTML = y;
|
|
---|---|
|
|
Example for continue
Resulting value y
document.getElementById("Example for continue").innerHTML =
'var j; var x = 0;
' +
'for (j=1; j<=4; j++) (
' +
' if (j===3) ( continue )
' +
' x = x + j;
' +
')';
var j;
var y = 0;
for (j=1; j<=4; j++) (
if (j===3)
( continue
)
y = y + i;
)
document.getElementById("Results").innerHTML = y;
Výkon:
Vo vyššie uvedenom programe je výsledok 7.
7. Pokračujte vo výpise pomocou For, If Loop
príklad
continue statement for if Loop
continue statement for if Loop
continue statement for if Loop
continue statement for if Loop
Výkon
var newPara = document.createElement ("p");
var newText = document.createTextNode ('Zoznam nepárnych čísel');
newPara.appendChild (newText);
document.body.appendChild (newPara);
var z = 0;
var y = 0;
pre (y = 1; y <= 10; y ++)
(
if (y% 2 === 0)
(
ďalej;
)
var newPara = document.createElement ("p");
var newText1 = document.createTextNode (y);
newPara.appendChild (newText1);
document.body.appendChild (newPara);
)
Výkon:
Záver - Pokračujte v JavaScript
Pokračovanie sa dá použiť na skok z jednej podmienky alebo iterácie na získanie požadovaného výstupu. Existuje niekoľko spôsobov, ako použiť príkaz „pokračovať“ v programe. V slučkách môžeme dať určité podmienky a pomocou príkazov pokračovania preskočiť tieto podmienky iba v konkrétnom čase. Tu môžete použiť príkaz Pokračovať na vykonanie zmien vo výstupe. Predstavte si, že chcete vytlačiť iba párne čísla. Príkaz Continue potom môže byť použitý na tlač iba párnych čísel z vyššie uvedeného programu. V každej iterácii musíme skontrolovať, či je j nepárne. Ak je j nepárne, mali by sme iteráciu prerušiť pomocou príkazu „pokračovať“. Ak je j párne, pokračujeme v iterácii a pintujeme iba párne hodnoty.
Odporúčané články
Bol to sprievodca Pokračovať v JavaScripte. Tu diskutujeme o syntaxi, vývojovom diagrame a o tom, ako funguje príkaz Continue Statement v jazyku JavaScript? Ďalšie informácie nájdete aj v nasledujúcich článkoch -
- Kľúčové slová jazyka JavaScript
- Pokračovať vo vyhlásení v C ++
- Kompilátory JavaScriptu
- Prípad v jazyku JavaScript