C# Lesson 4
C# Lesson 4
Jalil Verdiyev
TOPICS
• Loop control statements: continue,
break, (goto), (return)
• Functions(aka methods)
2
LOOP CONTROL
STATEMENTS
Sometimes we may want to control the loops that we are using.
There comes very handy statements
• break
• continue
• goto
• return
BREAK STATEMENT
Break statement, as the name suggests is used to break loops
or switch cases. It breaks the nearest statement or loop.
But as you can see using if, like this way can sometimes lead
to unintended code bugs. Or will make our code hard to read.
Instead, we can use second approach
SECOND WAY
myScope:
{
// You can write any code here
}
And after declaring we can use goto statement to
jump that scope:
myScope:
{
Console.WriteLine("Hello from myScope");
}
goto myScope;
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
and so on.. Yes, we may instead use loops which we passed earlier but what if
Console.WriteLine($"I
the code block itself is too big and weam
wantprinting
to simplify our5code?
+ 6: {5comes
There +
6}");
functions
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
DECLARING
FUNCTIONS(METHODS)
For declaring functions, the syntax is: returnType
functionName(parameters).
You may ask what is return type? In programming at least
every function must return something. And they shouldn't. You
may think it as you ask someone to do something, and if you
ask them to tell something to someone, they doesn't return
anything to you, but if you ask them to buy something for you,
they go to market and buy the thing then gives(returns) to you.
In programming functions also have two types:
void PrintSum()
{
// here do some code
Console.WriteLine($"I am printing 5 + 6: {5 +
6}");
}
As you may see there is no return keyword in function body. They
are useful when you need to just call them and wait them to finish the
job.
TYPE RETURNING
FUNCTIONS
int Return1()
{
Console.WriteLine("Returning oneee!");
return 1;
}
If you want to return string or other data type, you can change
the int at the declaration.
USING OR CALLING
FUNCTIONS(METHO
DS)
Functions don't work unless you call them. For calling the
function here is an example:
// Declaring
void PrintSum()
{
Console.WriteLine($"I am printing 5 + 6: {5 + 6}");
}
// Calling
PrintSum();
You may use functions in anywhere you
want, in loops, in other functions or so on.
Let's have a look at an example of calling
type returning functions:
PARAMETERIZED
FUNCTIONS(METHOD
S)
While working with functions we will need to get some values
from outside. There comes parameters. You can define as much
as parameters you want in function. Here is an example, which
takes two parameters and returns the sum of them:
There is only one rule for declaring default parameters, they always
must be declared at the end of parameter list. But you can as much as
default parameters you want. Meaning:
double sum = 0;
foreach (var item in doubles)
{
sum += item;
}
This is quite good also as our function becomes much dynamic. However, there is a small issue,
while calling user needs to pass an array even, he or she wants to get average of two. What if there
was a way to combine these two ways? For this purpose, we have *params* keyword. Here is an
example:
// We just added params keyword
double Average(params double[] doubles)
{
if (doubles.Length == 0)
return 0;
double sum = 0;
foreach (var item in doubles)
{
sum += item;
}