0% found this document useful (0 votes)
3 views30 pages

CSC 221 Lecture 3new - Part1

This document outlines Lecture 3 of CSC 221, focusing on control flow in C# programming, including looping methods (do, while, for loops), interrupting loops, and type conversion. It provides examples and exercises related to these topics, emphasizing the structure and execution of different loop types. Additionally, it covers implicit and explicit type conversions in C#.

Uploaded by

PROF D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views30 pages

CSC 221 Lecture 3new - Part1

This document outlines Lecture 3 of CSC 221, focusing on control flow in C# programming, including looping methods (do, while, for loops), interrupting loops, and type conversion. It provides examples and exercises related to these topics, emphasizing the structure and execution of different loop types. Additionally, it covers implicit and explicit type conversions in C#.

Uploaded by

PROF D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CSC 221

Computer Programming II (C# Programming


Language)
2020/2021 Session

LECTURE 3
LECTURE OUTLINE

 How to control the execution of your code.(part2)


 Looping Methods
 Type conversion
 Complex Variable Types
 Debugging and Error Handing
Looping:
do Loops
while Loops
for Loops
Interrupting Loops
do Loops
 The structure of a do loop is as follows, where < Test >
evaluates to a Boolean value:
do
{
< code to be looped >
} while ( < Test > );

 The semicolon after the while statement is required.


Examples
 For example, you could use the following to write the numbers from 1 to
10 in a column:
static void Main()
{
int i = 1;
do
{
Console.WriteLine("{0}", i++);
}while (i<=10);
}
 You can use the following to sum number 1 to 10: σ𝑥=10
𝑥=1 𝑥

// this will find the sum of 1-10 as 55


Console.WriteLine("this will find the sum of 1-10 as 55");
Console.WriteLine();
i = 1;
sum = 0;
do
{
sum = sum + i; // this is the same as sum += i ;
++i; // the same as i = i+1;
} while (i <= 10);
Console.WriteLine(“The Sum of 1-10 is {0}", sum); Console.WriteLine();
// to sum 1 to 10 in a column as 1 3 6 ....55
int i = 1;
int sum = 0;
do
{
Console.WriteLine("{0}", sum +=i++); // this will use i before increment
Console.WriteLine();
} while (i <= 10);
Exercise 1:

1. Study the code on slide 11 and explain the execution process.


2. With the following inputs, what will be the outputs?
 balance = 200
 interestRate = 40
 targetBalance = 500
while Loops

 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

 break — Causes the loop to end immediately .


 continue — Causes the current loop cycle to end immediately
(execution continues with the next loop cycle) .
 goto — Allows jumping out of a loop to a labeled position (not
recommended if you want your code to be easy to read and understand) .
 return — Jumps out of the loop and its containing function (this will be
treated under function in the next chapter )
Break

 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

bool and string


have no implicit
conversions, but
the numeric types
have a few.
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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy