Loop Break vs Continue
C
Medium
3 views
Problem Description
There are numbers in the Array. Loop in:
Break at negative number (stop completely)
Zero on continue (skip current iteration)
Demonstrate the effect of both by printing positive numbers.
Official Solution
#include <stdio.h>
int main() {
int arr[] = {5, 10, 0, 7, 0, 3, -2, 8, 9};
int i;
int size = sizeof(arr) / sizeof(arr[0]);
printf("Positive numbers until negative number:n");
for (i = 0; i < size; i++) {
if (arr[i] < 0) { // break at negative number
break;
}
if (arr[i] == 0) { // skip zero
continue;
}
// Only positive numbers will reach here
printf("%d ", arr[i]);
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!