When, Why and How to use While loop in C

Notes:

When, Why and How to use While loop in C Programming Language:

Program to compute the sum of digits in a given Integer in C:
Ex: 12, 123, 1234, 12345, etc.

12 = 1+2 =3
123 = 1+2+3 =6
1234 = 1+2+3+4 =10
12345 = 1+2+3+4+5 =15

Program to compute the sum of digits in a given Integer without using while loop:

int num = 123;
int lastDigit=0;
int sumOfDigits=0;

lastDigit = num % 10; // 3
sumOfDigits = sumOfDigits + lastDigit; // 3
num = num / 10; // 12

lastDigit = num % 10; // 2
sumOfDigits = sumOfDigits + lastDigit; // 5
num = num / 10; // 1

lastDigit = num % 10; // 1
sumOfDigits = sumOfDigits + lastDigit; // 6
num = num / 10; // 0
printf("Sum of all digits= %d\n",sumOfDigits);

Program to compute sum of digits in a given integer using while loop:

int num = 123;
int lastDigit=0;
int sumOfDigits=0;

while(num!=0)
{
lastDigit = num % 10;
sumOfDigits = sumOfDigits + lastDigit;
num = num / 10;
}

printf("Sum of all digits= %d\n",sumOfDigits);