Slider 1 Slider 2 Slider 3 Slider 4

C Sharp Loops



In a program many times you would need to repeat instructions. In C#, the for statement is one form of a loop that lets you repeat statements. However, as we already know, a statement can also be a block of statements, thus it also allows repetition of multiple statements.


For Loop.

The Basic Structre of For Loop

for(assignment; condition; increment/decrement)
       { statements or block of statements}


Example:1

For(i=1; i<=15; i++)
{
Console.writeline(“The value of  i  is {0}”,i)
}


Example:2


class xyz
{
static void Main()
{
int i;
for ( i = 1; i <= 5; i++)
System.Console.WriteLine ("Hi  {0}", i);
}
}

Output
Hi  1
Hi  2
Hi  3
Hi  4
Hi  5

The for has 2 semicolons as part of its syntax or rules. The statement goes on till the condition is false. When i has the value 6, the condition checked is, is 6 <= 5. The answer being false, the for terminates. This is how the for statement enables the repetition of code. 


While Loop.


Note: The while loop takes a condition before entering the first iteration, hence the variable i is initialized to 1 before entering the loop.


General Form of while Loop is

           while(expression)
     
         { statements or block of statements}

Example:1
      
       int i=1;
  
       while(i<=10)

{
Console.writeline(“The value of  i  is {0}”,i);

 i++;
}

     
Example:2

class xyz
{
static void Main()
{
int i;
i=1;
while ( i <= 5 )
{
System.Console.WriteLine ("Hi  {0}", i);
}
}
}

The condition checks whether i <= 5 evaluates to true. Currently the value of i is 1. The condition evaluates to true and the statement within the curly braces is executed. System.Console.WriteLine is called with Hi and the value of i. Note that here we are not changing the value of i within the loop. Since the value of i remains 1 the condition always evaluates to true and the loop will go on forever, indefinitely.

Our next program3 will resolve this problem.

Example :3

class xyz
{
static void Main()
{
int i;
i=1;
while ( i <= 5 ){
System.Console.WriteLine ("Hi  {0}", i);
i++;
}
}
}

Output
Hi 1
Hi 2
Hi 3
Hi 4
Hi 5

This program is similar to the previous one. The only change that we have made is that we added the line i++; This will go on until i becomes 6. Once i is 6 the condition evaluates to false and the loop terminates.

Now the question is, "Should I use a for or a while?' To answer your question in the most simple manner is that the for loop is similar to the while loop  In other words, it is at your discretion to use the one you are comfortable with. Once again C# offers you multiple ways of doing the same thing.


do while Loop.


Note: The while loop executes first and , then the Condition is Checked. The loop will continue while the condition remains true.


General Form of while Loop is

           while(expression)
     
         { statements or block of statements}

Example:1
      
int i=1;
  
 do     
{

Console.writeline(“The value of  i  is {0}”,i);

 i++;

} while(i<=10)


     
Example:2

class xyz
{
static void Main()
{
int i;
i=1;
do
{
System.Console.WriteLine ("Hi  {0}", i);
} while ( i <= 5 )
}
}

C Sharp Function



let's understand how functions are called and created.

Here we are calling a function abc().


class xyz

{

static void Main()

{

abc();

}

}


On executing this program you will get, an error. Compiler Error (5,1): error CS0103: The name 'abc' does not exist in the class or namespace 'xyz'
                                                                                 
In any case, the error says that abc does not exist. Here we are calling a function called abc(), but where is abc() defined or created ? It is not a function that has been provided by C# to us free of charge. It is our own homegrown function that we are calling. The lesson here is that we cannot call a function without first creating it. So, to rectify this error we will first create a function abc. Our next example demonstrates this.

class xyz
{
static void Main()
{
abc();
}


static void  abc()

{
System.Console.WriteLine ("Suhail");
}


}


Output
Suhail

. 

You can call as many functions as you like from your program. But you must remember to separate each one with a semi-colon. The next program illustrates this.


class xyz
{

static void Main()

{
abc();

pqr();

abc();
}
                                                                                      
static void  abc()

{
System.Console.WriteLine ("I am ABC");
}

static void  pqr()

{
System.Console.WriteLine ("I am PQR ");
}

}

Output
I am ABC
I am PQR
I am ABC

At first the function abc is called, then pqr and then again we are calling abc. On executing this program  'I am ABC', 'I am PQR' and 'I am ABC' will be displayed.  This is because we have included the code for these lines to be displayed in the respective functions.

In the following program we are calling the function pqr from abc and not from Main.


class xyz
{
static void Main()
{
abc();
}
static void  abc()
{
pqr();
System.Console.WriteLine ("I am ABC");
}
static void  pqr()
{
System.Console.WriteLine ("I am PQR ");
}
}

Output
I am PQR
I am ABC

In the function abc, we are first calling pqr and then displaying 'I am ABC' using the WriteLine function. Hence, first 'I am PQR' is displayed and then 'I am ABC'. Thus, this program demonstrates how functions can be called from other functions.

Now that we have created our own functions abc and pqr, we have an intuitive understanding of how C# created the function System.Console.WriteLine.

The next program uses the printing or formatting capabilities of the WriteLine function.


class xyz
{
static void Main()
{
System.Console.WriteLine("100 {0}",100);
}
}

Output
100 100

The zero in the curly braces means that after the first comma there is some value and that it should display this value. You cannot differentiate between the two 100's. The {0} is replaced with 100, the number, which follows the first comma. If you don't like the number 100, use the number 420 instead. We won't mind - at least it's something that some of you can easily identify with!

The program below is simply an extension of the above.


class xyz
{
static void Main()
{
System.Console.WriteLine("100 {0},{1}",100,200);
}
}

Output
100  100,200

Here the {0} is replaced with 100 and {1} is replaced with 200. The comma (,) separates the two numbers. Thus {0} means the first number and {1} means the second. C# likes to count from zero and not one.

C Sharp Introduction


 
C# is pronounced as "C sharp".

Enables programmers in quickly building solutions

basic structure of a C# program.

xyz.cs
class xyz
{
……..
……..
} 

In general everything has a beginning and an end. Similarly, a C# program also has a start and an end. 

You can specify the entry point by adding static void Main() to your program, just as we have done below.


class xyz

{

static void Main()
{

}

} 


If we Compile the above code,  the program will run successfully without any errors but shows no output on the  screen. The compiler will generate an exe file.

The words, static and void, will be explained to you a little later,  Main() is nothing but a function. Here we are creating a function called Main. It is followed by the '{' and '}' curly braces. Note that the letter M in Main is capital. The { signifies the start of a function and the } signifies the end of the function.

Now we are going to  add some code in our program.


class xyz
{
static void Main()
{
System.Console.WriteLine("Suhail Osmani")
}
}


Output

Suhail Osmani

Here WriteLine is a function name which exist in Console Class and Console Class inside System Namespace. 

In the next program, we have called WriteLine function twice.


class xyz
{
static void Main()
{
System.Console.WriteLine("Suhail");
System.Console.WriteLine("Osmani");
}
}

Output
Suhail
Osmani

On executing this program, 'Suhail' and 'Osmani' are displayed on two separate lines. Here, we are not required to give anything that has the 'enter' effect, WriteLine automatically prints on a new line each time. Which simply means you can call a function as many times as you like.
Copyright © 2016 AspDotNet link .