0% found this document useful (0 votes)
2 views

C++ Course Notes _ Fundamentals of Programming - 2 _ Week 3

The C++ course notes for Week 3 cover key programming concepts including the 'break' and 'continue' keywords, which control loop execution, and various pattern printing techniques. Examples illustrate how to use these keywords in loops and demonstrate creating rectangular and triangular patterns using both stars and numbers. The notes provide code snippets and explanations for each pattern type, enhancing understanding of loop structures and output formatting.

Uploaded by

prakashgyan1067
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Course Notes _ Fundamentals of Programming - 2 _ Week 3

The C++ course notes for Week 3 cover key programming concepts including the 'break' and 'continue' keywords, which control loop execution, and various pattern printing techniques. Examples illustrate how to use these keywords in loops and demonstrate creating rectangular and triangular patterns using both stars and numbers. The notes provide code snippets and explanations for each pattern type, enhancing understanding of loop structures and output formatting.

Uploaded by

prakashgyan1067
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3

C++ Course Notes | Fundamentals of Programming - 2 | Week 3

C++ Course Notes | Fundamentals of Programming


- 2 | Week 3
Rahil Sehgal
Created 6 months ago • Updated 6 months ago

Hello everyone welcome to the weekly lecture notes


Topics to be covered:
1. Break keyword
2. Continue keyword
3. Pattern Printing

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

5. NOTE : Break keyword is really useful in case of breaking infinite


loops. Example :
1
int i = 0;
2
while(1) {
3
if(i == 5) break;
4
cout << i << " ";
5
i++;
6
}

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
}

3. Syntax (another in case of while loop) :


1
while(condition1) {
2
if(condition2) {
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

5. Another example (in case of while loop) :


1
int i = 0;
2
while(i < 5){
3
if(i == 2) continue;
4
cout << i << " " ;
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.

2. Rectangular Pattern (with spaces) :


Another example of rectangular pattern:
https://content.pwskills.com/Cfi4Yf7pgt7qHq 4/9
10/4/23, 9:49 AM C++ Course Notes | Fundamentals of Programming - 2 | Week 3

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.

3. Rectangular Pattern (using Numbers) :


Pattern :

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 .

4. Rectangular Pattern (with Numbers and Spaces both)


Pattern:
1
11111
2
2 2
3
3 3
4
44444
5

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.

6. Triangular Pattern (with numbers):


Pattern :
1
1
2
22
3
333
4
4444
5

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

You might also like