0% found this document useful (0 votes)
17 views22 pages

C# Lesson 4

The document discusses various C# programming concepts including loop control statements, functions, parameters, and the params keyword. It provides examples of using break, continue, goto, and return statements to control loops. It demonstrates how to declare, call, and pass parameters to functions. It also shows how to set default parameter values and how the params keyword allows functions to accept a variable number of arguments.

Uploaded by

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

C# Lesson 4

The document discusses various C# programming concepts including loop control statements, functions, parameters, and the params keyword. It provides examples of using break, continue, goto, and return statements to control loops. It demonstrates how to declare, call, and pass parameters to functions. It also shows how to set default parameter values and how the params keyword allows functions to accept a variable number of arguments.

Uploaded by

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

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.

for (int i = 0; i < 10; i++)


{
if (i == 3)
break;
Console.WriteLine($"The number is
{i}");
}
As you can see after 2, there will be no output.
If you have switch statement in your loop and break in
cases, the loop won't be broken

for (int i = 0; i < 10; i++)


{
switch (i % 2)
{
case 0:
Console.WriteLine($"{i} is even");
break;
case 1:
Console.WriteLine($"{i} is odd");
break;
default:
Console.WriteLine("Not a
number ;/");
break;
}
}
CONTINUE STATEMENT

Sometimes we want to do nothing at specific part or


parts of our loops. For example while printing
numbers from 1-12 for some reason, we want to not
print 3. We may achieve it by 2 ways
FIRST WAY

for (int i = 0; i < 12; i++)


{
if (i != 3)
{
Console.WriteLine($"{i}");
}
}

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

for (int i = 0; i < 12; i++)


{
if (i == 3)
continue;
Console.WriteLine($"{i}");
}

This makes our code more readable + maintainable.


GOTO STATEMENT
Goto statements helps to change flow of our programs to
elsewhere. And it uses scopes to achieve this. Scopes are just
some named code blocks like in example:

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;

This will lead to infinite calling. When it reaches


myScope prints “Hello from myScope”, then it comes goto
and goto jumps the flow to myScope, this makes our
program to run endless. By this way you can build a loop
with gotos and scopes but usually this doesn't preferred
much.
RETURN
STATEMENT

Kind of works like break statement


but only works with functions(which
we will pass) in loops you can also
use return, but it will directly end the
function that the loop in. We will
deep dive in functions;).
FUNCTIONS(OR
METHODS)
Functions help us to reduce code repetitions. Let's say in our
program we print some sentence and we do it ~20 times. Let's
look an example:

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 functions(don't return anything)

• Type returning functions


VOID FUNCTIONS
Void functions don't return anything they just do something
and finishes their job, like we said in earlier example. The best
example for void functions is Console.WriteLine(), it just
prints something to Console then disappears. Let's look how to
declare them:

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

Type returning functions, as the name suggests must return


something(or let's say the declared type) at the end. They are
useful for calculations. The best example for type returning
functions is Console.ReadLine() it gets the user's input and
returns as a string to you. Let's look some examples:

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:

int Sum(int num1, int num2)


{
return num1 + num2;
}
// Calling and getting the returned value
int sum = Sum(3, 4);
// Printing the value
Console.WriteLine(sum);

Parameter types can be any type.


void PrintName(string name = "Jalil")
Sometimes while building our functions, we may need to some {
default values, in other words we would need some optional
parameters, that they aren't much important:
Console.WriteLine(name);
}

// Will print Jalil


PrintName();

// Will print Akbar


PrintName("Akbar");

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:

// Will generate error


void PrintNames(string name = "Jalil", string surName, double grade =
90.2)
{
Console.WriteLine("Name is : {0}, Surname is : {1}", name, surName);
}
// Fine, there won't be errors
void PrintNames(string surName, string name = "Jalil", double grade =
90.2)
{
Console.WriteLine("Name is : {0}, Surname is : {1}", name, surName);
}
PARAMS KEYWORD
Sometimes size of parameters can be different. For example, if
we are writing some function to calculate average of numbers,
then we have 2 ways. First:

double AverageOfTwo(double d1, double d2)


{
return (d1 + d2) / 2;
}
double AverageOfThree(double d1, double d2, double d3)
{
return (d1 + d2 + d3) / 3;
}
// .. and so on
This is first approach but what if we need to find hundreds of
numbers' average. Then we can look at second approach:
double Average(double[] doubles)
{
if (doubles.Length == 0)
return 0;

double sum = 0;
foreach (var item in doubles)
{
sum += item;
}

return sum / doubles.Length;


}

double av = Average(new double[] { 1, 2, 3, 5, 23.3,


32 });

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;
}

return sum / doubles.Length;


}
// Both ways are correct
Console.WriteLine(AverageOfTwo(1, 2));
Console.WriteLine(Average(new double[] { 2, 4, 56, 80, 2 }));

Now as you see, while calling we can use both ways.

You might also like