4.2.3. The do-while loop, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

4.2.3. The do-while loop, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

12th Class (HSSC-II) Computer SLO Based Key Point Notes

(National Book Foundation - As Federal Textbook Board, Islamabad 
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards) 

{Contact WhatsApp 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}

************************************

************************************

Unit 4: Control Structures


4.2.3. The do-while loop

  • The do-while loop is used to execute one or more statements repeatedly as long as the given condition is TRUE.
  • It is similar to a “while loop” but in the do-while loop, the body of the loop comes before the condition.
  • In a do-while loop, the body of the loop executes at least once even if the given condition is FALSE.

Syntax:

do
{
        statement 1;
        …………
        …………
}
while ( condition );


Working of do-while Loop

  1. The block of statements following the keyword do is executed.
  2. The condition at the end of the loop is evaluated. If the condition is true, control is transferred back to the beginning of the loop.
  3. The loop is executed once again and then the condition is also checked again. This process continues till the condition becomes false.
  4. When the condition becomes false, control is transferred to the next statement.
  5. If the body of the loop consists of a single statement then braces are not required.

Flow chart of “do-while” loop



Example # 1:

Write a program that displays the first five numbers with their cubes.

#include <iostream.h>
#include <conio.h>
void main()
{
        clrscr();
        int c = 1;
        do
        {
                cout<<“\n”<<c*c*c;
                c++;
        }
        while ( c <= 5);
        getch();
}


Example # 2:

Write a program that reads the current state of a telephone line. The user should enter ‘w’ for working and ‘d’ for dead. Any other input will be invalid.

#include <iostream.h>
#include <conio.h>
void main()
{
        clrscr();
        char ch;
        do
        {
                cout<<“ \n Enter the state of phone: w for working and d for dead: “;
                ch = getche();
        }
        while ( ch != ‘w’ && ch != ‘d’);
        getch();
}

************************************

************************************


************************************
Shortcut Links For:

1.  5th Class All Subjects Notes

2.  8th Class All Subjects Notes

3.  Easy English Grammar Notes



1. Website for School and College Level Physics   
2. Website for School and College Level Mathematics  
3. Website for Single National Curriculum Pakistan - All Subjects Notes 

© 2023 & onwards Academic Skills and Knowledge (ASK  

Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.

Post a Comment

0 Comments

cwebp -q 80 image.png -o image.webp