0% found this document useful (0 votes)
22 views17 pages

By Shahbaz Alvi Awan

The document discusses different loop constructs in C++ including for, while, do-while loops. It provides examples of how to initialize, check conditions, and increment/decrement values in a for loop. It also discusses how to create infinite loops using while and do-while loops. The document concludes by covering goto, break, and continue statements used to control program flow in loops.

Uploaded by

Ahmad Rahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views17 pages

By Shahbaz Alvi Awan

The document discusses different loop constructs in C++ including for, while, do-while loops. It provides examples of how to initialize, check conditions, and increment/decrement values in a for loop. It also discusses how to create infinite loops using while and do-while loops. The document concludes by covering goto, break, and continue statements used to control program flow in loops.

Uploaded by

Ahmad Rahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

By

Shahbaz Alvi Awan


 C++ For Loop
 The C++ for loop is used to iterate a part of the
program several times. If the number of
iteration is fixed, it is recommended to use for
loop than while or do-while loops.
 The C++ for loop is same as C/C#. We can
initialize variable, check condition and
increment/decrement value.
for(initialization; condition; incr/decr){
//code to be executed
}
#include <iostream>
using namespace std;
int main() {
for(int i=1;i<=10;i++){
cout<<i <<"\n";
}
}
 If we use double semicolon in for loop, it will be
executed infinite times. Let's see a simple
example of infinite for loop in C++.
#include <iostream>
using namespace std;

int main () {
for (; ;)
{
cout<<"Infinitive For Loop";
}
}
In C++, while loop is used to iterate a part of
the program several times. If the number of
iteration is not fixed, it is recommended to
use while loop than for loop.
Syntax:
while(condition){
//code to be executed
}
#include <iostream>
using namespace std;
int main() {
int i=1;
while(i<=10)
{
cout<<i <<"\n";
i++;
}
}
#include <iostream>
using namespace std;
int main () {
while(true)
{
cout<<"Infinitive While Loop";
}
}
 The C++ do-while loop is used to iterate a part of the
program several times.
 If the number of iteration is not fixed and you must have
to execute the loop at least once, it is recommended to
use do-while loop.
 The C++ do-while loop is executed at least once because
condition is checked after loop body.
Syntax:
do{
//code to be executed
}while(condition);
#include <iostream>
using namespace std;
int main() {
int i = 1;
do{
cout<<i<<"\n";
i++;
} while (i <= 10) ;
}
In C++, if you pass true in the do-while loop, it
will be infinitive do-while loop.
do{
//code to be executed
}while(true);
#include <iostream>
using namespace std;
int main() {
do{
cout<<"Infinitive do-while Loop";
} while(true);
}
 The C++ goto statement is also known as
jump statement.
 It is used to transfer control to the other
part of the program. It unconditionally jumps
to the specified label.
 It can be used to transfer control from
deeply nested loop or switch case label.
#include <iostream>
using namespace std;
int main()
{
ineligible:
cout<<"You are not eligible to vote!\n";
cout<<"Enter your age:\n";
int age;
cin>>age;
if (age < 18){
goto ineligible;
}
else
{
cout<<"You are eligible to vote!";
}
}
The C++ break is used to break loop or switch
statement. It breaks the current flow of the
program at the given condition. In case of
inner loop, it breaks only inner loop.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\n";
}
}
 The C++ continue statement is used to
continue loop.
 It continues the current flow of the program
and skips the remaining code at specified
condition.
 In case of inner loop, it continues only inner
loop.
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
cout<<i<<"\n";
}
}

You might also like