Jump to content

quix

Manager
  • Posts

    563
  • Joined

  • Last visited

  • Days Won

    64

Posts posted by quix

  1. Raspunsuri la intrebarile de la Revolut (INCH) :

    Lesson 1 – Decentralised exchanges

    · Question #1: Who facilitates an exchange on a DEX?

    ·         Answer: No one. The exchange is made peer-to-peer.

    · Question #2: Which of the following is a decentralised exchange?

    ·         Answer: Uniswap.

    · Question #3: What does DEX stand for in crypto?

    ·         Answer: Decentralised exchange.

    Lesson 2 – How does 1inch get the best prices?

    · Question #1: What is the name of 1inch’s price search algorithm?

    ·         Answer: Pathfinder.

    · Question #2: How does 1inch find the best prices?

    ·         Answer: 1inch aggregates trade options across DEX platforms.

    · Question #3: Which of the following is not a feature of 1inch’s Pathfinder?

    ·         Answer: Provide directions to your destination.

    Lesson 3 – DAOs and governance

    · Question #1: Which of the following is a characteristic of a DAO?

    ·         Answer: Decentralised.

    · Question #2: Who can vote on 1inch proposals?

    ·         Answer: Users who stake their 1INCH tokens.

    · Question #3: What is an example of a proposal that 1inch DAO members could vote on?

    ·         Answer: Allocating treasury funds for a particular proposal.

     

    Mai jos aveti un tabel pentru a va usura treaba:

     

    Crypto basics answers table:

    Question

    Lesson 1

    Lesson 2

    Lesson 3

    Lesson 4

    Question 1:

    3

    3

    1

    2

    Question 2:

    2

    4

    4

    4

    Question 3:

    1

    1

    2

    1

    Polkadot answers table:

    Question

    Lesson 1

    Lesson 2

    Lesson 3

    Lesson 4

    Lesson 5

    Question 1:

    3

    4

    1

    3

    4

    Question 2:

    1

    1

    3

    1

    2

    Question 3:

    2

    2

    4

    1

    1

    1inch answers table:

    Question

    Lesson 1

    Lesson 2

    Lesson 3

    Question 1:

    4

    4

    4

    Question 2:

    3

    1

    2

    Question 3:

    1

    3

    1

     

  2. Raspunsuri la intrebarile de la Revolut (DOT) - part 2 :

     

    Lesson 1 – Intro to Polkadot

    · Question #1: What are some limitations of early blockchains like Bitcoin?

    ·         Answer: Slow transaction speeds, high fees and inability to communicate with other blockchains.

    · Question #2: What is Web 3.0?

    ·         Answer: A new web built around decentralised technologies, where users have more control of their data.

    · Question #3: How does Polkadot address some of the limitations of earlier blockchains?

    ·         Answer: Polkadot allows multiple blockchains to communicate with each other and process transactions at the same time.

    Lesson 2 – How Polkadot works

    · Question #1: What is a relay chain?

    ·         Answer: A blockchain that connects other blockchains and allows them to communicate.

    · Question #2: What is a parachain?

    ·         Answer: A blockchain connects to the Relay chain and operates parallel to other parachains.

    · Question #3: What problems do parachains and relay chains solve?

    ·         Answer: The inability of blockchains to communicate with each other.

    Lesson 3 – Who decides the future of Polkadot

    · Question #1: Who can vote for new initiatives, i.e. Polkadot network upgrades?

    ·         Answer: People who hold Polkadot (DOT) tokens.

    · Question #2: What is Polkadots ‘on-chain’ treasury?

    ·         Answer: A container of tokens that can be used to support projects that benefit the Polkadot network.

    · Question #3: How does Polkadot’s governance system differ from other popular blockchains?

    ·         Answer: It’s governed by a community through voting.

    Lesson 4 – DOT Token

    · Question #1: What is Polkadot’s crypto token called?

    ·         Answer: DOT.

    · Question #2: What does ‘staking’ your Polkadot (DOT) tokens mean?

    ·         Answer: It helps secure the network by locking up DOT tokens in return for a reward in DOT tokens.

    · Question #3: What does ‘bonding’ your Polkadot tokens mean?

    ·         Answer: Locking up tokens to secure a slot for your para chain on the relay chain.

    Lesson 5 – Polkadot and its uses

    · Question #1: What are some use cases of Polkadot?

    ·         Answer: NFTs, powering smart cities, DeFi, etc.

    · Question #2: What makes apps built on Polkadot different from the apps on your phone?

    ·         Answer: Polkadot applications don’t need to rely on a middleman to work and can keep your data secure.

    · Question #3: How do applications on parachains communicate?

    ·         Answer: Via the Relay Chain.

     

  3. Raspunsuri la intrebarile de la Revolut (DOT) - part 1 :

     

    Lesson 1 – Crypto vs fiat quiz answers

    · Question #1: What is ‘fiat’ money?

    ·         Answer: Government-issued money.

    · Question #2: What are the main differences between fiat currencies and cryptocurrencies?

    ·         Answer: Cryptocurrencies are decentralised, and a central authority controls fiat money.

    · Question #3: Who validates transactions on the blockchain?

    ·         Answer: Special users that are called ‘miners’ or validators.

     

    Lesson 2 – Cryptography in crypto

     

    · Question #1: Why is cryptography important for cryptocurrencies?

    ·         Answer: It (cryptography) removes the need for a central authority, prevents double spending, and adds security.

    · Question #2: What are private and public keys for cryptocurrencies?

    ·         Answer: A public key is like your account number, and your private key is like your password.

    · Question #3: Which statement is true about private and public keys?

    ·         Answer: It’s impossible to work out someone’s private key by looking at their public key.

     

    Lesson 3 – Basics of Blockchain

     

    · Question #1: What is a Blockchain?

    ·         Answer: A decentralised database.

    · Question #2: Who can view the Blockchain?

    ·         Answer: Anyone can view the Blockchain with a computer and an Internet connection.

    · Question #3: What makes the Blockchain different from a regular database?

    ·         Answer: It’s not controlled by any central party and can be viewed by anyone.

     

    Lesson 4 – Risks of crypto

     

    · Question #1: What regulatory protections does your crypto have?

    ·         Answer: None – crypto is not regulated in most countries worldwide.

    · Question #2: How much can you lose if you buy cryptocurrencies?

    ·         Answer: Everything! Your investment could go to zero.

    · Question #3: When would it not be suitable to buy cryptocurrencies?

    ·         Answer: When one is in debt or cannot afford to lose the money invested.

     

     O sa revin si cu un update pentru INCH!

  4. Modify the Pointer Value

    You can also change the pointer's value. But note that this will also change the value of the original variable:

    Example

    string food = "Pizza";
    string* ptr = &food;

    // Output the value of food (Pizza)
    cout << food << "\n";

    // Output the memory address of food (0x6dfed4)
    cout << &food << "\n";

    // Access the memory address of food and output its value (Pizza)
    cout << *ptr << "\n";

    // Change the value of the pointer
    *ptr = "Hamburger";

    // Output the new value of the pointer (Hamburger)
    cout << *ptr << "\n";

    // Output the new value of the food variable (Hamburger)
    cout << food << "\n";

     

     

    Thank you for being part of my journey to teaching you all the C++ Basics you'll need to start programming! Have fun coding 😄

  5. Get Memory Address and Value

    In the example from the previous page, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). However, you can also use the pointer to get the value of the variable, by using the * operator (the dereference operator):

    Example

    string food = "Pizza";  // Variable declaration
    string* ptr = &food;    // Pointer declaration

    // Reference: Output the memory address of food with the pointer (0x6dfed4)
    cout << ptr << "\n";

    // Dereference: Output the value of food with the pointer (Pizza)
    cout << *ptr << "\n";

     

    Note that the * sign can be confusing here, as it does two different things in our code:

    ·         When used in declaration (string* ptr), it creates a pointer variable.

    ·         When not used in declaration, it act as a dereference operator.

     

  6. Creating Pointers

    You learned from the previous chapter, that we can get the memory address of a variable by using the & operator:

    Example

    string food = "Pizza"; // A food variable of type string

    cout << food;  // Outputs the value of food (Pizza)
    cout << &food; // Outputs the memory address of food (0x6dfed4)

     

    pointer however, is a variable that stores the memory address as its value.

    A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator. The address of the variable you're working with is assigned to the pointer:

    Example

    string food = "Pizza";  // A food variable of type string
    string* ptr = &food;    // A pointer variable, with the name ptr, that stores the address of food

    // Output the value of food (Pizza)
    cout << food << "\n";

    // Output the memory address of food (0x6dfed4)
    cout << &food << "\n";

    // Output the memory address of food with the pointer (0x6dfed4)
    cout << ptr << "\n";

     

    Example explained

    Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're working with.

    Use the & operator to store the memory address of the variable called food, and assign it to the pointer.

    Now, ptr holds the value of food's memory address.

    Tip: There are three ways to declare pointer variables, but the first way is preferred:

    string* mystring; // Preferred
    string *mystring;
    string * mystring;

     

  7. Memory Address

    In the example from the previous page, the & operator was used to create a reference variable. But it can also be used to get the memory address of a variable; which is the location of where the variable is stored on the computer.

    When a variable is created in C++, a memory address is assigned to the variable. And when we assign a value to the variable, it is stored in this memory address.

    To access it, use the & operator, and the result will represent where the variable is stored:

    Example

    string food = "Pizza";

    cout << &food; // Outputs 0x6dfed4

     

    Note: The memory address is in hexadecimal form (0x..). Note that you may not get the same result in your program.

    And why is it useful to know the memory address?

    References and Pointers (which you will learn about in the next chapter) are important in C++, because they give you the ability to manipulate the data in the computer's memory - which can reduce the code and improve the performance.

    These two features are one of the things that make C++ stand out from other programming languages, like Python and Java.

     

  8. Creating References

    A reference variable is a "reference" to an existing variable, and it is created with the & operator:

    string food = "Pizza";  // food variable
    string &meal = food;    // reference to food

     

    Now, we can use either the variable name food or the reference name meal to refer to the food variable:

    Example

    string food = "Pizza";
    string &meal = food;

    cout << food << "\n";  // Outputs Pizza
    cout << meal << "\n";  // Outputs Pizza

     

  9. C++ Structures

    Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.

    Unlike an array, a structure can contain many different data types (int, string, bool, etc.).


    Create a Structure

    To create a structure, use the struct keyword and declare each of its members inside curly braces.

    After the declaration, specify the name of the structure variable (myStructure in the example below):

    struct {             // Structure declaration
      int myNum;         // Member (int variable)
      string myString;   // Member (string variable)
    } myStructure;       // Structure variable


    Access Structure Members

    To access members of a structure, use the dot syntax (.😞

    Example

    Assign data to members of a structure and print it:

    // Create a structure variable called myStructure
    struct {
      int myNum;
      string myString;
    } myStructure;

    // Assign values to members of myStructure
    myStructure.myNum = 1;
    myStructure.myString = "Hello World!";

    // Print members of myStructure
    cout << myStructure.myNum << "\n";
    cout << myStructure.myString << "\n";


    One Structure in Multiple Variables

    You can use a comma (,) to use one structure in many variables:

    struct {
      int myNum;
      string myString;
    } myStruct1, myStruct2, myStruct3; // Multiple structure variables separated with commas

    This example shows how to use a structure in two different variables:

    Example

    Use one structure to represent two cars:

    struct {
      string brand;
      string model;
      int year;
    } myCar1, myCar2; // We can add variables by separating them with a comma here

    // Put data into the first structure
    myCar1.brand = "BMW";
    myCar1.model = "X5";
    myCar1.year = 1999;

    // Put data into the second structure
    myCar2.brand = "Ford";
    myCar2.model = "Mustang";
    myCar2.year = 1969;

    // Print the structure members
    cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
    cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";


    Named Structures

    By giving a name to the structure, you can treat it as a data type. This means that you can create variables with this structure anywhere in the program at any time.

    To create a named structure, put the name of the structure right after the struct keyword:

    struct myDataType { // This structure is named "myDataType"
      int myNum;
      string myString;
    };

    To declare a variable that uses the structure, use the name of the structure as the data type of the variable:

    myDataType myVar;

    Example

    Use one structure to represent two cars:

    // Declare a structure named "car"
    struct car {
      string brand;
      string model;
      int year;
    };

    int main() {
      // Create a car structure and store it in myCar1;
      car myCar1;
      myCar1.brand = "BMW";
      myCar1.model = "X5";
      myCar1.year = 1999;

      // Create another car structure and store it in myCar2;
      car myCar2;
      myCar2.brand = "Ford";
      myCar2.model = "Mustang";
      myCar2.year = 1969;
     
      // Print the structure members
      cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
      cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";
     
      return 0;
    }

     

  10. Multi-Dimensional Arrays

    A multi-dimensional array is an array of arrays.

    To declare a multi-dimensional array, define the variable type, specify the name of the array followed by square brackets which specify how many elements the main array has, followed by another set of square brackets which indicates how many elements the sub-arrays have:

    string letters[2][4];
     

    As with ordinary arrays, you can insert values with an array literal - a comma-separated list inside curly braces. In a multi-dimensional array, each element in an array literal is another array literal.

    string letters[2][4] = {
      { "A", "B", "C", "D" },
      { "E", "F", "G", "H" }
    };

     

    Each set of square brackets in an array declaration adds another dimension to an array. An array like the one above is said to have two dimensions.

    Arrays can have any number of dimensions. The more dimensions an array has, the more complex the code becomes. The following array has three dimensions:

    string letters[2][2][2] = {
      {
        { "A", "B" },
        { "C", "D" }
      },
      {
        { "E", "F" },
        { "G", "H" }
      }
    };

     


    Access the Elements of a Multi-Dimensional Array

    To access an element of a multi-dimensional array, specify an index number in each of the array's dimensions.

    This statement accesses the value of the element in the first row (0) and third column (2) of the letters array.

    Example

    string letters[2][4] = {
      { "A", "B", "C", "D" },
      { "E", "F", "G", "H" }
    };

    cout << letters[0][2];  // Outputs "C"

    Remember that: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.


    Change Elements in a Multi-Dimensional Array

    To change the value of an element, refer to the index number of the element in each of the dimensions:

    Example

    string letters[2][4] = {
      { "A", "B", "C", "D" },
      { "E", "F", "G", "H" }
    };
    letters[0][0] = "Z";

    cout << letters[0][0];  // Now outputs "Z" instead of "A"


    Loop Through a Multi-Dimensional Array

    To loop through a multi-dimensional array, you need one loop for each of the array's dimensions.

    The following example outputs all elements in the letters array:

    Example

    string letters[2][4] = {
      { "A", "B", "C", "D" },
      { "E", "F", "G", "H" }
    };

    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 4; j++) {
        cout << letters[j] << "\n";
      }
    }

    This example shows how to loop through a three-dimensional array:

    Example

    string letters[2][2][2] = {
      {
        { "A", "B" },
        { "C", "D" }
      },
      {
        { "E", "F" },
        { "G", "H" }
      }
    };

    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          cout << letters[j][k] << "\n";
        }
      }
    }


    Why Multi-Dimensional Arrays?

    Multi-dimensional arrays are great at representing grids. This example shows a practical use for them. In the following example we use a multi-dimensional array to represent a small game of Battleship:

    Example

    // We put "1" to indicate there is a ship.
    bool ships[4][4] = {
      { 0, 1, 1, 0 },
      { 0, 0, 0, 0 },
      { 0, 0, 1, 0 },
      { 0, 0, 1, 0 }
    };

    // Keep track of how many hits the player has and how many turns they have played in these variables
    int hits = 0;
    int numberOfTurns = 0;

    // Allow the player to keep going until they have hit all four ships
    while (hits < 4) {
      int row, column;

      cout << "Selecting coordinates\n";

      // Ask the player for a row
      cout << "Choose a row number between 0 and 3: ";
      cin >> row;

      // Ask the player for a column
      cout << "Choose a column number between 0 and 3: ";
      cin >> column;

      // Check if a ship exists in those coordinates
      if (ships[row][column]) {
        // If the player hit a ship, remove it by setting the value to zero.
        ships[row][column] = 0;

        // Increase the hit counter
        hits++;

        // Tell the player that they have hit a ship and how many ships are left
        cout << "Hit! " << (4-hits) << " left.\n\n";
      } else {
        // Tell the player that they missed
        cout << "Miss\n\n";
      }

      // Count how many turns the player has taken
      numberOfTurns++;
    }

    cout << "Victory!\n";
    cout << "You won in " << numberOfTurns << " turns";

     

  11. Get the Size of an Array

    To get the size of an array, you can use the sizeof() operator:

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    cout << sizeof(myNumbers);

     

    Result:

    20

    Why did the result show 20 instead of 5, when the array contains 5 elements?

    It is because the sizeof() operator returns the size of a type in bytes.

    You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.

    To find out how many elements an array has, you have to divide the size of the array by the size of the data type it contains:

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    int getArrayLength = sizeof(myNumbers) / sizeof(int);
    cout << getArrayLength;

     

    Result:

    5


    Loop Through an Array with sizeof()

    In the Arrays and Loops Chapter, we wrote the size of the array in the loop condition (i < 5). This is not ideal, since it will only work for arrays of a specified size.

    However, by using the sizeof() approach from the example above, we can now make loops that work for arrays of any size, which is more sustainable.

    Instead of writing:

    int myNumbers[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
      cout << myNumbers << "\n";
    }

    It is better to write:

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < sizeof(myNumbers) / sizeof(int); i++) {
      cout << myNumbers << "\n";
    }

    Note that, in C++ version 11 (2011), you can also use the "for-each" loop:

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    for (int i : myNumbers) {
      cout << i << "\n";
    }

     

    It is good to know the different ways to loop through an array, since you may encounter them all in different programs.

     

  12. Omit Array Size

    In C++, you don't have to specify the size of the array. The compiler is smart enough to determine the size of the array based on the number of inserted values:

    string cars[] = {"Volvo", "BMW", "Ford"}; // Three arrays
     

    The example above is equal to:

    string cars[3] = {"Volvo", "BMW", "Ford"}; // Also three arrays
     

    However, the last approach is considered as "good practice", because it will reduce the chance of errors in your program.


    Omit Elements on Declaration

    It is also possible to declare an array without specifying the elements on declaration, and add them later:

    Example

    string cars[5];
    cars[0] = "Volvo";
    cars[1] = "BMW";
    ...

     

  13. Loop Through an Array

    You can loop through the array elements with the for loop.

    The following example outputs all elements in the cars array:

    Example

    string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
    for (int i = 0; i < 5; i++) {
      cout << cars << "\n";
    }

     

    This example outputs the index of each element together with its value:

    Example

    string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
    for (int i = 0; i < 5; i++) {
      cout << i << " = " << cars << "\n";
    }

     

    And this example shows how to loop through an array of integers:

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
      cout << myNumbers << "\n";
    }

     


    The foreach Loop

    There is also a "for-each loop" (introduced in C++ version 11 (2011), which is used exclusively to loop through elements in an array:

    Syntax

    for (type variableName arrayName) {
      // code block to be executed
    }

     

    The following example outputs all elements in an array, using a "for-each loop":

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    for (int i : myNumbers) {
      cout << i << "\n";
    }

     

  14. C++ Arrays

    Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

    To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store:

    string cars[4];
     

    We have now declared a variable that holds an array of four strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:

    string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
     

    To create an array of three integers, you could write:

    int myNum[3] = {10, 20, 30};
     


    Access the Elements of an Array

    You access an array element by referring to the index number inside square brackets [].

    This statement accesses the value of the first element in cars:

    Example

    string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
    cout << cars[0];
    // Outputs Volvo

     

    Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.


    Change an Array Element

    To change the value of a specific element, refer to the index number:

    cars[0] = "Opel";
     

    Example

    string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
    cars[0] = "Opel";
    cout << cars[0];
    // Now outputs Opel instead of Volvo

     

  15. C++ Break

    You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.

    The break statement can also be used to jump out of a loop.

    This example jumps out of the loop when i is equal to 4:

    Example

    for (int i = 0; i < 10; i++) {
      if (i == 4) {
        break;
      }
      cout << i << "\n";
    }

     

     


    C++ Continue

    The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

    This example skips the value of 4:

    Example

    for (int i = 0; i < 10; i++) {
      if (i == 4) {
        continue;
      }
      cout << i << "\n";
    }

     

     



    Break and Continue in While Loop

    You can also use break and continue in while loops:

    Break Example

    int i = 0;
    while (i < 10) {
      cout << i << "\n";
      i++;
      if (i == 4) {
        break;
      }
    }

     

    Continue Example

    int i = 0;
    while (i < 10) {
      if (i == 4) {
        i++;
        continue;
      }
      cout << i << "\n";
      i++;
    }

     

  16. C++ For Loop

    When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

    Syntax

    for (statement 1; statement 2; statement 3) {
      // code block to be executed
    }

     

    Statement 1 is executed (one time) before the execution of the code block.

    Statement 2 defines the condition for executing the code block.

    Statement 3 is executed (every time) after the code block has been executed.

    The example below will print the numbers 0 to 4:

    Example

    for (int i = 0; i < 5; i++) {
      cout << i << "\n";
    }

     

    Example explained

    Statement 1 sets a variable before the loop starts (int i = 0).

    Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.

    Statement 3 increases a value (i++) each time the code block in the loop has been executed.


    Another Example

    This example will only print even values between 0 and 10:

    Example

    for (int i = 0; i <= 10; i = i + 2) {
      cout << i << "\n";
    }

     


    Nested Loops

    It is also possible to place a loop inside another loop. This is called a nested loop.

    The "inner loop" will be executed one time for each iteration of the "outer loop":

    Example

    // Outer loop
    for (int i = 1; i <= 2; ++i) {
      cout << "Outer: " << i << "\n"; // Executes 2 times

      // Inner loop
      for (int j = 1; j <= 3; ++j) {
        cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3)
      }
    }

     


    The foreach Loop

    There is also a "for-each loop" (introduced in C++ version 11 (2011), which is used exclusively to loop through elements in an array (or other data sets):

    Syntax

    for (type variableName arrayName) {
      // code block to be executed
    }

     

    The following example outputs all elements in an array, using a "for-each loop":

    Example

    int myNumbers[5] = {10, 20, 30, 40, 50};
    for (int i : myNumbers) {
      cout << i << "\n";
    }

     

    Note: Don't worry if you don't understand the example above. You will learn more about arrays in the C++ Arrays Chapter.

     

  17. The Do/While Loop

    The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

    Syntax

    do {
      // code block to be executed
    }
    while (condition);

     

    The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

    Example

    int i = 0;
    do {
      cout << i << "\n";
      i++;
    }
    while (i < 5);

     

×
×
  • Create New...