Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

13 Nov 2011

C# for loop


The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The for loop is handy for iterating over arrays and for sequential processing. In the following example, the value of int i is written to the console and i is incremented each time through the loop by 1.
Example:
// statements_for.cs
// for loop
using System;
class ForLoopTest
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
}
}

Output: 

5









Remarks:
The for statement executes the enclosed statement or statements repeatedly as follows:
  • First, the initial value of the variable i is evaluated.
  • Then, while the value of i is less than or equal to 5, the condition evaluates to true, the Console.WriteLine statement is executed and i is reevaluated.
  • When i is greater than 5, the condition becomes false and control is transferred outside the loop.
Because the test of conditional expression takes place before the execution of the loop, therefore, a for statement executes zero or more times.
All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:
for (;;)
{
// ...
}

C# switch statement


The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body as the following example:
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}

C# foreach loop example with string array


This example shows how you can use the keyword foreach on a string array in a C# program to loop through the elements in the array. In the foreach-statement, you do not need to specify the loop bounds minimum or maximum, and do not need an 'i' variable as in for-loops. This results in fewer characters to type and code that it is easier to review and verify, with no functionality loss.
Program that uses foreach over array [C#]

using System;

class Program
{
static void Main()
{
// Use a string array to loop over.
string[] ferns =
{
"Psilotopsida",
"Equisetopsida",
"Marattiopsida",
"Polypodiopsida"
};
// Loop with the foreach keyword.
foreach (string value in ferns)
{
Console.WriteLine(value);
}
}
}

Output

Psilotopsida
Equisetopsida
Marattiopsida
Polypodiopsida

C# if else


The if statement selects a statement for execution based on the value of a Boolean expression. In the following example a Boolean flag flagCheck is set to true and then checked in the if statement. The output is: The flag is set to true.
bool flagCheck = true;
if (flagCheck == true)
{
Console.WriteLine("The flag is set to true.");
}
else
{
Console.WriteLine("The flag is set to false.");
}

C# Working With string part 2

Consider this example, which uses Replace, Insert, and ToUpper:

public class TestStringsApp
    public static void Main(string[] args) 
    { 
         string a = "strong"; // Replace all 'o' with 'i' 
         string b = a.Replace('o', 'i'); 
         Console.WriteLine(b);
         string c = b.Insert(3, "engthen"); string d = c.ToUpper(); 
         Console.WriteLine(d); 
    }
}

The output from this application will be:

string STRENGTHENING

C# Working With int Array 1


Int array example

As an introduction to the C# array type, let's look at a simple example program that allocates and initializes an integer array of three elements. Please notice how the elements can be assigned to or read from using the same syntax (values[int]). The array is zero-based; we also demonstrate the foreach loop upon it.
Program that uses an array [C#]

using System;

class Program
{
static void Main()
{
// Use an array.
int[] values = new int[3];
values[0] = 5;
values[1] = values[0] * 2;
values[2] = values[1] * 2;

foreach (int value in values)
{
Console.WriteLine(value);
}
}
}

Output

5
10
20

C# Working With Int Array


This first example shows how you can declare int arrays in four different ways. The first three arrays declared below are declared on single lines, while the fourth array is declared by individual assignment of the elements.
Code:
using System;

class Program
{
static void Main()
{
int[] arr1 = new int[] { 3, 4, 5 }; // Declare int array
int[] arr2 = { 3, 4, 5 }; // Another
var arr3 = new int[] { 3, 4, 5 }; // Another

int[] arr4 = new int[3]; // Declare int array of zeros
arr4[0] = 3;
arr4[1] = 4;
arr4[2] = 5;

if (arr1[0] == arr2[0] &&
arr1[0] == arr3[0] &&
arr1[0] == arr4[0])
{
Console.WriteLine("First elements are the same");
}
}
}

Output

First elements are the same

C# integer


You can declare and initialize a variable of the type int like this example:
        int i = 123;
When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In this example, it is of the type int.

C# Your First Program

C# Hello World! Tutorial 1

1. Open Visual Studio and Click on New Project.

2. Click Visual C# > Console Application and name it Hello World

3. This Window will appear and all the code we'll write in main function.

4. Write the code shown below. as you write cout << "Hello World!"; in C++.
here you'll write Console.WriteLine("Hello World!"); and compile the program by pressing F5.

5. Here's the final output.