Computer Science SLO Based Key Point Notes
************************************
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
- The block of statements following the keyword do is executed.
- 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.
- The loop is executed once again and then the condition is also checked again. This process continues till the condition becomes false.
- When the condition becomes false, control is transferred to the next statement.
- 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();
}
👇👇👇👇👇👇👇👇👇
👇👇👇👇👇👇👇👇👇
************************************
************************************
(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)
(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 # +923339719149 for Class-XII & XI Computer Science Complete SLO-Based Key Points notes in pdf for preparing/delivering the Lectures}
Note: Write me in the comments box below for any query and also Share this information with your class-fellows and friends.
0 Comments
Note: Write me in the comments box below for any queries and also Share this information with your class-fellows and friends.