C++ Tutorial/Operators statements/continue

Материал из C\C++ эксперт
Перейти к: навигация, поиск

break and continue statements

#include <iostream>
using namespace std;
int main()
{
    int count = 0;
    while (true)
    {
        count += 1;
        //end loop if count is greater than 10
        if (count > 10)
             break;
        //skip the number 5
        if (count == 5)
             continue;
        cout << count << endl;
    }
    return 0;
}

Set up 3 stop conditions for the loop: break and continue

#include <iostream>
 using namespace std;// this file uses std::cout,
                     // std::cin, std::endl, etc.
 
 int main()
 {
     unsigned short small = 1;
     unsigned long  large = 10;
     unsigned long  skip = 2;
     unsigned long target = 8;
     const unsigned short MAXSMALL=65535;
 
     
     while (small < large && large > 0 && small < 65535)
     {
         small++;
 
         if (small  skip == 0)// skip the decrement?
         {
             cout << "skipping on " << small << endl;
             continue;
         }
 
         if (large == target)  // exact match for the target?
         {
             cout << "Target reached!";
             break;
         }
 
         large-=2;
     }
 
     cout << "\nSmall: " << small << " Large: " << large << endl;
     return 0;
 }
skipping on 2
skipping on 4
Target reached!
Small: 5 Large: 8

Using the continue statement with do while loop

#include <iostream>
#include <iomanip>
#include <cctype>
#include <limits>
using std::cout;
using std::endl;
using std::setw;
int main() {
  cout << endl
       << setw(13) << "Character  "
       << setw(13) << "Hexadecimal "
       << setw(13) << "Decimal   "
       << endl;
  cout << std::uppercase;                      
  unsigned char ch = 0;                        
  do {
    if(!std::isprint(ch))                      
      continue;                                
    cout << setw(7)  << ch
         << std::hex                           
         << setw(13) << static_cast<int>(ch)
         << std::dec                           
         << setw(13) << static_cast<int>(ch)
         << endl;
  } while(ch++ < std::numeric_limits<unsigned char>::max());
  return 0;
}
Character   Hexadecimal    Decimal
                  20           32
      !           21           33
      "           22           34
      #           23           35
      $           24           36
      %           25           37
      &           26           38
      "           27           39
      (           28           40
      )           29           41
      *           2A           42
      +           2B           43
      ,           2C           44
      -           2D           45
      .           2E           46
      /           2F           47
      0           30           48
      1           31           49
      2           32           50
      3           33           51
      4           34           52
      5           35           53
      6           36           54
      7           37           55
      8           38           56
      9           39           57
      :           3A           58
      ;           3B           59
      <           3C           60
      =           3D           61
      >           3E           62
      ?           3F           63
      @           40           64
      A           41           65
      B           42           66
      C           43           67
      D           44           68
      E           45           69
      F           46           70
      G           47           71
      H           48           72
      I           49           73
      J           4A           74
      K           4B           75
      L           4C           76
      M           4D           77
      N           4E           78
      O           4F           79
      P           50           80
      Q           51           81
      R           52           82
      S           53           83
      T           54           84
      U           55           85
      V           56           86
      W           57           87
      X           58           88
      Y           59           89
      Z           5A           90
      [           5B           91
      \           5C           92
      ]           5D           93
      ^           5E           94
      _           5F           95
      "           60           96
      a           61           97
      b           62           98
      c           63           99
      d           64          100
      e           65          101
      f           66          102
      g           67          103
      h           68          104
      i           69          105
      j           6A          106
      k           6B          107
      l           6C          108
      m           6D          109
      n           6E          110
      o           6F          111
      p           70          112
      q           71          113
      r           72          114
      s           73          115
      t           74          116
      u           75          117
      v           76          118
      w           77          119
      x           78          120
      y           79          121
      z           7A          122
      {           7B          123
      |           7C          124
      }           7D          125
      ~           7E          126