Post #3 Costants Variables and Arithmetic Operations

Variables: First let's discuss variables. There are basically the same variable types we have seen before in Java(char, int, string, Boolean, float, and double, etc). They work pretty much the same way. There does seem to be an indication of at least one major change. That is that a variable can only work within its scope. Similar to how in Java a variable can't be used outside of it's method, for C# it can't be used outside it's scope. A scope is basically just a range limit which you can place anywhere. Normally of course you place your variables at the top of the main method after the beginning curly brace. Those are your basic global variables. This is a little weird but you can throw a set of curly braces anywhere and create a scope inside your main method. When you initialize a variable in a scope it cannot be used outside of it. If the variable is directly inside the main method it can be used in all scopes because it's a global variable for the main method. If it is inside a scope that has other scopes nested in it, it can be used in all those scopes but not outside the main scope it's in. It seems like this is a way to isolated code so you can work on bits and pieces without effecting everything. It's basically just like calling a method once. The main thing being if you go outside of the scope it will error you out. As far as I can tell you can have as many scopes as you want via adding curly braces. I hope I explained variables well, leave comments below.

Constants: Constants are variables that cannot be changed. In C#'s case they are initialized through the key word const followed by variable type and name and whatever it is set too. They work the same way as Java mostly. The most common constants are known as literals. They again all start with the word const at the declaring point. In addition, there is a set of escape sequences. These I'm putting in here because they are relevant to coding. They actually code special characters in strings. \\ will give you one backslash. \' will give you a comma. \" will be your parenthesis. \? will give you a question mark. Most of these give you different actions. \a will give you an alert or bell. \b will give you a backspace. \f will let a character be a form feed character. \n will give you a new line. \r will give you a return character. \t is a horizontal tab. \v is a vertical tab. To sum up Constants are variables that can not be changed. Escape sequences are basically how you get special characters in strings. I hope I explained constants well, leave comments below. 

Operators: This is basically just a lot of math. It uses the basics we've seen in Java and more. The basics are +, -, *, /, %, ++, -- which work the same way as in Java. Those are the Arithmetic operators for C#. Relational Operators are what we use for conditionals. ==, !=, >, <, >=, <=, work the same and are all of C# relational operators. Logical Operators are AND OR NOT. && is AND, || is OR, ! is NOT. They work the same way. Assignment Operators are used to change the value of something while keeping that change or just initialize. =, +=, -= are ones we've used in Java. You can also *=, /=, and %= which I don't know if these existed in Java but they exist here. There are also Bitwise Operators which I'm not going to explain but they are interesting. These are not just Arithmetic Operators but all the types of Operators I have learned in C#. I hope I explained well, leave comments below.

using System;

namespace Variables
{
    class Program
    {

        static void Main(string[] args)
        {
            // initializing variables here will make them global to entire main 
// method
            char part1 = 'p';
            char part2 = 'a';
            char part3 = 'r';
            char part4 = 't';
            const string p = "Potato";
            Boolean var = true;
            
            // chars can be combined like normal. 
            Console.WriteLine("This is a " + part1 + part2 + part3 + part4);

            // This is the start of a scope.
            {
                // These variables can only be used inside the scope otherwise ERROR.
                int count = 0;
                int three = 3;
                int two = 2

                // This is just me showcasing ints
                while(count<5){
                    count++;
                }
                Console.WriteLine(count + " fingers ");
                if(count == 5){
                    Console.WriteLine(var);
                }else{
                    var = false;
                    Console.WriteLine(var);
                }

                count = count + three;

                if(count == 10){
                    Console.WriteLine("I have " + count + " fingers.");
                }else{
                    Console.WriteLine("I have " + count + " fingers.");
                    Console.WriteLine("Oh wait I forgot two");
                    count = count + two;
                    Console.WriteLine("I have " + count + " fingers.");
                }
            }
            // Variables can still work after scopes if they are declared and 
// initialized outside of scopes.
            // Special note count, three, and two used here will cause ERROR.
            Console.WriteLine("This is a " + p);
        }
    }
}

This Program Accesses .Net Framework, creates namespace and class for project, and
creates the main method. I'm always probably going to use this main method as I do
not think I need to use the other ones. Inside main it creates variables and gives
them a value even though it is not required. It prints a line combining characters.
It uses a scope. The scope has it's own variables inside it to show how variables
must be used inside their scopes. It uses a couple of conditionals mostly just for
fun. It ends the scope and has a line after to show that variables can be used outside
if they are not initialized outside.
NOTE: DO NOT TRY TO USE VARIABLES OUTSIDE OF THEIR SCOPE IT WILL ERROR OUT!

OUTPUT
This is a part
5 fingers
true
I have 8 fingers
Oh wait I forgot two
I have 10 fingers
This is a Potato

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

I was mainly using the book by Nathan Clark for this Post

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!