C#

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

#C

Introduction
 C Sharp is a programming language, first appeared at 2000
(16 years ago).
 One of the most widely used languages around the world
due to its speed, its IDE (integrated development
environment): Microsoft visual studio, and its ease that it
contains many built-in functions which avoids writing
many-many codes.
 Any code you want to write in C# must be in a class
(exception of calling libraries).
?#How to make a project in C
Libraries

Class name

Main function (Program starts from here)


?#How to make functions in C
Notice the type of data the function returns
(num1 + num2)

Type of data which the function will return Function


name

Data parameters (Type of data will be


sent to the function)
?How to debug
Start debugging (F5)

F9

Break point (to determine where to start debugging)


Stop debugging (Shift + F5)

Note: 1. To move line by line (F10)


2. To step Into a function (F11)
3. To jump from break point to another (F5)
After pressing F11, now we can move line by line in the function by F10
Exercises (not assignment)
1. Write a C# program, which contains a function called
“Calculator” which takes two numbers, and displays
their summation, subtraction, multiplication, and
division.
2. Write a C# program, which contains a function called
“Average” which takes two numbers, and displays their
average.
3. Write a C# program, which contains a function called
“PosNeg” which takes a number, then checks if it’s
positive, or negative, then returns “Positive”, or
“Negative”.
4. Write a C# program, which contains a function called
“EvenOdd” which takes a number, then checks if it’s
even, or odd, then returns “Even”, or “Odd”.
5. Write a C# program, which contains a function called
“WordCount” which takes a text contains words
separated by spaces, then counts the numbers of these
words, and returns this number. (Note: you may need to
search)
Loop (For, While)
 We use loops, if we have some code which repeats more than
one time, so instead of re-writing it many time we use loops.

 Ex. If I want to display from 1 to 5 in C#, so


Console.WriteLine(1);
What if we want to display from 1 to 1000 !!
Console.WriteLine(2);
Console.WriteLine(3);
Console.WriteLine(4);
Console.WriteLine(5);
For
 The answer is to use loops.

 For loop: We use it if we know how many times we want to loop.


 Syntax:

 for (start index; condition(end loops if false); (increment/decrement))


 for (int i = 0(start); i < 5(condition); i++(increment by one))
 Ex: for (int i = 1; i <= 1000; i++)
{
Console.Write(i);
}
As we notice function (Console.Write(i)) will be called 1000 times, and in each time
it takes different input.
:Example 1

 Write a C# program using for loop which displays even


numbers from 1 to 100.
Answer: (write this code in the main)

for (int i = 1; i <= 100; i++)


{
if (i % 2 == 0)
WriteConsole.WriteLine(i);
a program displays odd numbers.
}
:Example 2

 Write a C# program using for loop which displays time table


for number 3 from 3*1 to 3*1000
Answer: (write this code in the main)

for (int i = 1; i <= 1000; i++)


{
Console.WriteLine(i * 3);
}
While
 While loop: We use it if we don’t know how many times we want to loop.

 Syntax:
 While(condition(ends loop if false)) Ex:
int i = 0; int i = 1;
while (i < 5 (Condition)) while (i <= 1000)
{ {
i++; Console.Write(i);
} i++;
}

As we notice function (Console.Write(i)) will be called 1000 times, and in each time it
takes different input.
:Example 1

 Write a C# program using while loop which displays even


numbers from 1 to 100.
Answer: (write this code in the main)

int i = 1;
while (i <= 100)
{
if (i % 2 == 0)
Write a program displays odd numbers.
Console.WriteLine(i);
i++;
}
:Example 2

 Write a C# program using while loop which displays time table


for number 3 from 3*1 to 3*1000
Answer: (write this code in the main)

int i = 1;
while (i <= 1000)
{
Console.WriteLine(i * 3);
i++;
}
:Example 3
 Write a C# program using which calculates the numbers of
