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

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
WHAT'S APP NUMBER
WHAT'S APP :92-3193416769
EMAIL:12SOLUTIONSSS@GMAIL.COM

You might also like