Login


Lost your password?

Don't have an account ? Register ×
  
CPP All : Tricks in Loops – break continue scope
Concept Learning Code Sheets

Sum of sales using break in while loop #2071

Program to demonstrate an infinite while loop and the use of break in loop

#include<iostream>
using namespace std;
int main( )
{
float sale,sum=0.0;
while(true)
{
cout<<"Enter Sale value :";
cin>>sale;
sum+=sale;
if(sale==0.0) break;
}
cout<<"Sum of total sales is "<<sum;
return 0 ;
}

Sum of sales using break in for loop #2073

Program to demonstrate an infinite for loop and the use of break in loop

#include<iostream>
using namespace std;
int main( )
{
float sale,sum=0.0;
for(;true;)
{
cout<<"Enter Sale value :";
cin>>sale;
sum+=sale;
if(sale==0.0) break;
}
cout<<"Sum of total sales is "<<sum;
return 0 ;
}

Empty code block in a for loop #2078

Demonstration of empty code block in a for loop for using the loop as a delay loop.

#include<iostream.h>
using namespace std;
int main()
{
cout<<"Wait a few seconds!"
<<"Some message will soon be displayed."<<endl;
for(int i=1;i<999999999;++i);
cout<<"Delayed .. Hello!"<<endl;
return 0 ;
}

Odd number series using continue in the for loop #2085

Program to print the series of odd numbers using an alternative technique of using the continue within the for loop.

#include <iostream>
using namespace std;
int main( )
{
for(int i=1;i<=20;++i)
{
if(i%2==0) continue;
else cout<<i<<" ";
}
return 0 ;
}

Odd number series using continue in the while loop #2088

Program to print the series of odd numbers using an alternative technique of using the continue within the while loop.

#include<iostream>
using namespace std;
int main( )
{
int i=1;
while(i<=20)
{
if(i%2==0)
{
++i;
continue;
}
else cout<<i<<" ";
++i;
}
return 0 ;
}

Scope of variables declared inside loop #2092

Reuse  of variable declared in a loop scope for another loop.

#include<iostream>
using namespace std;
int main( )
{
  for(int i=1;i<=10;++i)
  {
    int x=5;
    cout<<i+x<<" ";
  }
  cout<<endl;
  for(int i=1;i<=10;++i)
  {
    int x=10;
    cout<<i+x<<" ";
  }
  return 0 ;
}
Solved Problems

Numbers divisible by 2 or by 3 #2094

Write the output of the following program.


Code Sheets:6  Solved Problems:1 
Back