You are on page 1of 45

Programming Fundamentals

(for beginners)
Repetition/Looping
Control Structure

2
What is loop
• In computer programming, a loop is a
sequence of instructions that is
continually repeated until a certain
condition is reached

3
Repetition Control Structure
• C++ provide following Repetition Control
Structure
– For loop
– while loop
– do-while loop
• In addition of that we also need to
study the concept of
• break statement
• continue statement
4
Loops
• Counter Loop
– Counter loop repeats a statement or set of
statements up to specific number of times
• Conditional loop
– Conditional loops are used to execute a
statement or set of statements based on a
condition
 Pre Conditional Loop
 Post Conditional loop

5
Loop Control variable
• Loop control variable is a variable that is
used to control iterations of loop

• Normally we use at least one variable in


each loop

6
Parts of loop
• Initialization Expression(s) initialize(s) the
loop variables in the beginning of the loop. It is
executed only once in a loop
• Test Expression decides whether the loop
will be executed (if test expression is true) or
not (if test expression is false).
• Update Expression(s) update(s) the values
of loop variables after every iteration of the
loop.
• The Body-of-the-Loop contains statements
to be executed repeatedly.
7
For loop
• A for loop is a repetition control
structure that allows repeat one or more
than one statements upto a specific
number of times.
• It is a pre-conditional/pretest loop i.e
condition is tested before the execution
of body of loop

8
Syntax of for loop
for loop has the following syntax:
The initialization The statement is
is executed once executed until the
before the loop condition becomes
begins false

for ( initialization ; condition ; update )


{
//body of for loop
}
The increment portion is executed
at
the end of each iteration

9
For loop

Initialization Condition
Increment /
Decrement

for(int i=0 ; i <=10 ; i++)


{
cout<<"Please enter a number"<<endl;
cin>>num;
sum=sum+num;
10 }
Flowchart of for loop

11
Sample program using for loop
Factorial of any
user defined
number

12
Sample program using for loop
Output of above
program

13
Another Sample program using for loop
Power of any
user defined
number

14
Another Sample program using for loop
Power of any
user defined
number

15
for loop(Sample Exercise for beginners)
1) Write a C++ program to find sum of all even
numbers between 1 to n.
2) Write a for statement to add all the multiples of 3
between 1 and 100.
3) Write a C++ program to find sum of all even and odd
numbers between 1 to n.
4) Write a C++ program to count number of digits in a
number.
5) Write a C++ program to enter a number and print its
reverse.
6) Write a C program to enter a number and find its
palindrome or not.
16
Break statement
• The break statement enables a program to
skip over part of the code.
• it is a jump instruction and can be used
inside the switch and loop statements.

17
Break statement(Example)

18
Break statement(Example)
Sample
program of
break stmt

19
Continue statement
 It is a jump statement.
It is used only inside the loop.

20
Continue statement(Sample Example)

21
Continue statement(Sample Example)
Sample
program of
Continue stmt

22
Nested for loop(syntax)
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
23
Nested for loop(Sample Example 1)
#include <iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 6; j++)
cout << "* ";
cout << endl;
}
system("pause");
return 0;
}
24
Nested for loop

Output

25
Nested for loop
(Sample Practice questions)
1) Write C++ code to print following
pattern on console using nested for
loop.

26
Solution
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 6; i++)
{
for (int j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
system("pause");
return 0;
}

27
Nested for loop
(Sample Practice questions)

2) Write C++ code to print following


pattern on console using nested for
loop.

28
Solution
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 6; i++)
{
for (int j = 1; j <= i; j++)
cout << j;
cout << endl;
}
system("pause");
return 0;
}

29
Nested for loop (Sample Practice questions)
3) Write C++ code to print following
pattern on console using nested for
loop.

30
Solution
#include<iostream>
using namespace std;
int main(){
unsigned short int rows;
cout << "Enter no of rows=";
cin >> rows;
for (int i =rows ; i >0; i--)
{
for (int j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
system("pause");
return 0;
31 }
Nested for loop
(Sample Practice questions)
4) Write C++ code to print following
pattern on console using nested for
loop.

32
Solution
#include<iostream>
using namespace std;
int main(){
unsigned short int rows;
cout << "Enter no of rows=";
cin >> rows;
for (int i =rows ; i >0; i--)
{
for (int j = 1; j <= i; j++)
cout << j;
cout << endl;
}
system("pause");
return 0;
33 }
Nested for loop
(Sample Practice questions)
5) Write C++ code to print following
pattern on console using nested for
loop.

34
Solution
#include<iostream>
using namespace std;
int main(){
for (int i = 1; i < 6; i++)
{
for (int j = 5; j > 0; j--)
if (i < j)
cout << " ";
else
cout << "*";
cout << endl;
}
system("pause");
return 0;
35 }
Nested for loop
(Sample Practice questions)
6) Write C++ code to print following
pattern on console using nested for
loop.

36
Solution
#include<iostream>
using namespace std;
int main(){
for (int i = 1; i < 6; i++)
{
for (int j = 5; j > 0; j--)
if (i < j)
cout << " ";
else
cout << j;
cout << endl;
}
system("pause");
return 0;
37 }
Nested for loop
(Sample Practice questions)
7) Write C++ code to print following
pattern on console using nested for
loop.

38
Solution
#include<iostream>
using namespace std;
int main(){
for (int i = 1; i < 6; i++)
{
for (int j = 5; j > 0; j--)
if (i < j)
cout << " ";
else
cout << “* ”;
cout << endl;
}
system("pause");
return 0;
39
}
Nested for loop
(Sample Practice questions)
8) Write C++ code to print following
pattern on console using nested for
loop.

40
Solution
#include <iostream>
using namespace std;

int main(){
int rows, count = 0, count1 = 0, k = 0;

cout << "Enter number of rows: ";


cin >> rows;

for (int i = 1; i <= rows; ++i)


{
for (int space = 1; space <= rows - i; ++space)
{
cout << " ";
++count;
}

while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i + k << " ";
++count;
41 }
Solution(Cont…)
else
{
++count1;
cout << i + k - 2 * count1 << " ";
}
++k;
}
count1 = count = k = 0;
cout << endl;
}
system("pause");
return 0;
}

42
Nested for loop
(Sample Practice questions)
9) Write C++ code to print following
pattern on console using nested for
loop.

43
Solution
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= 1; j--)
if (j > i)
cout << " ";
else
cout << "* ";
cout << endl;
}
for (int i = 1; i <5 ; i++)
{
for (int j = 1; j <= 5; j++)
if (j <= i)
cout << " ";
else
cout << "* ";
cout << endl;
}
system("pause");
return 0;
44 }
“You can’t manage knowledge –
nobody can. What you can do is to
manage the environment in which
knowledge can be created,
discovered, captured, shared,
distilled, validated, transferred,
adopted, adapted and applied.”

You might also like