logo

while petlja u C

Dok je petlja također poznata kao unaprijed testirana petlja. Općenito, while petlja dopušta da se dio koda izvrši više puta ovisno o danom booleovom uvjetu. Može se promatrati kao naredba if koja se ponavlja. While petlja se uglavnom koristi u slučaju kada broj ponavljanja nije poznat unaprijed.

Sintaksa while petlje u C jeziku

Sintaksa while petlje u c jeziku je data ispod:

 while(condition){ //code to be executed } 

Dijagram toka while petlje u C-u

dijagram toka c while petlje

Primjer while petlje u jeziku C

Pogledajmo jednostavan program while petlje koja ispisuje tablicu od 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(&apos;Enter a number: &apos;); scanf(&apos;%d&apos;,&amp;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(&apos;hello Javatpoint&apos;); } } </pre> <h4>Output</h4> <pre> compile time error: while loop can&apos;t be empty </pre> <h4>Example 3</h4> <pre> #include void main () { int x = 10, y = 2; while(x+y-1) { printf(&apos;%d %d&apos;,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 ispis tablice za zadani broj koristeći while petlju u C-u

 #include int main(){ int i=1,number=0,b=9; printf(&apos;Enter a number: &apos;); scanf(&apos;%d&apos;,&amp;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(&apos;hello Javatpoint&apos;); } } </pre> <h4>Output</h4> <pre> compile time error: while loop can&apos;t be empty </pre> <h4>Example 3</h4> <pre> #include void main () { int x = 10, y = 2; while(x+y-1) { printf(&apos;%d %d&apos;,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 

Svojstva while petlje

  • Za provjeru uvjeta koristi se uvjetni izraz. Naredbe definirane unutar while petlje više će se puta izvršavati sve dok se zadani uvjet ne ispuni.
  • Uvjet će biti istinit ako vrati 0. Uvjet će biti netočan ako vrati bilo koji broj različit od nule.
  • U while petlji izraz uvjeta je obavezan.
  • Pokretanje while petlje bez tijela je moguće.
  • Možemo imati više od jednog uvjetnog izraza u while petlji.
  • Ako tijelo petlje sadrži samo jednu izjavu, zagrade su opcionalne.

Primjer 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(&apos;hello Javatpoint&apos;); } } </pre> <h4>Output</h4> <pre> compile time error: while loop can&apos;t be empty </pre> <h4>Example 3</h4> <pre> #include void main () { int x = 10, y = 2; while(x+y-1) { printf(&apos;%d %d&apos;,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)>

Primjer 2

 #include void main () { while() { printf(&apos;hello Javatpoint&apos;); } } 

Izlaz

 compile time error: while loop can&apos;t be empty 

Primjer 3

 #include void main () { int x = 10, y = 2; while(x+y-1) { printf(&apos;%d %d&apos;,x--,y--); } } 

Izlaz

 infinite loop 

Infinitivna while petlja u C-u

Ako izraz proslijeđen u while petlju rezultira bilo kojom vrijednošću koja nije nula, tada će se petlja pokrenuti beskonačan broj puta.

 while(1){ //statement }