Difference between revisions of "Loop"

esse quam videri
Jump to: navigation, search
(Examples)
(Examples)
Line 35: Line 35:
 
// This example will look at a list of names and shuffle them. Suppose this will determine who the winner of a raffle will be.
 
// This example will look at a list of names and shuffle them. Suppose this will determine who the winner of a raffle will be.
  
string[] raffleNames= {Adam, Ben, Charile, Derek, Erik, Frank };
+
string[] raffleNames= { "Adam", "Ben", "Charile", "Derek", "Erik", "Frank" };
 
string tempName; // When shuffling, a temporary variable will be needed in order to prevent losing data.
 
string tempName; // When shuffling, a temporary variable will be needed in order to prevent losing data.
 
Random r = new Random(); // Random is a class available in C# that allows for generating random numbers. Great for our purposes of shuffling!
 
Random r = new Random(); // Random is a class available in C# that allows for generating random numbers. Great for our purposes of shuffling!
Line 50: Line 50:
 
randomNumberGenerated = r.Next(raffleNames.Length);
 
randomNumberGenerated = r.Next(raffleNames.Length);
 
string winner = raffleNames[randomNumberGenerated];
 
string winner = raffleNames[randomNumberGenerated];
 +
console.WriteLine(winner);
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:38, 21 August 2019

Definition

Relevance

Examples

Below are examples of loops in C#

// Do-While Loop: This type of loop will always run at least once because it will not check the termination condition until after the first run.
// This example will loop through an example game's prompt for a player to make a choice. Until the player makes a valid choice, the loop will continue asking the for the player's input.

string choice; // This will be used to store the user's input.
bool validSelection; // This will be used to terminate the loop.

do
{
    console.WriteLine("Which path will you take? 1 or 2?"); // prompt for asking the user.
    choice = console.ReadLine(); // getting the user's input.

    if(choice == "1" || choice == "2") // checking to see if the choice is valid.
    {
        validSelection = true; // valid choice, no longer need to loop.
    }
    else
    {
        validSelection = false; // invalid choice. Because the variable is set to false, it will cause the loop to repeat, meaning that the user will be asked to make a choice again.
    }

} while(validSelection == false) // if validSelection is set to false, repeat the loop.
// For Loop: This type of loop will run the specified number of times. Typically used for iterating through collections such as arrays or lists.
// This example will look at a list of names and shuffle them. Suppose this will determine who the winner of a raffle will be.

string[] raffleNames= { "Adam", "Ben", "Charile", "Derek", "Erik", "Frank" };
string tempName; // When shuffling, a temporary variable will be needed in order to prevent losing data.
Random r = new Random(); // Random is a class available in C# that allows for generating random numbers. Great for our purposes of shuffling!
int randomNumberGenerated; // Although Random can generate a random number, we'll be using this integer to store that generated number.

for(int i = 0; i < raffleNames.Length; i++)
{
    randomNumberGenerated = r.Next(raffleNames.Length); // r.Next is a function that accepts an integer as an argument to determine the range. raffleNames.Length is a property of the array that is an integer, that's what we're using for the argument.
    tempName = raffleNames[i];
    raffleNames[i] = raffleNames[randomNumberGenerated];
    raffleNames[randomNumberGenerated] = tempName;
}

randomNumberGenerated = r.Next(raffleNames.Length);
string winner = raffleNames[randomNumberGenerated];
console.WriteLine(winner);

Resources

See also

Notes

External Links