Post #4 Conditional Statements

Conditional Statements: Conditional Statements in programming are statements that give different results depending on a value. They require certain conditions to give different results. C# is no exception to this definition. The if statement is the main conditional statement used for C#. If statements require a condition or multiple conditions to be completed to give a specific result. If statements can have multiple parts. You can just write if, you can also write if else, or even if else if else. If statements are obviously used the same way they are in Java. Switch statements are another type of conditional. A switch statement checks the value of a variable against a number of conditions. Switch statements are similar to if statements. There is a difference. Switch statements only check the value of one variable to conditions. Using the word conditions might be a little confusing so borrowing a word from C# and the creator of C# these are cases not conditions. A variable is given to a switch statement. The switch statement checks against cases or values that the variable might be set to. You should probably always use a default case that will account for if you're variable is not set to any cases you provided. You can have two cases give the same result. These are the conditionals of C#. Now for the code.

using System;

namespace Conditionals
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating variables
            string word = "hello";
            string animal = "dog";
            int x = 2 + 3;
            int i = 2;
            //saying hello
            if(word == "hello"){
                Console.WriteLine("Hello World!");
            }

            // example of using scopes again
            {
                int count = 0;
                {
                    while(count < 3){
                        if(animal == "flyingsquirrel"){
                            Console.WriteLine("He's flying!");
                            x = x + 5;
                            animal = "dog";
                            count++;
                        } else if(animal == "bird"){
                            Console.WriteLine("It's a bird!");
                            animal = "cat";
                            count++;
                        } else if(animal == "cat"){
                            Console.WriteLine("meow meow meow");
                            animal = "dog";
                            count++;
                        }else if (animal == "dog"){
                            Console.WriteLine("bark bark bark");
                            animal = "cat";
                            count++;
                        }
                    }
                }
                // testing something I didn't answer last time
                Console.WriteLine(count);
                if(x > 10){
                    Console.WriteLine(11);
                }else{
                    Console.WriteLine(x);
                }
            }
            // using a switch statement
            switch(i){
            case 0:
                 Console.WriteLine(0);
                break;
            case 1:
                 Console.WriteLine(1);
                break;
            case 2:
                 Console.WriteLine(2);
                break;
            default:
                 Console.WriteLine(i);
                break;
            }
        }
    }
}
Program of Conditional statements. Creates variables. Says Hello World. Goes into a
scope for fun. Creates count which can't be used outside of it's scope but can effect
scopes inside it. Goes through while loop. Checks if statements for what animal is and
increases count by one. The next line prints count to check the value of count after it
goes into the scope inside it's scope. The answer to the question "Will a variable have
the same value after it goes through an inner scope?" is no. I then added a switch
statement to show the format. It has the keyword switch, then parenthesis variable
end parenthesis, then curly brackets. It checks the value of that variable against the
cases(or switch sections). It does only what's inside of that case. Important note
you must always use the keyword break or you will error out. Second note though I did
not show this you can have multiple labels(case 1: case 3: does something important)
as part of the same switch section. When I say switch section I mean case or set of
cases.

I used the book The C# Programming Language Fourth Edition By Anders Hejlsberg, Mads
Torgersen, Scott Wiltamuth, and Peter Golde for information in this post.

Logic Operators: Another quick note. C# can use logic operators which I did not say
before. You can have multiple conditions using them and you can say something is not
that value. Here they are.
&& is AND, || is OR, ! is NOT

Resources: Same Resources
MICROSOFT VISUAL C# .NET by Mickey Williams
PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde

Visual Studio Code Environment:

Link to get started:

Comments

Popular posts from this blog

Post #11 Complete Arcade

Post #10 Arcade

Post #2 Hello World!