PD1: Chapter 5: Iterations

Cards (35)

  • What is the generic List datatype in C#?
    A generic collection datatype that contains a dynamic number of elements
  • What is required to complete the generic List datatype?
    An extra datatype known as the Type parameter
  • What are examples of List datatypes in C#?
    List<int>, List<string>, List<float>
  • How does the datatype inside a List affect its elements?
    It specifies which datatype the elements in the list adhere to
  • What notation is used to retrieve an item at a specific position in a List?
    Square brackets notation []
  • What is the term for the squared bracket notation used in Lists?
    Indexer
  • What method can be used to remove all elements from a List?
    The Clear method
  • How does the capacity of a List change when elements are added?
    It grows dynamically as needed
  • What is the purpose of iteration statements in C#?
    To execute commands multiple times
  • What must be written between parentheses in a while statement?
    The test condition
  • What happens if the test in a while statement fails immediately?
    The code block is not executed
  • What is a potential risk of a while loop if the test is always true?
    It can lead to an infinite loop
  • When is a while loop particularly useful?
    When the number of iterations is not known in advance
  • What does the second while loop in the practical example do?
    Generates random numbers until the secret number is found
  • What happens if the user provides a number outside the range [0-20]?
    The program will never find a solution
  • How does a do statement differ from a while statement?
    The test comes after the code block in a do statement
  • What is a benefit of using a do statement?
    The code block is executed at least once
  • What is the structure of a for statement in C#?
    It contains a for keyword with three control elements
  • What are the three control elements in a for statement?
    Initializer, test condition, and increment
  • What is the significance of the control variable in a for loop?
    It is used to process elements related to the loop
  • What does it mean that positions in a list are 0 based?
    The first index is 0, meaning indices range from 0 to count-1
  • What is the purpose of the foreach instruction?
    To process each element in a collection automatically
  • What does the break keyword do in a loop?
    It moves control to the first instruction after the loop
  • What is the function of the continue keyword in a loop?
    It halts the current iteration and starts the next one
  • What is the yield keyword used for?
    To simulate collection behavior without providing the collection
  • Why is it not allowed to delete an item from a List using a foreach statement?
    It causes a runtime error
  • What happens if you try to delete items from a List while iterating through it?
    Only half the elements may be removed
  • What is the recommended method to delete all items in a List?
    Use the Clear method
  • What is the relationship between while and for statements?
    Each can perform the same logic
  • What must be included in a for loop to avoid an infinite loop?
    A break or return statement
  • What are the key features of the List datatype in C#?
    • Generic collection datatype
    • Dynamic number of elements
    • Requires a Type parameter
    • Supports indexers for element retrieval
    • Allows various operations using dot-notation
    • Supports Clear method to remove all elements
    • Dynamic capacity that grows as needed
  • What are the types of iteration statements in C#?
    1. while statement
    2. do statement
    3. for statement
    4. foreach statement
  • What are the differences between while, do, and for statements?
    • while: test condition before executing code block
    • do: test condition after executing code block
    • for: includes initialization, test condition, and increment in one line
  • What are the uses of break, continue, and yield keywords in loops?
    • break: exits the loop
    • continue: skips to the next iteration
    • yield: simulates collection behavior without a complete collection
  • What are the methods for deleting elements in a List?
    • Use Clear method to remove all elements
    • Avoid deleting while iterating with foreach
    • Use a for loop to remove specific elements