Medtem ko je zanka znana tudi kot vnaprej testirana zanka. Na splošno zanka while omogoča, da se del kode izvede večkrat, odvisno od danega logičnega pogoja. Nanj lahko gledamo kot na ponavljajoči se stavek if. Zanka while se večinoma uporablja v primeru, ko število ponovitev ni vnaprej znano.
Sintaksa zanke while v jeziku C
Sintaksa zanke while v jeziku c je podana spodaj:
while(condition){ //code to be executed }
Diagram poteka zanke while v C
Primer zanke while v jeziku C
Oglejmo si preprost program zanke while, ki natisne tabelo 1.
#include int main(){ int i=1; while(i<=10){ printf('%d ',i); i++; } return 0; < pre> <h4>Output</h4> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h2>Program to print table for the given number using while loop in C</h2> <pre> #include int main(){ int i=1,number=0,b=9; printf('Enter a number: '); scanf('%d',&number); while(i<=10){ printf('%d ',(number*i)); i++; } return 0; < pre> <h4>Output</h4> <pre> Enter a number: 50 50 100 150 200 250 300 350 400 450 500 </pre> <pre> Enter a number: 100 100 200 300 400 500 600 700 800 900 1000 </pre> <hr> <h2>Properties of while loop</h2> <ul> <li>A conditional expression is used to check the condition. The statements defined inside the while loop will repeatedly execute until the given condition fails.</li> <li>The condition will be true if it returns 0. The condition will be false if it returns any non-zero number.</li> <li>In while loop, the condition expression is compulsory.</li> <li>Running a while loop without a body is possible.</li> <li>We can have more than one conditional expression in while loop.</li> <li>If the loop body contains only one statement, then the braces are optional.</li> </ul> <h4>Example 1</h4> <pre> #include void main () { int j = 1; while(j+=2,j<=10) { printf('%d ',j); } printf('%d',j); < pre> <h4>Output</h4> <pre> 3 5 7 9 11 </pre> <h4>Example 2</h4> <pre> #include void main () { while() { printf('hello Javatpoint'); } } </pre> <h4>Output</h4> <pre> compile time error: while loop can't be empty </pre> <h4>Example 3</h4> <pre> #include void main () { int x = 10, y = 2; while(x+y-1) { printf('%d %d',x--,y--); } } </pre> <h4>Output</h4> <pre> infinite loop </pre> <h2>Infinitive while loop in C</h2> <p>If the expression passed in while loop results in any non-zero value then the loop will run the infinite number of times.</p> <pre> while(1){ //statement } </pre></=10)></pre></=10){></pre></=10){>
Program za tiskanje tabele za dano število z uporabo zanke while v C
#include int main(){ int i=1,number=0,b=9; printf('Enter a number: '); scanf('%d',&number); while(i<=10){ printf(\'%d \',(number*i)); i++; } return 0; < pre> <h4>Output</h4> <pre> Enter a number: 50 50 100 150 200 250 300 350 400 450 500 </pre> <pre> Enter a number: 100 100 200 300 400 500 600 700 800 900 1000 </pre> <hr> <h2>Properties of while loop</h2> <ul> <li>A conditional expression is used to check the condition. The statements defined inside the while loop will repeatedly execute until the given condition fails.</li> <li>The condition will be true if it returns 0. The condition will be false if it returns any non-zero number.</li> <li>In while loop, the condition expression is compulsory.</li> <li>Running a while loop without a body is possible.</li> <li>We can have more than one conditional expression in while loop.</li> <li>If the loop body contains only one statement, then the braces are optional.</li> </ul> <h4>Example 1</h4> <pre> #include void main () { int j = 1; while(j+=2,j<=10) { printf(\'%d \',j); } printf(\'%d\',j); < pre> <h4>Output</h4> <pre> 3 5 7 9 11 </pre> <h4>Example 2</h4> <pre> #include void main () { while() { printf('hello Javatpoint'); } } </pre> <h4>Output</h4> <pre> compile time error: while loop can't be empty </pre> <h4>Example 3</h4> <pre> #include void main () { int x = 10, y = 2; while(x+y-1) { printf('%d %d',x--,y--); } } </pre> <h4>Output</h4> <pre> infinite loop </pre> <h2>Infinitive while loop in C</h2> <p>If the expression passed in while loop results in any non-zero value then the loop will run the infinite number of times.</p> <pre> while(1){ //statement } </pre></=10)></pre></=10){>
Enter a number: 100 100 200 300 400 500 600 700 800 900 1000
Lastnosti zanke while
- Za preverjanje pogoja se uporablja pogojni izraz. Stavki, definirani v zanki while, se bodo ponavljali, dokler dani pogoj ne bo izpolnjen.
- Pogoj bo resničen, če vrne 0. Pogoj bo napačen, če vrne katero koli število, ki ni nič.
- V zanki while je izraz pogoja obvezen.
- Izvajanje zanke medtem brez telesa je možno.
- V zanki while imamo lahko več kot en pogojni izraz.
- Če telo zanke vsebuje samo en stavek, potem oklepaji niso obvezni.
Primer 1
#include void main () { int j = 1; while(j+=2,j<=10) { printf(\'%d \',j); } printf(\'%d\',j); < pre> <h4>Output</h4> <pre> 3 5 7 9 11 </pre> <h4>Example 2</h4> <pre> #include void main () { while() { printf('hello Javatpoint'); } } </pre> <h4>Output</h4> <pre> compile time error: while loop can't be empty </pre> <h4>Example 3</h4> <pre> #include void main () { int x = 10, y = 2; while(x+y-1) { printf('%d %d',x--,y--); } } </pre> <h4>Output</h4> <pre> infinite loop </pre> <h2>Infinitive while loop in C</h2> <p>If the expression passed in while loop results in any non-zero value then the loop will run the infinite number of times.</p> <pre> while(1){ //statement } </pre></=10)>
Primer 2
#include void main () { while() { printf('hello Javatpoint'); } }
Izhod
compile time error: while loop can't be empty
Primer 3
#include void main () { int x = 10, y = 2; while(x+y-1) { printf('%d %d',x--,y--); } }
Izhod
infinite loop
Infinitiv while zanka v C
Če izraz, posredovan v zanki while, povzroči katero koli vrednost, ki ni nič, se bo zanka zagnala neskončno število krat.
while(1){ //statement }=10)>=10){>=10){>