This is an archive
The University of
Q: Xcode: Build F
Mixed results from
How to make a simp
Hemoglobin content
Growth and structu
The goal of this r
Amid all the talk
Sickle cell diseas

In the past year,
"And let's hear th
The present invent
Orofacial and soma
What if our world
/* Bullet Continu
/* * Copyright (
A new method of es
Dallas County Cler
Q: Are there know
Q: Why does one need to use if/else statement for a do-while loop? I am studying "Do while loop in C", and I don't understand the need of a if/else statement in below code snippet. Why is if (num == 0) at the end needed for the do-while statement? int num = 1, m; do { printf("Enter a number (0-9): "); scanf("%d", &num); m = num % 10; } while (m != 0); A: because you're not getting any input when it is equal to zero. if it was equal to any non-zero number then if (num == 0) will not execute. A: There is no magic involved. It's like this: while (m != 0) { printf("Enter a number (0-9): "); scanf("%d", &num); m = num % 10; } if(num == 0) { printf("Input is 0"); } So if it enters the body of the while loop, then it keeps going round and round. However, if it doesn't enter the body of the while loop, then the do while loop stops executing. However, if it enters the loop, then you check the condition m!=0, which works like the do while loop condition you gave in your question. If it's not 0, then go round again. A: This answer to a question by the OP has more details. It's very useful when doing math calculations like this. For example, the following code will calculate the sum of 1, 2 and 3. int main() { int count = 0; printf("Enter a number"); scanf("%d", &count); while(count!=0) { printf("%d + 1 = ", count); printf("%d + 2 = ", count+1); printf("%d + 3 = ", count+2); count = count + 3; printf("The value is: "); scanf("%d", &count); } return 0; } Thanks to this, it's easy to correct any mistake. Let's change the 4 and 5 inputs to: 1 + 6 = -3 1 + 4 = 5 1 + 5 = 6 The value is: 4 1 + 6 = -3 1 + 4 = 5 1 + 5 = 6 The value is: 6 1 + 6 = -3 1 + 4 = 5 1 + 5 = 6 The value is: 7 Because we have if() statements, it can be easily corrected. The above code won't work as expected if scanf() does not allow spaces to be inputted, for example: printf("Enter a number"); scanf("%d", &num); This will result in a segmentation fault as num is not initialized. This works: printf("Enter a number"); scanf("%d", &num); num = (num + '0'); But this will fail if num is more than 9. In such cases, we have to test num >= 0 first: int num = 1, m; do { printf("Enter a number (0-9): "); scanf("%d", &num); m = num % 10; } while (m != 0 && num >= 0); This can be more properly written with the && operator instead of the &&&. For completeness, here's an alternative: int num = 1, m; do { printf("Enter a number (0-9): "); scanf("%d", &num); m = num % 10; } while (m != 0); /* check the number */ if(num == 0) printf("num == 0"); if (m != 0) printf("m != 0"); if (num == 0 && m != 0) printf("Both num == 0 and m != 0"); if (num == 0) printf("num == 0"); if (m != 0) printf("m != 0"); if (num == 0 && m != 0) printf("Both num == 0 and m != 0"); if (num != 0) printf("num != 0"); if (m != 0) printf("m != 0");