C# - Basic & Control Flow

    科技2022-08-18  110

    Basic variables and operation

    Read line

    Console.WriteLine("Enter your name: "); name = Console.ReadLine();

    Variables and Types

    string foo = "Hello"; int x = 5; Console.WriteLine(foo); double x = 81; Console.Write(Math.Sqrt(x)); bool a = (2>1); bool b = a && true; bool c = !false || (7<8);

    Math operator

    double pow_ab = Math.Pow(6,2) a++ b--

    String operator

    string upperstr2 = str2.ToUpper(); strong lower = mixedCase.ToLower(); string str = "Divyesh"; int index1 = str.IndexOf('s'); char first = str[0]; string test1 = str.Substring(2); string name = firstName + " " + lastName; str.Length; Console.WwriteLine("Hello\nWorld");

    Control Flow

    If Statement & Else Clause & If and Else If

    if (true) { Console.WriteLine("Hello User!"); } if (true) { Console.WriteLine("Seen!"); } else { Console.WriteLine("Not seen!"); } int x = 100, y = 80; if (x > y) { Console.WriteLine("x is greater than y"); } else if (x < y) { Console.WriteLine("x is less than y"); } else { Console.WriteLine("x is equal to y'); }

    Switch Statements & Break Keyword: evaluates one expression and decides which code block to run by trying to match the result of the expression to each case. Switch statements are often used to replace if else structures when all conditions test for equality on one value.

    string color = "blue"; switch (color) { case "red": Console.WriteLine("I don't like that color."); break; case "blue": Console.WriteLine("I like that color."); break; default: Console.WriteLine("I feel ambivalent about that color."); break; }

    Ternary Operator

    bool isRaining = true; string umbrellaOrNot = isRaining ? "Umbrella" : "No Umbrella";
    Processed: 0.034, SQL: 9