Sample


[br]6 [br]<strong>A loop statement allows you to repeat a piece of code under a certain condition</strong>. There are 3 types of loop statements in C: <ul> <li><strong>while</strong></li> <li><strong>do-while</strong></li> <li><strong>for</strong></li> </ul> <code>while</code> repeats a piece of code <strong>as long as the given condition is true</strong>. [crayon-5d58ab2fd487d930075029/] For example the loop: [crayon-5d58ab2fd4886252948946/] divides <code>number</code> until it becomes odd. <code>do-while</code> is similar to <code>while</code>, but <strong>its code is executed at least once</strong>. [crayon-5d58ab2fd488a661335117/] This programs keeps reading a number from the terminal until it's odd. To learn more visit: <a href="https://itszephyro.com/blog/learn-c/c-while-loops/" rel="noopener" target="_blank">https://itszephyro.com/blog/learn-c/c-while-loops/</a> Finally <code>for</code> is a loop that is executed <strong>a certain amount of times</strong>. A for statements needs three expressions to work: an <strong>initialisation</strong>, a <strong>condition </strong>and an <strong>operation</strong>. <ul> <li>The <strong>initialisation</strong> is done only once, at the beginning of the loop.</li> <li>The <strong>condition</strong> is tested at the beginning of each cycle.</li> <li>The <strong>operation</strong> is executed at the end of each cycle.</li> </ul> Programmers usually use for to create a variable, do something until that variable has reached a certain value and increment it at the end of each cycle. The code below, for example, counts from 1 to 10: [crayon-5d58ab2fd488f741032846/] [br]https://itszephyro.com/blog/learn-c/c-for-loop/[br]#include <stdio.h> int main() { int var; do { printf("Enter an even number: "); scanf("%d", &var); }while(var%2==1); printf("The half of %d is %d", var, var/2); } [br]

[br]6
[br]A loop statement allows you to repeat a piece of code under a certain condition.

There are 3 types of loop statements in C:

  • while
  • do-while
  • for

while repeats a piece of code as long as the given condition is true.

For example the loop:

divides number until it becomes odd.

do-while is similar to while, but its code is executed at least once.

This programs keeps reading a number from the terminal until it’s odd.

To learn more visit: https://itszephyro.com/blog/learn-c/c-while-loops/

Finally for is a loop that is executed a certain amount of times.

A for statements needs three expressions to work: an initialisation, a condition and an operation.

  • The initialisation is done only once, at the beginning of the loop.
  • The condition is tested at the beginning of each cycle.
  • The operation is executed at the end of each cycle.

Programmers usually use for to create a variable, do something until that variable has reached a certain value and increment it at the end of each cycle.

The code below, for example, counts from 1 to 10:

[br]https://itszephyro.com/blog/learn-c/c-for-loop/[br]#include

int main() {
int var;
do {
printf(“Enter an even number: “);
scanf(“%d”, &var);
}while(var%2==1);
printf(“The half of %d is %d”, var, var/2);
}
[br]

To learn more, visit: