Week 3 Notesc
Week 3 Notesc
Topics to be covered:
Break keyword
Continue keyword
Pattern Printing
Break Keyword
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.
It can be used to immediate termination of a loop and works in all the loops (for / while / do while
loop).
Syntax :
for(; ; ) {
if(condition) {
break;
}
}
Example:
Output:
0 1 2 3 4
NOTE : Break keyword is really useful in case of breaking infinite loops. Example :
int i = 0;
while(1) {
if(i == 5) break;
cout << i << " ";
i++;
}
Output:
0 1 2 3 4
Continue keyword
The continue keyword is used to end the current iteration in a for loop (or a while loop), and
continues to the next iteration.
Syntax :
for(; ;) {
if(condition) {
continue;
}
}
while(condition1) {
if(condition2) {
continue;
}
}
Example :
Output:
0 1 3 4 5
Output:
0 1 3 4 5
Pattern Printing
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 :
*****
*****
*****
*****
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4, m = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << "*";
}
cout << '\n';
}
}
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.
Rectangular Pattern (with spaces) :
Another example of rectangular pattern:
Pattern:
*****
* *
* *
*****
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4, m = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || j == 0 || j == m - 1 || i == n - 1) {
cout << "*";
} else cout << " ";
}
cout << '\n';
}
}
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.
11111
22222
33333
44444
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4, m = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << (i + 1);
}
cout << '\n';
}
}
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 .
11111
2 2
3 3
44444
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4, m = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
cout << (i + 1);
else cout << " ";
}
cout << '\n';
}
}
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.
Triangular Pattern:
Pattern :
*
**
***
****
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4, m = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
cout << "*";
}
cout << '\n';
}
}
Explanation:
Here we just loop the inner for loop to only till i so that it prints a triangular pattern.
1
22
333
4444
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4, m = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
cout << (i + 1);
}
cout << '\n';
}
}
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.