C++ tutorial-C++ loops


C++ loops

Put simply, loop enables your program to execute the block of code repeatedly. In C++, there are for, while, and do while loops that are discussed below.

For loop


for(start value;condition;increment){
 statement1
 statement2
}

The code in for loop will be executed from the start value until the condition is met. The increment will be added continuously to the start value until the condition is met. Example:
   
 int i;
      for (i = 1; i <=10; i++)
         count<<"\C++ programming";//The words C++ programming will be printed 10 times
   

While Loop


The while loop executes code repeatedly if the condition is still true.


while(condition){
  statement
  statement
---------

}

Example:
int i = 1;
while (i <= 10)
{
   cout << "\nC++";
   i = i + 1;
}


Do While Loop


The do while loop tests the condition at the bottom of the loop.
int i = 1;
do
{
   cout << "\nC++ do while loop";
   i = i + 1;
}
while (i <= 10);
 

Break


The break command can be used to exit a loop at any time.
Example:
int i;
for (i = 1; i <= 10; i++)
{
   cout << "\nI like C++";
if(i==5) break;
}

In the above example, the program will print “I like C++” 5 times and then the loop will exit.

Continue


The continue command lets the program start the next iteration of the loop.
Example:
int i=1;
while(i <= 5)
{
   continue;
   cout << "\nI like C++";
i=i+1;

}





Comments

deena.dynamite comment

 deena.dynamite

Question 2:

#include <iostream>

using namespace std;

int int_values[10];
float float_values[10];
double double_values[10];

void bubble(int arr[]) {
int temp;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
if (arr[i - 1] > arr[i]) {
temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
for (int k = 0; k < 10; k++) {
cout << arr[k] << endl;
}
}

void bubble(float arr[]) {
float temp;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
if (arr[i - 1] > arr[i]) {
temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
for (int k = 0; k < 10; k++) {
cout << arr[k] << endl;
}
}

void bubble(double arr[]) {
double temp;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
if (arr[i - 1] > arr[i]) {
temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
for (int k = 0; k < 10; k++) {
cout << arr[k] << endl;
}
}

int main() {

cout << "Please pass in 10 integers!\n";
for (int i = 0; i < 10; i++) {
cin >> int_values[i];
}
bubble(int_values);
cout << endl;

cout << "Why don't we try this with 10 floats?!\n";
for (int i = 0; i < 10; i++) {
cin >> float_values[i];
}
bubble(float_values);
cout << endl;

cout << "And now, do the doubles!\n";
for (int i = 0; i < 10; i++) {
cin >> double_values[i];
}
bubble(double_values);
cout << endl;

return 0;
}


2017-08-18
Troy comment

 Troy

for more C++, C exercises,
please visit http://ccphplusplus.blogspot.com/


2015-05-26
Dara comment

 Dara

The following program uses a while loop to allow the user to input ten odd numbers and stores them in an array. The maximum value of those numbers is calculated by calling the getMax function.

#include<iostream>
using namespace std;
int getMax(int arr[],int n){
int max=arr[0];
for(int i=1;i<n;i++)
if(max<arr[i]) max=arr[i];
return max;
}
int main()
{

int arr[10];
int i=0;
int v;
while(i<10){
cout<<"value["<<i<<"]:";
cin>>v;
if(v%2!=0){
arr[i]=v;
i++;
}
else{
cout<<"Only odd number is accepted.\n";
}
}

int max=getMax(arr,10);
cout<<"Max="<<max<<endl;
system("PAUSE");
return 0;
}


2013-12-22
Kadeo comment

 Kadeo

Please help me to write a program by using loops to allow the user to input ten odd numbers. Then find the max value of those numbers. You need to create a function to get the max value. Thanks.


2013-12-21
Dara comment

 Dara

This code will be useful to you:
#include <cstdlib>
#include <iostream>
#include <vector>
#include<string.h>

using namespace std;

int main(int argc, char *argv[])
{
int n;
cout<<"Enter n:";
cin>>n;
for(int i=0;i<n;i++)
{
if(i!=n-1){
for(int j=i+1;j<=(n+i);j++)
{

if(j<=n)
cout<<j;

else
cout<<j-n;

}

cout<<"\n";
}
else{
for(int j=n;j>0;j--)
cout<<j;
}
cout<<"\n";


}
system("PAUSE");
return EXIT_SUCCESS;
}


2013-11-29
kalmark comment

 kalmark

Please help me to write a C++ program to allows the user to input in values then the program will print a pattern. For example, if 5 is input, the output pattern is:
12345
23451
34512
45123
54321


2013-11-29



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.