Computer Science SLO Based Key Point Notes
👉👉👉 Computer Science 12th Class Notes (Main Page)
************************************
Unit 4: Control Structure
4.2.2. The While loop:
- While loop is used to execute one or more statements repeatedly as long as the given condition remains TRUE.
- It is useful when the number of repetitions or iterations is not known in advance.
- It is the simplest loop of C.
Condition: The condition is given as a relational expression that controls the repetition of a loop. If the condition is TRUE, the body of the loop is executed. If the condition is FALSE, the body of the loop is not executed.
Loop Body: It consists of the statements that are to be executed repeatedly as long as the given condition is TRUE.
Working of while Loop
“while loop” is executed as follows:- First, the condition is evaluated.
- If the condition is TURE, the body of the loop is executed. After executing the body, the control goes back to evaluate the condition.
- Step 1 and step 2 will be repeated until the condition becomes FALSE. When the condition is FALSE, the loop is terminated or exited.
Flow chart of “while loop”
Example 1:
Write a program that displays “Pakistan” five times using a while loop.#include <iosteam.h>
#include <conio.h>
void main()
{
clrscr();
int n = 1;
while (n<=5)
{
cout<<“\n Pakistan”;
n++;
}
getch();
}
#include <conio.h>
void main()
{
clrscr();
int n = 1;
while (n<=5)
{
cout<<“\n Pakistan”;
n++;
}
getch();
}
Example 2:
Write a program that prints the sum of all the positive numbers up to 15 using a while loop.#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int k=1, sum=0;
while(k<=15)
{
sum = sum + k;
k++;
}
cout<<“Sum = “<<sum<<endl;
getch();
}
#include <conio.h>
void main()
{
clrscr();
int k=1, sum=0;
while(k<=15)
{
sum = sum + k;
k++;
}
cout<<“Sum = “<<sum<<endl;
getch();
}
👇👇👇👇👇👇👇👇👇
👇👇👇👇👇👇👇👇👇
************************************👉👉👉 Computer Science 12th Class Notes (Main Page)
************************************
(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 Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation 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.