Infinite Loop with Exit
C
Medium
1 views
Problem Description
Create infinite loop by using while(1). Check other conditions and exit with break on specific condition. Example of multiple exit points.
Official Solution
#include <stdio.h>
int main() {
int num;
while (1) { // infinite loop
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) { // Exit condition 1
printf("Negative number entered. Exiting loop.n");
break;
}
if (num == 0) { // Exit condition 2
printf("Zero entered. Exiting loop.n");
break;
}
if (num > 100) { // Exit condition 3
printf("Number greater than 100. Exiting loop.n");
break;
}
// Normal execution
printf("You entered: %dn", num);
}
printf("Program ended.n");
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!