0% found this document useful (0 votes)
7 views

Topic 03 - Basic Concepts

The document explains data types and variables in C#, including common types like int, double, char, bool, string, and var, along with examples of their usage. It also covers control flow with conditional statements (if-else, switch) and loops (for, while, foreach), providing examples for each. Additionally, it includes exercises for practicing these concepts through user input and logic implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Topic 03 - Basic Concepts

The document explains data types and variables in C#, including common types like int, double, char, bool, string, and var, along with examples of their usage. It also covers control flow with conditional statements (if-else, switch) and loops (for, while, foreach), providing examples for each. Additionally, it includes exercises for practicing these concepts through user input and logic implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Types and Variables

In C#, variables are used to store data, and data types define the kind of data a variable can
hold.
Common Data Types:

● int: Stores integer values (e.g., 10, -100).


● double: Stores floating-point numbers (e.g., 3.14, -0.99).
● char: Stores a single character (e.g., 'A').
● bool: Stores boolean values (true or false).
● string: Stores text (e.g., "Hello World").
● var: Allows the compiler to infer the type based on the assigned value.

var number = 10; // The compiler infers that "number" is of type int

var name = "John"; // The compiler infers that "name" is of type string

var isActive = true; // The compiler infers that "isActive" is of type bool

In these examples:

● number is inferred to be of type int because 10 is an integer.


● name is inferred to be of type string because "John" is a string.
● isActive is inferred to be of type bool because true is a boolean value.

Example:
int age = 25;
double price = 19.99;
char grade = 'A';
bool isStudent = true;
string name = "Alice";

Avoiding conflicts
To use an identifier that clashes with a reserved keyword, we can
do so by qualifying it with the @ prefix. For instance:
class class {...} // Illegal
class @class {...} // Legal
Control Flow (Conditional Statements)
Control flow allows the program to make decisions and execute different code paths based on
conditions.
Common Statements:

● if-else: Executes a block of code based on a condition.


o Nested if else
o Else if ladder
● switch: Selects one of many code blocks to be executed.

Example:
int score = 85;
if (score >= 90)
{
Console.WriteLine("Grade: A");
}else if (score >= 80)
{
Console.WriteLine("Grade: B");
}else
{
Console.WriteLine("Grade: C");
}
switch (score)
{
case 90:
Console.WriteLine("Excellent!");
break;
case 80:
Console.WriteLine("Good job!");
break;
default:
Console.WriteLine("Keep trying!");
break;
}
Loops
Loops in C# are used to repeat a block of code multiple times.
Common Loop Types:

● for loop: Executes code a specific number of times.


● while loop: Executes code as long as a condition is true.
● foreach loop: A foreach loop in C# is used to iterate over a collection or array. It simplifies looping by
directly accessing each element in a collection without needing an index.

Example:

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


{
Console.WriteLine("Iteration: " + i);
}
int count = 3;while (count > 0)
{
Console.WriteLine("Count: " + count);
count--;
}
Foreach Loop Example

class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };

// Using foreach to iterate over the array


foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
OUTPUT
12345

Exercise:

1. Write a C# program that prompts the user to enter a student's score (an integer
between 0 and 100). Based on the score, the program should determine and display
the corresponding letter grade according to the following criteria:

 A: 90 - 100
 B: 80 - 89
 C: 70 - 79
 D: 60 - 69
 F: Below 60

2. Write a C# program that prompts the user to enter an integer. The program should
determine whether the number is even or odd and display the appropriate message.
3. Write a C# program that prompts the user to enter their age. The program should
classify the user into one of the following age groups and display the appropriate
message:

 Child: 0 - 12 years
 Teenager: 13 - 19 years
 Adult: 20 - 64 years
 Senior: 65 years and above

4. Write a C# program that prompts the user to enter a positive integer N. The
program should find and display all prime numbers less than or equal to N.
5. Write a C# program that prompts the user to enter a positive integer N. The
program should calculate and display the factorial of N.
6. Palindrome number, Neon number, Duck number, Armstrong number, Perfect
Number, Spy Number.

You might also like