Lec 2
Lec 2
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;
string[] cars;
string[] cars = {“mehran", “alto", “Toyota", “city"};
Console.WriteLine(cars[0]);
FUNDAMENTALS OF
PROGRAMING
// Create an array of four elements, and add values later
string[] cars = new string[4];
Loops
1.For
2.Foreach
3.While
4.Do While
FUNDAMENTALS OF
PROGRAMING
for (initialization; condition; iterator)
{
// body of for loop
}
String concatenation
String functions
Generally, smaller types like int (having less memory size) are automatically converted to larger types like double
(having larger memory size).
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
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);
Generally, while performing type conversion between non-compatible types like int and string, we use Parse().
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{
Console.WriteLine("The person should stay at home and take rest");
}
FUNDAMENTALS OF
PROGRAMING
Console.WriteLine("Enter username:");
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
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]