CSC 221 Lecture 3new - Part1
CSC 221 Lecture 3new - Part1
LECTURE 3
LECTURE OUTLINE
while loops are very similar to do loops, but they have one important
difference:
The Boolean test in a while loop takes place at the start of the loop cycle, not the end.
If the test evaluates to false, then the loop cycle is never executed.
Instead, program execution jumps straight to the code following the loop.
while Loops
The Syntax
while ( < Test > )
{
< code to be looped >
}
for Loops
for loop executes a set number of times and maintains its own counter.
To define a for loop you need the following information:
A starting value to initialize the counter variable .
A condition for continuing the loop, involving the counter variable .
An operation to perform on the counter variable at the end of each loop cycle .
For example, if you want a loop with a counter that increments from 1 to
10 in steps of one, then the
starting value is 1;
the condition is that the counter is less than or equal to 10;
and the operation to perform at the end of each cycle is to add 1 to the counter.
This information must be placed into the structure of a for loop as
follows:
for ( < initialization > ; < condition > ; < operation > )
{
< code to loop >
}
int i;
for (i = 1; i < = 10; ++i)
{
Console.WriteLine(“{0}”, i);
}
Interrupting Loops
The break command simply exits the loop, and execution continues at the first line
of code after the loop, as shown in the following example:
int i = 1; This code writes out the
while (i < = 10) numbers from 1 to 5 because
the break command causes
{ the loop to exit when
if (i == 6) // when this is true stop the loop i reaches 6.
break;
Console.WriteLine(“{0}”, i++);
}
Continue
Continue only stops the current cycle, not the whole loop, as shown here:
int i; whenever the remainder of i
for (i = 1; i < = 10; i++) divided by 2 is zero, the continue
statement
{ stops the execution of the current
if ((i % 2) == 0) cycle, and continue with others so
only the numbers 1 , 3 , 5 , 7 , and
continue; 9 are displayed.
Console.WriteLine(i);
}
Goto
The third method of interrupting a loop is to use goto , as shown earlier:
int i = 1;
while (i < = 10)
{
if (i == 6)
goto exitPoint;
Console.WriteLine(“{0}”, i++);
}
Console.WriteLine(“This code will never be reached.”);
exitPoint:
Console.WriteLine(“This code is run when the loop is exited using goto.”);
Exercise (Class Activity) (15mins)
Write and run a program that reads a positive integer n and then
reads n more integers and prints their sum. Use a do. . . while loop.
Write and run a program that reads a positive integer n and then
reads n more integers and prints their sum. Use a for loop.
Type conversion:
Implicit conversion
Explicit conversion
Implicit conversion
ushort destinationVar;
char sourceVar = ‘a’;
destinationVar = sourceVar;
Console.WriteLine(“sourceVar val: {0}”, sourceVar);
Console.WriteLine(“destinationVar val: {0}”,
destinationVar);
ASCII Table
Implicit conversion
You cannot implicitly ask the compiler to convert a value from one data
type to another if they are not compatible.
byte destinationVar;
short sourceVar = 7;
destinationVar = sourceVar;
Console.WriteLine(“sourceVar val: {0}”, sourceVar);
Console.WriteLine(“destinationVar val: {0}”, destinationVar);