C++ Tutorial/Operators statements/while — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:30, 25 мая 2010

A while true loop

#include <iostream>
 
 int main()
 {
     int counter = 0;
 
     while (1)
     {
         counter ++;
         if (counter > 10)
             break;
     }
     std::cout << "counter: " << counter << "\n";
     return 0;
 }
counter: 11

Calculate the sum of the integers from 1 to 10 using while loop

#include <iostream>
using std::cout;
using std::endl;
int main()
{
   int sum; 
   int x; 
   x = 1; 
   sum = 0; 
   while ( x <= 10 )
   {
      sum += x;
      x++;
   }
   cout << "The sum is: " << sum << endl;
   return 0;
}
The sum is: 55

Counter-controlled repetition

#include <iostream>
using std::cout;
using std::endl;
int main()
{
   int counter = 1; 
   while ( counter <= 10 ) 
   {    
      cout << counter << " ";
      counter++; 
   } 
   cout << endl; 
   return 0; 
}
1 2 3 4 5 6 7 8 9 10

demonstrates WHILE loops using fibonacci series

#include <iostream>   
using namespace std;   
  
int main(){                           
   const unsigned long limit = 4294967295;   
   unsigned long next=0;       
   unsigned long last=1;       
  
   while( next < limit / 2 )   
   {   
      cout << last << "  ";    
      long sum = next + last;  
      next = last;             
      last = sum;              
   }   
   cout << endl;   
   return 0;   
}

Display all printable characters including the extended character set

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  unsigned char ch; 
 
  ch = 32; 
  while(ch) { 
    cout << ch; 
    ch++; 
  } 
 
  return 0; 
}
!"#$%&"()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno
pqrstuvwxyz{|}.....

Looping with while

#include <iostream>
 
 int main()
 {
     int counter = 0;
 
     while(counter < 5)
     {
         counter++;
         std::cout << "Looping!  ";
     }
 
     std::cout << "\nCounter: " << counter << ".\n";
     return 0;
 }
Looping!  Looping!  Looping!  Looping!  Looping!
Counter: 5.

Skip the body of the while loop when the condition is false

#include <iostream>
 
 int main()
 {
     int counter = 3;
     while (counter > 0)
     {
         std::cout << "Hello!\n";
         counter--;
     }
     std::cout << "counter is OutPut: " << counter;
     return 0;
 }
Hello!
Hello!
Hello!
counter is OutPut: 0

Use three conditions in while statement

#include <iostream>
 
 int main()
 {
     unsigned short small = 1;
     unsigned long  large = 123;
     const unsigned short MAXSMALL=65535;
 
     std::cout << "small: " << small << "...";
 
      // for each iteration, test three conditions
     while (small < large && large > 0 && small < MAXSMALL)
     {
         std::cout << ".";
         small++;
         large-=2;
     }
     std::cout << "\nSmall: " << small << " Large: "  << large << std::endl;
     return 0;
 }
small: 1............................................
Small: 42 Large: 41

While statement with "and"&&

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int len; 
 
  cout << "Enter length (1 to 79): "; 
  cin >> len; 
 
  while(len>0 && len<80)  { 
    cout << "."; 
    len--; 
  } 
 
  return 0; 
}