Professional Documents
Culture Documents
"Programing Fundamentals": "Do While Repetition Structure"
"Programing Fundamentals": "Do While Repetition Structure"
CS-102
Lecture 8
“Do While Repetition structure”
By:
Mr. Sana Ullah Khan
Lecturer in Computer Science
Institute of Computing
KUST, Pakistan
Topic: Do While Repetition Structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Outline
• Do while Repetition structure
• Examples
• Nested Loop
• Summary
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 1
1. // simple program to print the numbers 1 to 10
2. // Using the do/while repetition structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 2
• Display Numbers from 1 to 5
// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main()
{
int i = 1;
// while loop from 1 to 5
while (i <= 5)
{
cout << i << " "; ++i;
}
return 0;
}
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
OUT PUT
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 3
• Sum of Positive Numbers Only
// program to find the sum of positive numbers
// if the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main()
{
int number;
int sum = 0;
// take input from the user
cout << "Enter a number: ";
cin >> number;
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 3…
while (number >= 0) {
// add all positive numbers
sum += number;
// take input again if the number is positive
cout << "Enter a number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
}
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
OUT PUT
Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2
The sum is 25
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
• break statement
• Causes immediate exit from while, for, do/while or switch structure
• Program execution continues with first statement after structure
• Common uses
• Escape early from a loop
• Skip the remainder of switch structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 4
// Using the break statement in a for structure.
// the for loop will terminate as soon as x becomes 5
#include <iostream>
using namespace std;
// function main begins program execution
int main() {
int x; // x declared here so it can be used both in and after the loop
// loop 10 times
for ( x = 1; x <= 10; x++ ) 16 {
// if x is 5, terminate loop Exits for structure when break is
if ( x == 5 ) executed.
break; // break loop only if x is 5
cout << x << " "; // display value of x
} // end for
cout << "\nBroke out of loop when x became " << x << endl; Output
return 0; // indicate successful termination
} // end function main
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 5
// Using the continue statement in a for structure.
#include <iostream>
using namespace std;
// function main begins program execution
int main()
{
// loop 10 times
Skips to next iteration of the loop.
for ( int x = 1; x <= 10; x++ ) {
// if x is 5, continue with next iteration of loop
if ( x == 5 )
continue; // skip remaining code in loop body
cout << x << " "; // display value of x
} // end for structure
cout << "\nUsed continue to skip printing the value 5"
<< endl;
return 0; // indicate successful termination
} // end function main
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Output
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Nested Loop
• Nested Loop is a loop in which one loop resides inside another loop
• where the inner loop gets executed first .
• Execution of statements within the loop flows in a way that the inner
loop of the nested loop gets declared, initialized and then
incremented.
• Once all the condition within the inner loop gets satisfied and
becomes true it moves for the search of the outer loop.
• It is often called a “loop within a loop”.
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Nested Loop…
• Syntax of Nested While loop
while(condition)
{
while(condition)
• {
// set of statement of inside while loop
}
//set of statement for outer while loop
}
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Nested Loop…
Flowchart
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 1
// C++ program to display a pattern
// with 5 rows and 3 columns
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int columns = 3;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= columns; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
OUTPUT
* * *
* * *
* * *
* * *
* * *
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Example 2
#include <iostream>
using namespace std;
int main() {
int i=1,j;
while (i <= 5)
{
j=1;
while (j <= i )
{
cout <<j;
j++;
}
cout << endl;
i++; }
return 0;
}
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
OUTPUT
1
12
123
1234
12345
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Tasks
• Write a program in which user input their choices on the menu (A,B,C,D or E), if
they select the yes option. If not, then it goes straight to calculation of the total
sales price.
• Write a C++ program to print the given star pattern.
*
**
***
****
*****
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Topic: Do While Repetition Structure
Summary
Course: Programing Fundamentals Course Code: CS-102 -- Instructor: Sana Ullah Khan, Lecturer. Institute of Computing, KUST -- Email: sana.ullah@kust.edu.pk
Thanks