Advertisement
yahorrr

Untitled

Feb 9th, 2022
1,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.08 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SelectionStatments
  4. {
  5.     public static class Statements
  6.     {
  7.         public static void WriteLargestWithNestedIfElse(int first, int second, int third)
  8.         {
  9.             int max = first;
  10.             if (second >= max)
  11.             {
  12.                 max = second;
  13.             }
  14.  
  15.             if (third >= max)
  16.             {
  17.                 max = third;
  18.             }
  19.  
  20.             Console.WriteLine($"Number {max} is the largest");
  21.         }
  22.  
  23.         public static void WriteLargestWithIfElseAndTernaryOperator(int first, int second, int third)
  24.         {
  25.             int max = first >= second ? first : second;
  26.             max = max >= third ? max : third;
  27.             Console.WriteLine($"Number {max} is the largest");
  28.         }
  29.  
  30.         public static void WriteLargestWithIfElseAndConditionLogicalOperators(int first, int second, int third)
  31.         {
  32.             int max = first;
  33.  
  34.             if (second > first && second > third)
  35.             {
  36.                 max = second;
  37.             }
  38.             else if (third > first && third > second)
  39.             {
  40.                 max = third;
  41.             }
  42.  
  43.             Console.WriteLine($"Number {max} is the largest");
  44.         }
  45.  
  46.         public static void HowOldAreYouReactionWithCascadedIfElse(int userAge)
  47.         {
  48.             if (userAge >= 65)
  49.             {
  50.                 Console.WriteLine("Enjoy your retirement!");
  51.             }
  52.             else if (userAge >= 21)
  53.             {
  54.                 Console.WriteLine("Fancy an alcoholic beverage?");
  55.             }
  56.             else if (userAge >= 18)
  57.             {
  58.                 Console.WriteLine("You're old enough to drive.");
  59.             }
  60.             else
  61.             {
  62.                 Console.WriteLine("You are too young to drive, drink, or retire.");
  63.             }
  64.         }
  65.  
  66.         public static void WriteInformationAboutDailyDownloadsWithCascadedIfElse(int countOfDailyDownloads)
  67.         {
  68.             if (countOfDailyDownloads <= 0)
  69.             {
  70.                 Console.WriteLine("No downloads.");
  71.             }
  72.             else if (countOfDailyDownloads < 100)
  73.             {
  74.                 Console.WriteLine("Daily downloads: 1-100.");
  75.             }
  76.             else if (countOfDailyDownloads < 1000)
  77.             {
  78.                 Console.WriteLine("Daily downloads: 100-1,000.");
  79.             }
  80.             else if (countOfDailyDownloads < 10000)
  81.             {
  82.                 Console.WriteLine("Daily downloads: 1,000-10,000.");
  83.             }
  84.             else if (countOfDailyDownloads < 100000)
  85.             {
  86.                 Console.WriteLine("Daily downloads: 10,000-100,000.");
  87.             }
  88.             else
  89.             {
  90.                 Console.WriteLine("Daily downloads: 100,000+.");
  91.             }
  92.         }
  93.  
  94.         public static void WriteTheInformationAboutDayWithIfElse(DayOfWeek dayOfWeek)
  95.         {
  96.             if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday)
  97.             {
  98.                 Console.WriteLine("The weekend.");
  99.             }
  100.             else if (dayOfWeek == DayOfWeek.Monday)
  101.             {
  102.                 Console.WriteLine("The first day of the work week.");
  103.             }
  104.             else if (dayOfWeek == DayOfWeek.Friday)
  105.             {
  106.                 Console.WriteLine("The last day of the work week.");
  107.             }
  108.             else
  109.             {
  110.                 Console.WriteLine("The middle of the work week.");
  111.             }
  112.         }
  113.  
  114.         public static void WriteTheInformationAboutDayWithSwitchStatement(DayOfWeek dayOfWeek)
  115.         {
  116.             switch (dayOfWeek)
  117.             {
  118.                 case DayOfWeek.Monday:
  119.                     Console.WriteLine("The first day of the work week.");
  120.                     break;
  121.                 case DayOfWeek.Friday:
  122.                     Console.WriteLine("The last day of the work week.");
  123.                     break;
  124.                 case DayOfWeek.Saturday:
  125.                 case DayOfWeek.Sunday:
  126.                     Console.WriteLine("The weekend.");
  127.                     break;
  128.                 default:
  129.                     Console.WriteLine("The middle of the work week.");
  130.                     break;
  131.             }
  132.         }
  133.  
  134.         public static string GetTypeOfIntegerWithCascadedIfElse(object arg)
  135.         {
  136.             if (arg is sbyte)
  137.             {
  138.                 return $"{arg} is sbyte.";
  139.             }
  140.             else if (arg is byte)
  141.             {
  142.                 return $"{arg} is byte.";
  143.             }
  144.             else if (arg is short)
  145.             {
  146.                 return $"{arg} is short.";
  147.             }
  148.             else if (arg is int)
  149.             {
  150.                 return $"{arg} is int.";
  151.             }
  152.             else if (arg is long)
  153.             {
  154.                 return $"{arg} is long.";
  155.             }
  156.             else if (arg is ushort)
  157.             {
  158.                 return $"{arg} is ushort.";
  159.             }
  160.             else if (arg is uint)
  161.             {
  162.                 return $"{arg} is uint.";
  163.             }
  164.             else if (arg is ulong)
  165.             {
  166.                 return $"{arg} is ulong.";
  167.             }
  168.             else
  169.             {
  170.                 return $"{arg} is not integer.";
  171.             }
  172.         }
  173.  
  174.         public static string GetTypeOfIntegerWithSwitchStatement(object arg)
  175.         {
  176.             switch (arg)
  177.             {
  178.                 case sbyte _:
  179.                     return $"{arg} is sbyte.";
  180.                 case byte _:
  181.                     return $"{arg} is byte.";
  182.                 case short _:
  183.                     return $"{arg} is short.";
  184.                 case int _:
  185.                     return $"{arg} is int.";
  186.                 case long _:
  187.                     return $"{arg} is long.";
  188.                 case ushort _:
  189.                     return $"{arg} is ushort.";
  190.                 case uint _:
  191.                     return $"{arg} is uint.";
  192.                 case ulong _:
  193.                     return $"{arg} is ulong.";
  194.                 default:
  195.                     return $"{arg} is not integer.";
  196.             }
  197.         }
  198.  
  199.         public static string GetTypeOfIntegerWithSwitchExpression(object arg)
  200.         {
  201.             string type = arg switch
  202.             {
  203.                 sbyte _ => "sbyte",
  204.                 byte _ => "byte",
  205.                 short _ => "short",
  206.                 int _ => "int",
  207.                 long _ => "long",
  208.                 ushort _ => "ushort",
  209.                 uint _ => "uint",
  210.                 ulong _ => "ulong",
  211.                 _ => "not integer"
  212.             };
  213.             return $"{arg} is {type}.";
  214.         }
  215.  
  216.         public static void WriteTheInformationAboutSeasonsWithSwitchStatement(Month month)
  217.         {
  218.             switch (month)
  219.             {
  220.                 case Month.December:
  221.                 case Month.January:
  222.                 case Month.February:
  223.                     Console.WriteLine("It's winter now.");
  224.                     break;
  225.                 case Month.March:
  226.                 case Month.April:
  227.                 case Month.May:
  228.                     Console.WriteLine("It's spring now.");
  229.                     break;
  230.                 case Month.June:
  231.                 case Month.July:
  232.                 case Month.August:
  233.                     Console.WriteLine("It's summer now.");
  234.                     break;
  235.                 case Month.September:
  236.                 case Month.October:
  237.                 case Month.November:
  238.                     Console.WriteLine("It's autumn now.");
  239.                     break;
  240.                 default:
  241.                     Console.WriteLine("Sorry, the month was entered incorrectly.");
  242.                     break;
  243.             }
  244.         }
  245.  
  246.         public static byte GetLengthWithCascadedIfElse(int number)
  247.         {
  248.             if (number > 0)
  249.             {
  250.                 number = -number;
  251.             }
  252.  
  253.             if (number <= -1000000000)
  254.             {
  255.                 return 10;
  256.             }
  257.             else if (number <= -100000000)
  258.             {
  259.                 return 9;
  260.             }
  261.             else if (number <= -10000000)
  262.             {
  263.                 return 8;
  264.             }
  265.             else if (number <= -1000000)
  266.             {
  267.                 return 7;
  268.             }
  269.             else if (number <= -100000)
  270.             {
  271.                 return 6;
  272.             }
  273.             else if (number <= -10000)
  274.             {
  275.                 return 5;
  276.             }
  277.             else if (number <= -1000)
  278.             {
  279.                 return 4;
  280.             }
  281.             else if (number <= -100)
  282.             {
  283.                 return 3;
  284.             }
  285.             else if (number <= -10)
  286.             {
  287.                 return 2;
  288.             }
  289.             else
  290.             {
  291.                 return 1;
  292.             }
  293.         }
  294.  
  295.         public static byte GetLengthWithSwitchExpression(int number)
  296.         {
  297.             if (number > 0)
  298.             {
  299.                 number = -number;
  300.             }
  301.  
  302.             return number switch
  303.             {
  304.                 _ when number <= -1000000000 => 10,
  305.                 _ when number <= -100000000 => 9,
  306.                 _ when number <= -10000000 => 8,
  307.                 _ when number <= -1000000 => 7,
  308.                 _ when number <= -100000 => 6,
  309.                 _ when number <= -10000 => 5,
  310.                 _ when number <= -1000 => 4,
  311.                 _ when number <= -100 => 3,
  312.                 _ when number <= -10 => 2,
  313.                 _ => 1,
  314.             };
  315.         }
  316.  
  317.         public static Month? GetMonthWithCascadedIfElse(int month)
  318.         {
  319.             if (month == 1)
  320.             {
  321.                 return Month.January;
  322.             }
  323.             else if (month == 2)
  324.             {
  325.                 return Month.February;
  326.             }
  327.             else if (month == 3)
  328.             {
  329.                 return Month.March;
  330.             }
  331.             else if (month == 4)
  332.             {
  333.                 return Month.April;
  334.             }
  335.             else if (month == 5)
  336.             {
  337.                 return Month.May;
  338.             }
  339.             else if (month == 6)
  340.             {
  341.                 return Month.June;
  342.             }
  343.             else
  344.                     if (month == 7)
  345.             {
  346.                 return Month.July;
  347.             }
  348.             else if (month == 8)
  349.             {
  350.                 return Month.August;
  351.             }
  352.             else if (month == 9)
  353.             {
  354.                 return Month.September;
  355.             }
  356.             else if (month == 10)
  357.             {
  358.                 return Month.October;
  359.             }
  360.             else if (month == 11)
  361.             {
  362.                 return Month.November;
  363.             }
  364.             else if (month == 12)
  365.             {
  366.                 return Month.December;
  367.             }
  368.             else
  369.             {
  370.                 return null;
  371.             }
  372.         }
  373.  
  374.         public static Month? GetMonthWithSwitchStatement(int month)
  375.         {
  376.             switch (month)
  377.             {
  378.                 case 1:
  379.                     return Month.January;
  380.                 case 2:
  381.                     return Month.February;
  382.                 case 3:
  383.                     return Month.March;
  384.                 case 4:
  385.                     return Month.April;
  386.                 case 5:
  387.                     return Month.May;
  388.                 case 6:
  389.                     return Month.June;
  390.                 case 7:
  391.                     return Month.July;
  392.                 case 8:
  393.                     return Month.August;
  394.                 case 9:
  395.                     return Month.September;
  396.                 case 10:
  397.                     return Month.October;
  398.                 case 11:
  399.                     return Month.November;
  400.                 case 12:
  401.                     return Month.December;
  402.                 default:
  403.                     return null;
  404.             }
  405.         }
  406.  
  407.         public static Month? GetMonthWithSwitchExpression(int month)
  408.         {
  409.             return month switch
  410.             {
  411.                 1 => Month.January,
  412.                 2 => Month.February,
  413.                 3 => Month.March,
  414.                 4 => Month.April,
  415.                 5 => Month.May,
  416.                 6 => Month.June,
  417.                 7 => Month.July,
  418.                 8 => Month.August,
  419.                 9 => Month.September,
  420.                 10 => Month.October,
  421.                 11 => Month.November,
  422.                 12 => Month.December,
  423.                 _ => null
  424.             };
  425.         }
  426.     }
  427. }
  428.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement