c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.233 seguidores
Pré-visualização50 páginas
the condition is first 
tested after the block is executed. The do loop is not used as often as the other loops.
switch
while, for and do are control structures for loops or iterations, while if is a control structure for branching 
or selection. In most cases, if is used for selections, but there is an alternative, called switch. It is best 
explained by an example.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
48 
Program control
Exam13
Weekday
The task is to write a program where the user must enter an integer. If the number is 1, 2, 3, 4, or 5, the 
program will print the name of the corresponding day of the week, if the number is 6 or 7, the program 
will print the text Weekend, and otherwise the program will print an error message. Below is an example 
of a running program:
How to
The program can be written as follows, where in Man() the user must enter the number for the day of 
the week:
static void Main(string[] args)
{
Console.Write("Enter the day number in week: ");
string text = Console.ReadLine();
switch (Convert.ToInt32(text))
{
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
49 
Program control
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
case 7:
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Illegal day...");
break;
}
}
Explanation
After the word switch is an expression:
switch(Convert.ToInt32(text))
The construction also has a number of case entries where the case is a constant followed by a colon. The 
constant must have the same type as the expression:
case 1:
When the statement is executed, the expression is evaluated, and the control is transferred to the case 
entry that is identical to the value of the expression. The statements after the case entry are then executed, 
until you meet a break-statement, a return statement or the switch statement ends. If there is no case entry, 
which corresponds to the value of the expression after switch, there are two possibilities. Has the switch 
statement a default entry, the control is transferred to that entry. Otherwise the entire switch statement 
is skipped. Note that a switch does not need to have a default entry.
In the above if the input is 6 or 7, the program in both cases the print
Weekend
If, for example the user enters 6, the program will continue with the statement after the case 7, as there 
is no break after the case entry for 6.
A switch statement is kind of multi-branching, and it can sometimes be a reasonable solution, but is not 
as often used as the other control statements.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
50 
Program control
Exam14
The cross-sum
To conclude the use of control statements from this section, I will show two additional programs.
The first program calculates the cross-sum of a number, the user enters. If the user enters 123456 the 
cross-sum is 3:
1 + 2 + 3 + 4 + 5 + 6 = 21
2 + 1 = 3
The program will show how one can have two while-statements inside each other, but is also an example 
of an algorithm that are bit harder to solve than other algorithms shown so far.
How to
Solving the task can be decomposed into three sub problems:
\u2022	 Enter the number
\u2022	 Determine the cross-sum
\u2022	 Print the result
Here the solution of the first and last problem are what I have done previously, and are simple problems, 
and the task is reduced to determine the cross-sum of a number. This task can be described as follows:
\u2022	 determine the sum of the number\u2019s digits
\u2022	 repeat until the sum is less than 10
A little more formally the task can be formulated as an algorithm in the following manner:
while number > 9 do
{
let sum = sum of the numbers digits
number = sum
}
number is now the cross-sum
The problem can be solved with a loop, but yet it is not entirely clear how to determine the sum of the 
digits of a number. This can be done with a second loop:
while number > 0 do
{
add number % 10 to sum
divide number by 10
}
where the first operation in the loop adds the last digit of the number to the sum, while the second 
operation throws the last digit off.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
51 
Program control
With these considerations in place, you can write a method that determines and returns the cross-sum 
of a number:
static uint DigitSum(ulong n)
{
while (n > 9)
{
ulong s = 0;
while (n > 0)
{
s += n % 10;
n /= 10;
}
n = s;
}
return (uint)n;
}
The program itself can now be written as, where the method Enter() is as before:
static void Main(string[] args)
{
ulong number = Enter();
Console.WriteLine("The reduced sum of digits in {0} is {1}",
number, DigitSum(number));
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
52 
Program control
If you run the program the result could be:
Explanation
The inner loop determines the sum of the numbers digits. Note the operator % which is the modulus 
operator. For example means
number % 10
residue by dividing by 10, which is the rear digit of the number. Note also the result of
number /= 10;
where the division by 10 (integer division) \u201cthrow\u201d the last digit off. The outer loop is repeated as long 
as the sum of digits is greater than 9 \u2013 it is more than one digit.
Note also the data types. The parameter to the method DigitSum() has the type ulong. I chose a long to 
work with large numbers, and here again an ulong, because the number should not be negative. Similarly, 
the method returns an uint, which is a non-negative integer. Unsigneds are used less often, and I\u2019ve mostly 
used them here to show how they are used. n is an ulong, and the method returns an uint. In the return 
statement is thus a need to put a large number (an ulong) into a smaller (an uint). You can\u2019t just do that 
without telling the compiler that you mean the business. You do that with a type cast:
return (uint)n;
which tells the compiler that it should convert n to an uint.
Exam15
The biggest and the smallest number
This example is a program that uses the control structures while and if. if is the most frequently used 
control statement at general, and in practice, one can\u2019t imagine a program that does not apply if. In the 
program you must enter an arbitrary number of numbers and the method then prints
\u2022	 number of entered numbers
\u2022	 the sum of numbers entered
\u2022	 the smallest of the numbers entered
\u2022	 the largest of the numbers entered
\u2022	 the average of the numbers entered
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
53 
Program control
An example of an execution of the program might be:
How to
The program can be written as follows:
namespace Exam15
{
class Program
{
static void Main(string[] args)
{
double sum = 0;
int count = 0;
double min = double.MaxValue;
double max = double.MinValue;
double number;
while ((number = Enter()) != 0)
{
sum += number;
++count;
if (min > number) min = number;
if (max < number) max = number;
}
Print(count, sum, min, max);
}
static double Enter()
{
Console.Write(&quot;Enter a number (enter 0 to stop): &quot;);
string line = Console.ReadLine();
return Convert.ToDouble(line);
}
static void Print(int count, double sum, double min, double max)
{
if (count > 0)
{
Console.WriteLine(&quot;Number