October 16, 2020
Execute (a variation of) the code we just reviewed to make sure you understand the mechanism of the increment and decrement operators.
int a = 0, b = 0;
Console.WriteLine("Before changing their values:");
Console.WriteLine($"\ta is {a}\n\tb is {b}\n-----------");
Console.WriteLine("Incrementing, using postfix and prefix operators:");
a++;
++b;
Console.WriteLine($"\ta is {a}\n\tb is {b}\n-----------");
Console.WriteLine("Decrementing, using postfix and prefix operators:");
a--;
--b;
Console.WriteLine($"\ta is {a}\n\tb is {b}\n-----------");
Console.WriteLine("When combining decrementing and incrementing operators with other operations,\nit makes a difference to use postfix or prefix operators!");
int c = a--, d = ++b;
Console.WriteLine($"\ta is {a} (the decrementing took place as expected)\n\tb is {b} (the incrementing took place as expected)\n\tc is {c}" +
$" (c got its value *before* a was decremented)\n\td is {d} (d got its value *after* b was incremented)\n-----------");Write a while loop that displays the integers between 1 and 100 at the screen, with a space between them.
Write a while loop that displays the "*" character 100 times at the screen.
Modify your previous loop, so that a new line character is displayed on the screen every time 10 "*" has been displayed on the screen. That is, your program should display on the screen:
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
Consider the following code:
Console.WriteLine("Please enter a positive number");
int n = int.Parse(Console.ReadLine());
while (n < 0)
{
Console.WriteLine($"You entered {n}, I asked you for a positive number. Please try again.");
n = int.Parse(Console.ReadLine());
}Main method, compile it and execute it.Adapt the code above to perform string validation: ask the user to enter a string, and as long as the user does not enter “Yes” or “No”, ask him/her again to enter a value.
while loop asking the user for an integer, and looping as long as that integer is stricly greater than 0.while loop so that the program displays the sum of all the numbers entered when exiting the while loop.An example of execution could be:
Enter a positive number to sum, or a negative number to exit
12
Enter a positive number to sum, or a negative number to exit
3
Enter a positive number to sum, or a negative number to exit
-2
Your total is 15.Identify what type of loop it is: is it user-controlled, counter-controlled, or sentinel-controlled? Does it use a counter, a sentinel value, or an accumulator?
Consider the code we just studied:
Console.WriteLine("Please, enter an integer.");
string message = Console.ReadLine();
int a;
bool res = int.TryParse(message, out a);
if (res)
{
Console.WriteLine($"The value entered was an integer: {a}.");
}
else
{
Console.WriteLine("The value entered was not an integer, so 0 is assigned to a.");
}
Console.WriteLine(a);What happen if:
TryParse method in other classes as well: there is for instance a Double.TryParse and a Decimal.TryParse method. Write a small program that uses one of them.