📜 ⬆️ ⬇️

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

Good day, habrazhiteli!
For those who want to start programming in C #, I post the fourth lecture on the topic: “Conditions and cycles” . The lecture turned out to be very large (for a whole hour), therefore, who is ready to watch for so long, I want patience and a great desire not to stop at what has been achieved.

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 - the 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 variable name contains an extra character ".")
2. The string " thisisveryverylongstringindeedisntit " is NOT too large to fit into the type of string, since the memory allocated for variables of this type is dynamic and changes depending on the size.
3. Considering the priority of the expression operations: resultVar + = var1 * var2 + var3% var4 / var5;
will be executed with the following sequence:
one) *
2)%
3) /
4) +
5) + =

I hope that you didn’t have problems with homework, many even sent me answers by mail, which is very nice, but we turn to the most interesting.
Enjoy watching!
')


New homework:


1. If there are two integers stored in the variables var1 and var2, which boolean check needs to be performed to find out if one or the other (but not both) 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 in this case offers to enter two other numbers.
3. What is wrong with the following code (try to solve this task without using Visual Studio)?
int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  1. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  2. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  3. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  4. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  5. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  6. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
  7. int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .
int i = 10; for (i = 1; i <= 10; i++) { if ((i % 2) = 0) continue ; Console .WriteLine(i); } * This source code was highlighted with Source Code Highlighter .


Answers, if you want, can also send me an email or just do for yourself. Also, I will gladly 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:" ); // 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. Use of goto, if else,?: Operators (ternary operator)

  1. static void Main ( string [] args)
  2. {
  3. string result = String .Empty;
  4. double var1 = 0, var2 = 0;
  5. begin1:
  6. Console .WriteLine ( "Enter first number:" );
  7. if (! Double.TryParse ( Console .ReadLine (), out var1)) // here I simplified the code by simply putting "!" before bool expression
  8. {
  9. Console .WriteLine ( "You should enter a double value." );
  10. goto begin1;
  11. }
  12. begin2:
  13. Console .WriteLine ( "Enter second number:" );
  14. if (! Double.TryParse ( Console .ReadLine (), out var2))
  15. {
  16. Console .WriteLine ( "You should enter a double value." );
  17. goto begin2;
  18. }
  19. if (var1 <var2)
  20. result = "less than" ;
  21. else
  22. {
  23. result = var1 == var2? "equal to" : "greater than" ;
  24. }
  25. Console .WriteLine ( "The first number is {0} the second number." , Result);
  26. Console .ReadKey ();
  27. }
* 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. switch ( value )
  6. {
  7. case 1:
  8. Console .WriteLine ( "This is one" );
  9. break ;
  10. case 2:
  11. Console .WriteLine ( "This is two" );
  12. break ;
  13. case fail:
  14. Console .WriteLine ( "This is fail" );
  15. break ;
  16. default :
  17. Console .WriteLine ( "This is default" );
  18. break ;
  19. }
  20. }
* 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. int totalYears = 0;
  12. while (balance <targetBalance)
  13. {
  14. balance * = interestRate;
  15. totalYears ++;
  16. }
  17. Console .WriteLine ( "In {0} year {1} you will have a balance of {2}." , TotalYears,
  18. totalYears == 1? "" : "s" , balance);
  19. Console .ReadKey ();
* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/107957/


All Articles