Advertisement
yakovmonarh

Untitled

Aug 11th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. // 1.3.8.3 Подумайте когда лучше использовать цикл for а когда while.  И приведите наилучший пример использования того и того цикла.
  2.  
  3.         public static void Main(string[] args)
  4.         {  
  5.             OptimalCycleFor();
  6.             Console.ReadKey();
  7.             OptimalCycleWhile();
  8.             Console.ReadKey();
  9.         }
  10.        
  11.         static void OptimalCycleWhile()
  12.         {
  13.             int countUser;
  14.             int trueCount = 4;
  15.             do
  16.             {
  17.                 Console.Clear();
  18.                 Console.WriteLine("Сколько будет: 2 * 2 ?");
  19.             }
  20.             while(int.TryParse(Console.ReadLine(), out countUser) != true || countUser != trueCount);
  21.         }
  22.        
  23.         static void OptimalCycleFor()
  24.         {
  25.             Console.WriteLine("Введите целое число.");
  26.             int countUser;
  27.             if(int.TryParse(Console.ReadLine(), out countUser) == false)
  28.             {
  29.                 return;
  30.             }
  31.             int sum = 0;
  32.             for(int i = 1; i <= countUser; i++)
  33.             {
  34.                 sum += i;
  35.             }
  36.             Console.WriteLine("Сумма всех чисел: {0}", sum);
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement