Programming Fundametals in C+ Lec 6

You might also like

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

Programming Fundamentals

Lab
Lecture 6

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Break and Continue

 break is used to break or terminate a loop whenever we want.

 Continue passes control to the nearest condition in the loop.

 Continue statement works similar to break statement. The only difference is


that break statement terminates the loop whereas continue statement passes control to
the conditional test i.e., where the condition is checked.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Break
int main()
{
int n = 1;
while(n < 10)
{
if (n == 5)
{
n = n + 1;
break;
}
cout << "n = " << n << endl;
n++;
}
return 0;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
}
Continue
int main()
{
int n = 1;
while(n < 10)
{
if (n == 5)
{
n = n + 1;
continue;
}
cout << "n = " << n << endl;
n++;
}
return 0;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
}
Nested Loop

 Nested loop means a loop statement inside another loop statement.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Nested While

 Write a nested while loop program and display the loop execution cycle number.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


int main ()
{
int i = 0;
while( i < 3)
{
int j = 0;
while(j < 5)
{
cout << "i = " << i << " and j = " << j << endl;
j++;
}
i++;
}
return 0;
} Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
Nested do-while

 Write a program to print the output shown below using do-while loop.

11
12
13
21
22
23
31
32
33

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Nested do-while
int main() 
{  
     int i = 1;    
         do
{    
               int j = 1;          
               do
{    
                 cout<<i<<"\n";        
                   j++;    
               } 
while (j <= 3) ;    
               i++;    
          }
  while (i <= 3) ;     
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
}  
Task

Write a program to print the following pattern using While loop.

*
* *
* * *
* * * *

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar

You might also like