FUNDAMENTALS OF
PROGRAMING
Next Topic
Array
Loops
String concatenation
Case Sensitives
Casting ( Conversion )
* Scopes
FUNDAMENTALS OF
PROGRAMING
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type with square brackets []
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value. To declare an array, define the variable
// declare an array
int[] age;
// allocate memory for array
age = new int[5];
C#, initialize an array during the declaration. Like
int [] numbers = {1, 2, 3, 4, 5};
FUNDAMENTALS OF
PROGRAMING
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type with square brackets []
string[] cars;
string[] cars = {“mehran", “alto", “Toyota", “city"};
int[] myNum = {10, 20, 30, 40};
string[] cars = {" mehran", "mehran", " Toyota", "city"};
Console.WriteLine(cars[0]);
FUNDAMENTALS OF
PROGRAMING
// Create an array of four elements, and add values later
string[] cars = new string[4];
// Create an array of four elements and add values right away
string[] cars = new string[4] {"Volvo", "BMW", "Ford",
"Mazda"};
// Create an array of four elements without specifying the size
string[] cars = new string[] {"Volvo", "BMW", "Ford",
"Mazda"};
// Create an array of four elements, omitting the new keyword,
and without specifying the size
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
FUNDAMENTALS OF
PROGRAMING
Loops
1.For
2.Foreach
3.While
4.Do While
FUNDAMENTALS OF
PROGRAMING
for (initialization; condition; iterator)
{
// body of for loop
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
FUNDAMENTALS OF
PROGRAMING
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
FUNDAMENTALS OF
PROGRAMING
public static void Main(string[] args)
{
for (int i=1 ; i=>0; i++)
{
Console.WriteLine("C# For Loop:
Iteration {0}", i);
}
}
FUNDAMENTALS OF
PROGRAMING
Make an array of integer type takes 5 values
Make an array of string type takes 3 values
Print integer array value
Print string array value
FUNDAMENTALS OF
PROGRAMING
String concatenation
// Simple string concatenation Console.WriteLine("Hello" + " " + “World " +
"!"
// Declare strings
string firstName = “qasim";
string lastName = "Charana";
// Concatenate two string variables
string name = firstName + " " + lastName;
Console.WriteLine(name);
FUNDAMENTALS OF
PROGRAMING
String functions
string originalString = "HELLO WORLD";
Console.WriteLine(originalString.ToLower()); // Output: "hello world“
Console.WriteLine(originalString.ToUpper()); // Output: “HELLO WORLD"
FUNDAMENTALS OF
PROGRAMING
1. Implicit Type Conversion in C#
2. Explicit Type Conversion
3. Type Conversion using Parse()
4. Conversion using Convert Class
FUNDAMENTALS OF
PROGRAMING
1. Implicit Type Conversion in C#
Implicit type conversion, the C# compiler automatically converts one type to another.
Generally, smaller types like int (having less memory size) are automatically converted to larger types like double
(having larger memory size).
int num = 1500;
// Implicit Conversion
double numDouble = num;
int num = 2147483647;
long bigNum = num;
FUNDAMENTALS OF
PROGRAMING
1. Explicit Type Conversion in C#
In explicit type conversion, we explicitly convert one type to another.
Generally, larger types like double (having large memory size) are converted to smaller types like int (having small
memory size).
double numDouble = 1.23;
// Explicit casting
int numInt = (int) numDouble;
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
FUNDAMENTALS OF
PROGRAMING
1. Explicit Type Conversion in C#
C#, we can also use the Parse() method to perform type conversion.
Generally, while performing type conversion between non-compatible types like int and string, we use Parse().
string n = "100";
// converting string to int type
int a = int.Parse(n);
String str = "test";
int a = int.Parse(str); /// error
FUNDAMENTALS OF
PROGRAMING
1. Explicit Type Conversion in C#
C#, we can also use the Parse() method to perform type conversion.
Generally, while performing type conversion between non-compatible types like int and string, we use Parse().
int num = 100;
Console.WriteLine("int value: " + num);
// convert int to string
string str = Convert.ToString(num);
Console.WriteLine("string value: " + str);
FUNDAMENTALS OF
PROGRAMING
1. Explicit Type Conversion in C#
Method Description
ToBoolean() converts a type to a Boolean value
ToChar() converts a type to a char type
ToDouble() converts a type to a double type
ToInt16() converts a type to a 16-bit int type
ToString() converts a type to a string
FUNDAMENTALS OF
PROGRAMING
Case Sensitives
int Count = 1;
string[] flower = { "pink”,”yellow”,”red”,”green" };
//for (int count = 0; count <flower.Length; count++)
for (int count = 0; count <flower.Length; count++)
{
Console.WriteLine(flower[Count]);
}
FUNDAMENTALS OF
PROGRAMING
IF else Condition
FUNDAMENTALS OF
if(condition1)
PROGRAMING
{
// code block to be executed when if condition1 evaluates to true
}
else if(condition2)
{
// code block to be executed when
// condition1 evaluates to flase
// condition2 evaluates to true
}
else if(condition3)
{
// code block to be executed when
// condition1 evaluates to flase
// condition2 evaluates to false
// condition3 evaluates to true
}
Else{ } // no condition here always execute
FUNDAMENTALS OF
if(condition1)
PROGRAMING
{
// code block to be executed when if condition1 evaluates to true
}
else if(condition2)
{
// code block to be executed when
// condition1 evaluates to flase
// condition2 evaluates to true
}
else if(condition3)
{
// code block to be executed when
// condition1 evaluates to flase
// condition2 evaluates to false
// condition3 evaluates to true
}
Else{ } // no condition here always execute
// Declaring and initializing variables
int age = 26;
if (age < 18)
{
Console.WriteLine("The person should go to school and study");
//else statement && operator
else if(age>=18 && age<60){
Console.WriteLine("The person should go to party and enjoy life");
else{
Console.WriteLine("The person should stay at home and take rest");
}
FUNDAMENTALS OF
PROGRAMING
Console.WriteLine("Enter username:");
// Create a string variable and get user input from the
keyboard and store it in the variable
string userName = Console.ReadLine();
// Print the value of the variable (userName), which will
display the input value
Console.WriteLine("Username is: " + userName);
FUNDAMENTALS OF
PROGRAMING
Input CNIC
Verfied length
If length < 14 Message not complete
If length > 14 Extra num is entered
switch (variable/expression)
{
case value1:
// Statements executed if expression(or variable) = value1
break;
case value2:
// Statements executed if expression(or variable) = value1
break;
... ... ...
... ... ...
default:
// Statements executed if no case matches
}
char ltr;
Console.WriteLine("Enter any letter");
ltr = Convert.ToChar(Console.ReadLine());
switch(Char.ToLower(ltr))
{
case 'a':
Console.WriteLine("Vowel");
break;
case 'e':
Console.WriteLine("Vowel");
break;
case 'i':
Console.WriteLine("Vowel");
break;
case 'o':
Console.WriteLine("Vowel");
break;
case 'u':
Console.WriteLine("Vowel");
break;
default:
Console.WriteLine("Not a vowel");
break;}
C# - while Loop
C# :while loop to repeatedly execute a block of code as long
as the specified condition becomes true.
C# - while Loop
int i = 0; // initialization
while (i < 10) // condition
{
Console.WriteLine("i = {0}", i);
i++; // increment
}
C# - while Loop
int i, n;
Console.Write("Enter a Number : ");
n = Convert.ToInt32(Console.ReadLine());
i = 2;
while (i <= n)
{
Console.WriteLine(i);
i = i + 2;
}
Console.ReadKey();
WHAT'S APP NUMBER
WHAT'S APP :92-3193416769
EMAIL:[email protected]