In this post, we will see C++ Control Structures Part 3 : Loop Structure | for, while, do while | while & do while Difference
Program Code: (loopstructure1.cpp)
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<5;i++)
{
cout<<"Square of "<<i<<"="<<i*i<<endl;
}
//infinite loop
/*
for(;;)
{
cout<<"Hello World"<<endl;
}
*/
}
Program Code: (loopstructure2.cpp)
#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<5)
{
cout<<"Square of "<<i<<"="<<i*i<<endl;
i++;
}
//infinite loop
/*
while(true)
{
cout<<"Square of "<<i<<"="<<i*i<<endl;
i++;
}
*/
}
Program Code: (loopstructure3.cpp)
#include<iostream>
using namespace std;
int main()
{
int i=0;
do
{
cout<<"Square of "<<i<<"="<<i*i<<endl;
i++;
} while (i<5);
}
Watch following video:
Watch on YouTube: https://www.youtube.com/watch?v=VsiXwVTcco0
No comments:
Post a Comment