C# BASICS
Eng: Shimaa Gamal
AGENDA
C# Basics
Condition Statements
Looping
Arrays
2/7/20XX 2
INTRODUCTION
High Level programming
TO C# language from Microsoft
Runs on .NET Framework / .NET
Core
Simple yet powerful
Used for:
• Desktop apps
• Web apps
• Mobile apps
• Game development (Unity)
3
using System;
BASIC STRUCTURE
OF A C# PROGRAM namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello,
World!");
}
}
}
4
EXPLANATION:
•using System; → imports basic functionality
•namespace → group of related classes
•class Program → defines a class
•Main → program entry point
5
VARIABLES AND
DATA TYPES
int age = 25;
double salary = 3000.50;
char grade = 'A';
string name = "Ali";
bool isActive = true;
6
EXERCISE:
Declare:
•A variable for your name
•A variable for your age
•A variable for your grade (e.g. ‘B’)
•Print them using Console.WriteLine
7
CONDITION int age = 20;
STATEMENTS (IF –
ELSE) if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Child");
}
8
EXERCISE:
Write a program that checks if a number
is positive or negative.
9
int score = 75;
CONDITION
STATEMENTS if (score >= 90)
{
(ELSE IF) Console.WriteLine("Excellent");
}
else if (score >= 60)
{
Console.WriteLine("Good");
}
else
{
Console.WriteLine("Try Again");
}
10
EXERCISE:
Write a program that prints:
“High” if the number > 100
“Medium” if between 50 and 100
“Low” if less than 50
11
int day = 3;
SWITCH
STATEMENT
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
12
EXERCISE:
Write a program that prints the name
of the month based on a number (1-12).
13
LOOPING (FOR)
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
14
EXERCISE:
Print numbers from 10 to 1 (reverse).
15
LOOPING
(WHILE) int i = 1;
while (i <= 5)
{
Console.WriteLine(i);
i++;
}
16
EXERCISE:
Print even numbers from 2 to 10
using a while loop.
17
LOOPING (DO-
WHILE) int i = 1;
do
{
Console.WriteLine(i);
i++;
} while (i <= 5);
18
EXERCISE:
Write a program to print numbers from
1 to 8 using a do-while loop.
19
LOOPING string[] names = { "Ali", "Mona",
(FOREACH) "Sara" };
foreach (string name in names)
{
Console.WriteLine(name);
}
20
EXERCISE:
Create an array of 3 fruits and print
each fruit using foreach
21
ARRAYS •Used to store multiple
values of the same type
•Indexes start from 0
int[] numbers = { 10, 20, 30, 40
};
Console.WriteLine(numbers[0]);
// prints 10
string[] names = { "Ali", "Mona",
"Sara" };
22
EXERCISE:
Create an array of 4 student ages and
print the second element.
23
LOOPING
THROUGH
ARRAYS int[] nums = { 5, 10, 15 };
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
24
EXERCISE:
Create an array of 5 names and print
them all using a for loop.
25
MULTI-
int[,] matrix = {
DIMENSIONA
{1, 2, 3},
L ARRAYS
{4, 5, 6}
};
Console.WriteLine(matrix[1,2]); //
prints 6
26
EXERCISE:
Create a 2×2 matrix and print all values.
27
ASSIGNMENT 1: Ask the user to enter a grade (0-100),
GRADING SYSTEM and based on the value, print:
90–100 → “Excellent”
80–89 → “Very Good”
60–79 → “Good”
Less than 60 → “Fail”
Bonus: Add input validation if
grade > 100 or < 0 → print "Invalid grade".
28
ASSIGNMENT 2:
SIMPLE
CALCULATOR Write a program that takes:
•Two numbers (from the user)
•An operator (+, -, *, /) using switch
Then performs the operation and
prints the result
29
ASSIGNMENT 3 :Ask the user to enter a number, then
MULTIPLICATIONprint its multiplication table from 1 to
TABLE 10 using a for loop.
e.g.
5x1=5
5 x 2 = 10
...
5 x 10 = 50
30
ASSIGNMENT 4 :
NAME LIST
Create an array of 5 names (strings),
then print all names using foreach loop
31
ASSIGNMENT 5 :
STUDENT MARKS
•Ask the user to enter 5 student
marks (store in an array)
•Then calculate and print:
•Total
•Average
•Highest mark
32
ASSIGNMENT 6 :
COUNT EVEN AND
ODD
•Ask the user to enter 10 numbers
•Store them in an array
•Then count how many are even and how many are odd,
and print the result
33
THANK YOU
34