Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

“Programing Fundamentals”

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

Recommended and Reference Books

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

do-while Repetition Structure


• A variation of the while loop.
• A do-while loop is always executed at least once
• Makes loop continuation test at the end not the beginning
• The body of the loop is first executed
• The condition (Boolean expression) is checked after the body
has been executed

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

do-while Repetition Structure


• Syntax / Format
do {
statement block
} while ( condition );
• Execute the statement.
• Evaluate the expression.
• If it is TRUE then proceed to step (1) else exit the 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

do-while Repetition Structure…


• Syntax
do action
while (condition)
• How it works:
• execute action action
• if condition is true then execute
action again
• repeat this process until condition
evaluates to false. true
condition
• action is either a single statement or
a group of statements within braces. false

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

3. #include <iostream> //preprocessor directive


4. using namespace std;
5. int main()
6. {
7. int counter = 1; //initializing counter to 1 Notice that control variable counter is
8. do pre-incremented in loop continuation test
9. { //do while loop begins
10.cout << counter << " "; //display counter
11.} while ( ++counter <= 10 );//loop continuation test // do while loop ends
12. output
13. cout << endl;
14. return 0;
15. }

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 and continue Statements


• The break and continue statements alter the flow of control

• 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

Break and continue Statements…


• Continue statement
• Used in while, for, do/while
• Skips remainder of loop body
• Proceeds with next iteration of loop
• while and do/while structure
• Loop-continuation test evaluated immediately after the continue
statement
• for structure
• Increment expression executed
• Next, loop-continuation test evaluated

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

Do-While Loop vs. While Loop

• POST-TEST loop (exit-condition) • PRE-TEST loop (entry-condition)


• The looping condition is tested • The looping condition is tested
after executing the loop body. before executing the loop body.
• Loop body is always executed at • Loop body may not be executed
least once. at all.

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

You might also like