years that a city will take to move from an initial population to
another population with a constant percentage.
As3we
Takes mentioned before
inputs: we don’t
1. Initial know how many
population
times we need to loop to calculate the years, so the
2. Desired population
suitable loop is While()
3. Percentage

 Choose the suitable loop, and return #years.


Answer: (write this code in the main)
Console.Write("Please enter the initial population: ");
int InitPop = int.Parse(Console.ReadLine());
Console.Write("Please enter the desired population: ");
int DesirPop = int.Parse(Console.ReadLine());
Console.Write("Please enter the constant percentage: ");
double Percentage = double.Parse(Console.ReadLine());

int years = 0;

while (InitPop < DesirPop)


{
years++;
InitPop = InitPop + ((int)(InitPop * Percentage) / 100);
}

Console.WriteLine("The city needs " + years + " years");


Array
 Unlike regular variables, arrays can hold multiple values.
 Same name and type (int, char, etc.)
 Static entity (same size throughout program)

int int int double double double


Syntax

 Data Type [] Array name = new Data type [size];

 So, in[] this


Ex. string Namecase thestring
= new last position
[5]; is in index…?
 It means that we have reserved 5 positions in the memory each
Note: one is athe
To get string.
length of an Array we use ArrayName.Length
Ex. Name. Length
 Note: 1st position in the array is in index 0
:Example 1

 Write a C# program using array which allows user to enter 5


names then displays them.
Answer: (write this code in the main)

string[] name = new string[5];


Console.WriteLine("Please enter the names:");
for (int i = 0; i < 5; i++)
{
Console.Write((i + 1) + ") ");
name[i] = Console.ReadLine();
}

Console.WriteLine("-------------------------------------------------------");

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


{
Console.WriteLine("Name #" + (i + 1) + " is " + name[i]);
}
:Example 2

 Write a C# program using array which allows user to enter 7


double numbers then displays their square.
Answer: (write this code in the main)

double[] Number = new double[7];


Console.WriteLine("Please enter the numbers:");
Note: As we noticed the only way to write a
for (int i = 0; i < 7; i++)
dynamic
{ code using array (which allows all
possible sizes)
Console.Write((i + 1) +is to reserve a lot of positions in
") ");

memory.
Number[i] = double.Parse(Console.ReadLine());
}
ex. string [] Name = new string [10000000];
What is the solution!!
Console.WriteLine("__________________________________________________");

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


{
Console.WriteLine("Square of #" + (i + 1) + " is " + Number[i] * Number[i]);
}
List
 List is the same as the array, but the only difference that List is
dynamic, that you can extend its size as long as you want.

 Syntax:
Note: List<Data
To get the length of a List we use LstName.Count
type> List name = new List<Data type> ();
Ex. Name.Count
Ex. List<string> name = new List<string>();

 Now we will solve the same previous exa.Cmples, but using


List.
Answer 1: (write this code in the main)

List<string> name = new List<string>();


Console.WriteLine("Please enter the names:");
for (int i = 0; i < 5; i++)
{
Console.Write((i + 1) + ") ");
name.Add(Console.ReadLine());
}

Console.WriteLine("-------------------------------------------------------");

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


{
Console.WriteLine("Name #" + (i + 1) + " is " + name[i]);
}
Answer 2: (write this code in the main)

List<double> Number = new List<double>();


Console.WriteLine("Please enter the numbers:");
for (int i = 0; i < 7; i++)
{
Console.Write((i + 1) + ") ");
Number.Add(double.Parse(Console.ReadLine()));
}

Console.WriteLine("__________________________________________________");

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


{
Console.WriteLine("Square of #" + (i + 1) + " is " + Number[i] * Number[i]);
}
Nested loops
 Syntax:
 for (start index; condition(end loops if false); (increment/decrement))
{
for (start index; condition(end loops if false); (increment/decrement))
{
}
}

 The idea is that the outer loop for rows, and the inner loop is for columns, you
will find all examples attached in a folder.
Thanks

You might also like