0% found this document useful (0 votes)
5 views21 pages

Introloops Forloop

The document provides an introduction to programming structures, including sequential, selection, and iterative structures. It explains how to implement these structures in C++ with examples, focusing on loops such as for, while, and do-while loops, as well as counter-controlled and sentinel-controlled loops. Additionally, it includes practice problems for further understanding of the concepts presented.

Uploaded by

sultanameer008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views21 pages

Introloops Forloop

The document provides an introduction to programming structures, including sequential, selection, and iterative structures. It explains how to implement these structures in C++ with examples, focusing on loops such as for, while, and do-while loops, as well as counter-controlled and sentinel-controlled loops. Additionally, it includes practice problems for further understanding of the concepts presented.

Uploaded by

sultanameer008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to

Programming (CS-102)
Maham Shuja
[email protected]
Programming Structures

 Sequential Structure

 Selection/Conditional Structure

 Iterative / Repetitive Structure


Sequential Structure
 All statements are executed once.
 Problem: Take a number from user and display it.
#include<iostream>
using namespace std;
int main ()
{
int num;
cout<<"Enter a number: ";
cin>>num;
cout<<"You have entered "<<num;
return 0;
}
Selection/Conditional Structure
 Execute or skip statements on the basis of condition.
 Problem: Display the number if the entered number is greater than 5.
#include<iostream>
using namespace std;
int main ()
{
int num;
cout<<"Enter a number: ";
cin>>num;
if(num>5)
cout<<num<<" is greater then 5";
return 0;
}
Suppose you have to repeat the
previous problem for 100 times

Iterative / Repetitive Structure


Iterative / Repetitive Structure

 A type of control structure that repeats a statement or set of statements is


known as looping/ iterative/ repetitive structure.

 The statement or set of statements are executed repeatedly until some


condition is satisfied.

 Examples:
 User may display his/ her name for 10 times
 Display set of natural numbers from 1 to 10
Counter-Controlled & Sentinel- Controlled Loops

 Counter-Controlled Loops
 We can determine before loop execution exactly how many loop repetitions will
be needed to solve the problem.
 Example: Set of first five natural numbers

 Sentinel- Controlled Loops


 Depends on a special value known as sentinel value.
 Loop terminate when sentinel value is encountered.
 Iterations depends on the user’s input.
 Example: Loop terminates when user enters -1
Types of loops

 for loop
 while loop
 do - while loop
for loop

 for loop executes one or more statements for a specified number of times.
This loop also called counter- controlled loop.

Syntax:

for (initialization; condition; increment/decrement)


{
Statement 1;
Statement 2;
.
.
Statement N;
}
Parts of for loop

 Initialization
 It specifies the starting value of counter variable. One or many variables
can be initialized in this part. To initialize many variables, each variable is
separated by comma.
 Condition
 The condition is given as relational expression. The statement is executed
only if condition is true.
 Increment/Decrement
 This part specifies the change in counter variable after each execution of
the loop.
 Statements:
 Instructions that are executed when the condition is true.
Example
Flow Chart

Initialization

Increment/decrement

True
Condition Loop body

False
Example

count count <= 5 Print


1 1 <= 5 Hello
2 2 <= 5 Hello
3 3 <= 5 Hello
4 4 <= 5 Hello
5 5 <= 5 Hello
6 6 <= 5 ----
Example
x y x <= 4 Print
#include<iostream>
0 4 0 <= 4 0 4
using namespace std;
int main() 1 3 1 <= 4 1 3
{
2 2 2 <= 4 2 2
int x, y;
for(x=0, y=4; x<=4; ++x, --y) 3 1 3 <= 4 3 1

cout << x << “ “ << y << '\n'; 4 0 4 <= 4 4 0


return 0;
5 -1 5 <= 4 -----
}
Output
Example

Write a C++ program that display following output using for loop:
6 4 2 0 -2 -4 -6

i i >= -6 Print
#include<iostream> 6 6 >= -6 6
using namespace std; 4 4 >= -6 4
int main() 2 2 >= -6 2
{ 0 0 >= -6 0
for(int i=6; i >= -6; i = i-2) -2 -2 >= -6 -2
cout << i << ' '; -4 -4 >= -6 -4
return 0; -6 -6 >= -6 -6
-8 -8 >= -6 ----
}
Example

#include<iostream>
using namespace std;
int main()
{
123 is sentinel value
int x;
for(x=0; x != 123; )
{
cout << "Enter a number: "; x x != 123 Print
cin >> x; 0 0 != 123 Enter a number: 12
} 12 12 != 123 Enter a number: 8
cout<<"Exit"; 8 8 != 123 Enter a number: 123
return 0; 123 123 != 123 Exit
}
Output
Example
Write a program to print first ten even numbers using for loop.

#include <iostream>
Using namespace std;
int main()
{
int count;
for (count = 2; count <= 20; count = count +2)
cout<<count<< “ “ ;
return 0;
}

Write the output of above program …………


Practice Problems

 Write a program to calculate sum and average of ten numbers. The


program should take numbers from user as an input.
 Write a program to display table of 5 using for loop.
 Write a program to calculate factorial of a number.
Reference

Gary J. Bronson , Program Development and Design using C++, 2nd Edition.
(Chapter 5)

You might also like