Control Flow
Break, Continue & Pattern Programs
Control loops with break, continue and goto, then master nested loops through the star and number patterns every exam asks for.
break
break ends the nearest loop (or switch) immediately:
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 10; i++) {
if (i == 6) break;
printf("%d ", i);
}
printf("\nLoop ended early.\n");
return 0;
}1 2 3 4 5 Loop ended early.
continue
continue skips the rest of the current pass and jumps to the next one:
#include <stdio.h>
int main(void) {
printf("Odd numbers below 12: ");
for (int i = 1; i < 12; i++) {
if (i % 2 == 0) continue;
printf("%d ", i);
}
printf("\n");
return 0;
}Odd numbers below 12: 1 3 5 7 9 11
Nested loops
A loop inside another loop runs its inner body rows × columns times. The outer loop usually picks the row; the inner loop prints the columns of that row. That is the whole secret of pattern programs.
#include <stdio.h>
int main(void) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 4; col++)
printf("(%d,%d) ", row, col);
printf("\n");
}
return 0;
}(1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4)
Pattern 1: right triangle
Row i has i stars.
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}* * * * * * * * * * * * * * *
Pattern 2: number triangle and Floyd's triangle
#include <stdio.h>
int main(void) {
int n = 4, k = 1;
for (int i = 1; i <= n; i++) { // 1 / 1 2 / 1 2 3 ...
for (int j = 1; j <= i; j++) printf("%d ", j);
printf("\n");
}
printf("\n");
for (int i = 1; i <= n; i++) { // Floyd: keep counting across rows
for (int j = 1; j <= i; j++) printf("%-3d", k++);
printf("\n");
}
return 0;
}1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 6 7 8 9 10
Pattern 3: pyramid
Each row needs spaces first, then stars. For n rows, row i has n - i spaces and 2i - 1 stars.
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) printf(" ");
for (int j = 1; j <= 2 * i - 1; j++) printf("*");
printf("\n");
}
return 0;
}* *** ***** ******* *********
Pattern 4: diamond
A diamond is a pyramid followed by an upside-down pyramid:
#include <stdio.h>
int main(void) {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int s = 0; s < n - i; s++) printf(" ");
for (int j = 0; j < 2 * i - 1; j++) printf("*");
printf("\n");
}
for (int i = n - 1; i >= 1; i--) {
for (int s = 0; s < n - i; s++) printf(" ");
for (int j = 0; j < 2 * i - 1; j++) printf("*");
printf("\n");
}
return 0;
}* *** ***** ******* ***** *** *
Pattern 5: Pascal's triangle
Each number is the sum of the two above it. Using the formula C(i, j+1) = C(i, j) × (i − j) / (j + 1) we can print each row without storing earlier rows:
#include <stdio.h>
int main(void) {
int n = 6;
for (int i = 0; i < n; i++) {
for (int s = 0; s < n - i - 1; s++) printf(" ");
int c = 1;
for (int j = 0; j <= i; j++) {
printf("%4d", c);
c = c * (i - j) / (j + 1);
}
printf("\n");
}
return 0;
} 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1Test yourself
4 questions · pick an answer to check it instantly
- 1
What does this print?
Cfor (int i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d", i); }continueskips only the pass wherei == 3. - 2
How many stars does this print in total?
Cfor (int i = 1; i <= 4; i++) for (int j = 1; j <= i; j++) printf("*");Rowiprintsistars: 1 + 2 + 3 + 4 = 10. - 3
In a 5-row centred pyramid, how many stars are on row 3?
Rowihas2i − 1stars, so row 3 has 5. - 4
Which statement about
breakis true?breakleaves only the closest enclosing loop orswitch.continueskips to the next iteration.