C++ Course Notes _ Fundamentals of Programming - 2 _ Week 3
C++ Course Notes _ Fundamentals of Programming - 2 _ Week 3
Break Keyword
1. Break is a command that allows you to terminate and exit a loop ( do
while / for / while ) or switch commands from any point other than
the logical end.
2. It can be used to immediate termination of a loop and works in all the
loops (for / while / do while loop).
3. Syntax :
https://content.pwskills.com/Cfi4Yf7pgt7qHq 1/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
1
for(; ; ) {
2
if(condition) {
3
break;
4
}
5
}
4. Example:
1
for(int i = 0; i < 10; i++) {
2
if(i == 5) {
3
break;
4
}
5
cout << i << " " ;
6
}
Output:
0 1 2 3 4
Output:
0 1 2 3 4
Continue keyword
1. The continue keyword is used to end the current iteration in a for
loop (or a while loop), and continues to the next iteration.
https://content.pwskills.com/Cfi4Yf7pgt7qHq 2/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
2. Syntax :
1
for(; ;) {
2
if(condition) {
3
continue;
4
}
5
}
4. Example :
1
for(int i = 0; i < 5; i++) {
2
if(i == 2) continue;
3
cout << i << " ";
4
}
Output:
0 1 3 4 5
Output:
0 1 3 4 5
https://content.pwskills.com/Cfi4Yf7pgt7qHq 3/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
Pattern Printing
1. Simple Rectangular Pattern : In this type of patterns try to find the
length and breadth of the rectangle and then form the loop bounds
according to that only.
Example:
Pattern :
1
*****
2
*****
3
*****
4
*****
Code:
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int n = 4, m = 5;
6
for (int i = 0; i < n; i++) {
7
for (int j = 0; j < m; j++) {
8
cout << "*";
9
}
10
cout << '\n';
11
}
12
}
13
Explanation :
Here note the bounds of n and m and print accordingly like in this
case n is equal to 4 and m is equal to 5. Don't forget to print the new
line when needed.
Pattern:
1
*****
2
* *
3
* *
4
*****
5
Code:
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int n = 4, m = 5;
6
for (int i = 0; i < n; i++) {
7
for (int j = 0; j < m; j++) {
8
if (i == 0 || j == 0 || j == m - 1 || i == n - 1
9
cout << "*";
10
} else cout << " ";
11
}
12
cout << '\n';
13
}
14
}
15
Explanation :
Here see that we even have spaces with the star so in the boundary
we print the star and in between we print the spaces. To do this we
need to use if condition statement here.
https://content.pwskills.com/Cfi4Yf7pgt7qHq 5/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
1
11111
2
22222
3
33333
4
44444
5
Code:
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int n = 4, m = 5;
6
for (int i = 0; i < n; i++) {
7
for (int j = 0; j < m; j++) {
8
cout << (i + 1);
9
}
10
cout << '\n';
11
}
12
}
13
Explanation:
Here in this case rather than printing the star we print the value of
rows and for that we need to just print the value of i .
Code:
https://content.pwskills.com/Cfi4Yf7pgt7qHq 6/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int n = 4, m = 5;
6
for (int i = 0; i < n; i++) {
7
for (int j = 0; j < m; j++) {
8
if (i == 0 || i == n - 1 || j == 0 || j == m - 1
9
cout << (i + 1);
10
else cout << " ";
11
}
12
cout << '\n';
13
}
14
}
15
Explanation:
We change the if condition here to the boundary values and only
print the row numbers in case of boundary and otherwise we just
print spaces.
5. Triangular Pattern:
Pattern :
1
*
2
**
3
***
4
****
5
Code:
https://content.pwskills.com/Cfi4Yf7pgt7qHq 7/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int n = 4, m = 5;
6
for (int i = 0; i < n; i++) {
7
for (int j = 0; j < i + 1; j++) {
8
cout << "*";
9
}
10
cout << '\n';
11
}
12
}
13
Explanation:
Here we just loop the inner for loop to only till i so that it prints a
triangular pattern.
Code:
https://content.pwskills.com/Cfi4Yf7pgt7qHq 8/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int n = 4, m = 5;
6
for (int i = 0; i < n; i++) {
7
for (int j = 0; j < i + 1; j++) {
8
cout << (i + 1);
9
}
10
cout << '\n';
11
}
12
}
13
Explanation:
Here we just loop the inner for loop to only till i so that it prints a
triangular pattern and print value of the row instead of print stars.
https://content.pwskills.com/Cfi4Yf7pgt7qHq 9/9