Visual C # for beginners. Lecture 4. Conditions and cycles

    Good day, harazhiteli!
    For those who want to start programming in C #, I am posting my fourth lecture on the topic: “Conditions and cycles” . The lecture was very large (for an entire hour), so whoever is ready to watch for so long I want patience and a great desire not to stop there.

    Links to previous lectures

    Lecture 1. Introduction
    Lecture 2. Hello, World! and familiarity with Visual C # Express 2010
    Lecture 3. Variables and expressions

    And now - answers to the previous homework:
    1. Invalid names for variables are: 100metres (since the variable name cannot begin with a digit) and csharp.com (since the name the variable contains an extraneous character ".")
    2. The string " thisisveryverylongstringindeedisntit " is NOT too large to fit in the string type, because the memory allocated for variables of this type is dynamic and varies depending on the size.
    3. Given the priority of operations expression:resultVar + = var1 * var2 + var3% var4 / var5;
    will be executed with the following sequence:
    1) *
    2)%
    3) /
    4) +
    5) + =

    I hope that you did not have problems with homework, many even sent me answers to the mail, which is very nice, but we’re moving on to the most interesting.
    Happy viewing!



    New homework:


    1. If there are two integers stored in the variables var1 and var2, what kind of Boolean check should be performed to find out if one or the other (but not both together) is greater than 10?
    2. Write an application that receives two numbers from the user and displays them on the screen, but rejects the options when both numbers are greater than 10, and suggests in this case to enter two other numbers.
    3. What is wrong in the following code (try to solve this task without using Visual Studio)?
    1.       int i = 10;
    2.       for (i = 1; i <= 10; i++)
    3.       {
    4.         if ((i % 2) = 0)
    5.           continue;
    6.         Console.WriteLine(i);
    7.       }
    * This source code was highlighted with Source Code Highlighter.


    If you want answers, you can also send me an e-mail or just do it for yourself. Also with pleasure I will accept any advice on improving the course. For a small bonus, please read below.

    Code examples that were used in the lecture.



    1. Application of Boolean operations

    1.     static void Main(string[] args)
    2.     {
    3.       int myInt;
    4.       Console.WriteLine("Enter an integer:"); // Введите целое число
    5.       Int32.TryParse(Console.ReadLine(), out myInt);
    6.       Console.WriteLine("Integer less than 10? {0}", myInt < 10);
    7.       Console.WriteLine("Integer between 0 and 5? {0}", (myInt >= 0) && (myInt <= 5));
    8.       Console.ReadKey();
    9.     }
    * This source code was highlighted with Source Code Highlighter.


    2. Using goto, if else,?: (Ternary operator)

    1.     static void Main(string[] args)
    2.     {
    3.       string result = String.Empty;
    4.       double var1 = 0, var2 = 0;
    5.  
    6.       begin1:
    7.       Console.WriteLine("Enter first number:");
    8.       if (!Double.TryParse(Console.ReadLine(),out var1)) // здесь я упростил код просто поставив "!" перед выражением bool
    9.       {
    10.         Console.WriteLine("You should enter a double value.");
    11.         goto begin1;
    12.       }
    13.  
    14.       begin2:
    15.       Console.WriteLine("Enter second number:");
    16.       if (!Double.TryParse(Console.ReadLine(), out var2))
    17.       {
    18.         Console.WriteLine("You should enter a double value.");
    19.         goto begin2;
    20.       }
    21.  
    22.       if (var1 < var2)
    23.         result = "less than";
    24.       else
    25.       {
    26.         result = var1 == var2 ? "equal to" : "greater than";
    27.       }
    28.  
    29.       Console.WriteLine("The first number is {0} the second number.", result);
    30.       Console.ReadKey();
    31.     }
    * This source code was highlighted with Source Code Highlighter.


    3. Using the switch statement

    1.     static void Main(string[] args)
    2.     {
    3.       const int fail = 10;
    4.       int value = 0;
    5.  
    6.       switch (value)
    7.       {
    8.         case 1:
    9.           Console.WriteLine("This is one");
    10.           break;
    11.         case 2:
    12.           Console.WriteLine("This is two");
    13.           break;
    14.         case fail:
    15.           Console.WriteLine("This is fail");
    16.           break;
    17.         default:
    18.           Console.WriteLine("This is default");
    19.           break;
    20.       }
    21.     }
    * This source code was highlighted with Source Code Highlighter.


    4. Application of cycles

    1.     static void Main(string[] args)
    2.     {
    3.       double balance = 0, interestRate = 0, targetBalance = 0;
    4.       Console.WriteLine("What is your current balance?");
    5.       Double.TryParse(Console.ReadLine(), out balance);
    6.       Console.WriteLine("What is your current interest rate (in %)?");
    7.       Double.TryParse(Console.ReadLine(), out interestRate);
    8.       interestRate = 1 + interestRate/100.0;
    9.       Console.WriteLine("What balance would you like to have?");
    10.       Double.TryParse(Console.ReadLine(), out targetBalance);
    11.  
    12.       int totalYears = 0;
    13.  
    14.       while (balance < targetBalance)
    15.       {
    16.         balance *= interestRate;
    17.         totalYears++;
    18.       }
    19.  
    20.       Console.WriteLine("In {0} year{1} you will have a balance of {2}.", totalYears,
    21.         totalYears == 1 ? "" : "s", balance);
    22.       Console.ReadKey();
    * This source code was highlighted with Source Code Highlighter.

    Also popular now: