C++ Break and Continue Statements

Поделиться
HTML-код
  • Опубликовано: 18 фев 2024
  • Break and Continue C++. In this video you will learn how to use the break and continue statements to control the flow of your loops. You will learn how to use the break statement when searching for a value in a vector. This is known as early exit. In addition, you will learn to use the continue statement to skip an iteration. Finally, you will be given an example where both continue and break statements are utilized in a for loop.
    If you need a refresher on getting user input in C++: • C++ Ternary Operator
    C++ Playlist:
    • C++ Programming Tutorial
    Install C++ with VS Code:
    • How to set up C++ in V...
    Subscribe for more coding tutorials 😄!

Комментарии • 2

  • @junium01
    @junium01 2 месяца назад

    At 5:42, what difference is there between making a new string and int for the values at each index in the vectors versus just referencing them as "colorOrder[i]" and "quantityOrder[i]"?

    • @KennyYipCoding
      @KennyYipCoding  2 месяца назад

      good question! I haven't taught the concept of references at this point but when you do string color = colorOrder[i]; in C++ you are actually making a copy of the value (creating another string). This is not good in terms of performance if the vector were to contain a large number of strings because we just need to read the value and not modify it.
      Therefore, we should assign a reference (string&) instead. For primitive types (int, float, double, bool, char ...) it is actually more performant to make a copy of the value than to reference it. This is due to how references are implemented.
      Try changing string to string& and see what the output becomes:
      vector colors = {"Blue", "Red", "Green", "White", "Black"};
      for (size_t i = 0; i < colors.size(); i++) {
      string color = colors[i];
      color[0] = '1';
      }
      for (size_t i = 0; i < colors.size(); i++) {
      cout