14. Control structures in C#

Página 14

Control Structures in C#

When programming in C#, an object-oriented programming language used in Unity game development, it is essential to understand control structures. They allow you to control the flow of program execution and make decisions based on conditions. Control structures in C# include conditional statements, loops, and jumps.

Conditional Statements

Conditional statements allow the program to make decisions based on specific conditions. They are essential for creating interactive and responsive games. The main conditional statements in C# are if, else if, else and switch.

If: The 'if' statement tests a condition and executes a block of code if the condition is true. For example:

if (Playerlife > 0)
{
    // Code to continue the game
}

Else if: The 'else if' statement is used to test multiple conditions. If the 'if' condition is false, the 'else if' condition is tested. If the 'else if' condition is true, the corresponding code block is executed. For example:

if (Playerlife > 70)
{
    // Code for 'Healthy' status
}
else if (Playerlife > 30)
{
    // Code for 'Injured' status
}

Else: The 'else' statement is used when all previous conditions are false. The code block inside 'else' is executed. For example:

if (Playerlife > 70)
{
    // Code for 'Healthy' status
}
else if (Playerlife > 30)
{
    // Code for 'Injured' status
}
else
{
    // Code for 'Critical' status
}

Switch: The 'switch' statement is used to select a block of code to be executed from multiple options. Each option is called 'case', and the program will execute the block of code corresponding to the variable's value. For example:

switch (playerlevel)
{
    case 1:
        // Code for level 1
        break;
    case 2:
        // Code for level 2
        break;
    default:
        // Code for unknown levels
        break;
}

Loops

Loops allow you to execute a block of code multiple times. They are useful for repetitive tasks. The main types of loops in C# are for, while and do-while.

For: The 'for' loop is used when you know how many times the block of code should be repeated. For example:

for (int i = 0; i < 10; i++)
{
    // Code to be repeated 10 times
}

While: The 'while' loop is used when you want the code block to be repeated while a condition is true. For example:

while (Playerlife > 0)
{
    // Code to continue the game
}

Do-while: The 'do-while' loop is similar to the 'while' loop, but the condition is tested after executing the code block. This ensures that the code block is executed at least once. For example:

of
{
    // Code to play a round
}
while (Playerlife > 0);

Heels

Jump statements allow you to control the flow of the program by jumping to other parts of the code. The main jump statements in C# are break, continue, and return.

Break: The 'break' statement is used to exit the current loop or switch. For example:

for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        break;
    }
    // Code to be repeated until i is equal to 5
}

Continue: The 'continue' statement is used to skip the rest of the current loop and start the next iteration. For example:

for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        continues;
    }
    // Code to be repeated, skipping when i is equal to 5
}

Return: The 'return' statement is used to end the execution of the current function and return a value. For example:

int Sum(int a, int b)
{
    return a + b;
}

In summary, control structures in C# are fundamental for programming games with Unity. They allow you to control the flow of program execution, make decisions based on conditions, and perform repetitive tasks. Mastering these control structures will enable you to create more complex and interactive games.

Now answer the exercise about the content:

What are the main C# control structures used in game programming with Unity?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

1515. Functions and procedures in C#

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text