3. Difference Between Continue & Break:
Definition of Continue:
The 'continue' statement skips the current iteration of a loop and moves to the next iteration.
Definition of Break:
The 'break' statement terminates the loop or switch case immediately.
Example in C:
#include<stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue; // Skips iteration when i == 3
}
printf("%d ", i);
}
return 0;
}
Output: 1 2 4 5
#include<stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
break; // Terminates loop when i == 3
}
, printf("%d ", i);
}
return 0;
}
Output: 1 2
4. Difference Between While & Do-While:
Definition of While Loop:
The 'while' loop executes a block of code repeatedly as long as the condition is true. The condition is
checked before execution.
Definition of Do-While Loop:
The 'do-while' loop executes the block of code at least once and checks the condition after the
execution.
Example:
While Loop:
#include<stdio.h>
int main() {
int i = 1;
while(i <= 3) {
printf("%d ", i);
i++;
}
return 0;
}
Definition of Continue:
The 'continue' statement skips the current iteration of a loop and moves to the next iteration.
Definition of Break:
The 'break' statement terminates the loop or switch case immediately.
Example in C:
#include<stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue; // Skips iteration when i == 3
}
printf("%d ", i);
}
return 0;
}
Output: 1 2 4 5
#include<stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
break; // Terminates loop when i == 3
}
, printf("%d ", i);
}
return 0;
}
Output: 1 2
4. Difference Between While & Do-While:
Definition of While Loop:
The 'while' loop executes a block of code repeatedly as long as the condition is true. The condition is
checked before execution.
Definition of Do-While Loop:
The 'do-while' loop executes the block of code at least once and checks the condition after the
execution.
Example:
While Loop:
#include<stdio.h>
int main() {
int i = 1;
while(i <= 3) {
printf("%d ", i);
i++;
}
return 0;
}