Vous êtes sur la page 1sur 29

Loops are generally used to execute repetitive statements.

The following are the


different types of loops we have in C#
for loop
while loop
Do..While loop
foreach loop
Below is a simple example illustrating for loop.
This sample program expects an integer limit from the user upto which to generat
e even numbers. The entered value must be an integer. If the user inputs anythin
g apart from numbers an exception will be raised and an error message will be sh
own to the user. For catching exceptions and showing valid error messages we are
using exception handling in our program using try..catch statements. We will ta
lk about exception handling in our later article.
using System;
class ForLoopDemo
{
public static void Main()
{
int Limit=0;
Console.WriteLine("Enter the limit upto which to generate even numbers?"
);
try
{
Limit = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Entered value should be a number between 1 and 10
0000");
}
for (int i = 0; i <= Limit; i++)
{
if ((i % 2) == 0)
{
Console.WriteLine(i);
}
}
}
}

The for loop in the above program has three parts as shown below
for ( <initialization list>; <boolean condition>; <repetitive list> )
{
<statements>
}
The initialization list is a comma separated list of expressions. These expressi
ons are evaluated only once during the lifetime of the for loop. This is a one-t
ime operation, before loop execution. This section is commonly used to initializ
e an integer to be used as a counter.
Once the initialization list has been evaluated, the for loop gives control to i
ts second section, the boolean condition. There is only one boolean condition, b
ut it can be as complicated as you like as long as the result evaluates to a boo
lean true or false. The boolean condition is commonly used to verify the status
of a counter variable.
When the boolean condition evaluates to true, the statements within the curly br
aces of the for loop are executed. After executing for loop statements, control
moves to the top of loop and executes the repetitive list, which is normally use
d to increment or decrement a counter.
Understanding while loop
A while loop is very much similar to a for loop except that initialization, chec
king the boolean condition and incrementing the counter value is done at differe
nt steps as shown in the below example, otherwise while loop is same as the for
loop.
Note: An important point to keep in mind is that, if you miss incrementing the c
ounter variable, the program will run into an infinite loop.
using System;
class WhileLoopDemo
{
public static void Main()
{
int Limit=0;
Console.WriteLine("Enter the limit upto which to generate even numbers?"
);
try
{
Limit = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Entered value should be a number between 1 and 10
0000");
}
int i = 0; // Initialize counter variable
while (i <= Limit) // Condition check
{
if ((i % 2) == 0)
{
Console.WriteLine(i);
}
i++; //Increment the counter
}
}
}

Understanding do..while loop


Do..while loop is very similar to while loop except that the boolean condition i
s checked at the end of the loop. This means that a Do..while loop is guranteed
to execute atleast once. The same example we have been following thru this artic
le is rewritten using Do..while loop as shown below.
using System;
class DoWhileLoopDemo
{
public static void Main()
{
int Limit=0;
Console.WriteLine("Enter the limit upto which to generate even numbers?"
);
try
{
Limit = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Entered value should be a number between 1 and 10
0000");
}
int i = 0; // Initialize counter variable
do
{
if ((i % 2) == 0)
{
Console.WriteLine(i);
}
i++; //Increment the counter
}
while (i <= Limit); // Condition check
}
}

Understanding foreach loop


forech loop is a new introduction in C#. foreach loop is basically used to loop
thru a collection of items in a list or a collection. For example if we have a s
tring array of names and if we want to loop thru the array and print all the nam
es in the array we can use the foreach loop.
To achieve this we can use a for loop as well. But to use a for loop, we need to
know how many elements are present in the collection before hand so we can loop
that many times and print out the names. What if we dont have the information a
bout number of elements in the collection before hand. Thats when foreach loop b
ecomes very handy.
foreach loop is extremely useful when we donot know how many elements are presen
t in the collection. The below example loops thru the list of names in the Names
array and print them to the screen.
using System;
class ForEachLoopDemo
{
public static void Main()
{
string[] Names = { "Prasad","Giri","Kiran","Ravi"};
foreach (string s in Names)
{
Console.WriteLine(s);
}
}
}

Understanding the significance of "break" and "continue" statements in loops


continue and break statements can be optionally used to control how the loops ar
e executed. In general when a continue statement is encountered, the program wil
l skip executing the lines following the continue statement and the control will
be transfered back to the first line in the loop and will continue executing.
If a break statement is encountered the control comes out of the loop. Our examp
le below will print even numbers between 1 and 20.
The program will print even numbers 1 thru 10.
After print 10, user is provided with an option to continue printing even number
s or break out of the for loop.
If user opts B to break out of the loop, the rest of the even numbers are not pr
inted and the execution of the for loop is terminated. So when ever a break stat
ement is encountered in a loop, the program execution breaks out of the loop.
If user opts C to continue, rest of the even numbers 11 thru 20 are printed. How
ever, an interesting thing to ote is, the line after the continue statement is n
ot executed. This is bcos continue statement will skip the lines following it an
d control will be transfered back to the first statement in the loop.
using System;
class ForEachLoopDemo
{
public static void Main()
{
Console.WriteLine("This program will print even numbers between 0 and 20
");
for (int i = 0; i <= 20; i++)
{
if ((i % 2) == 0)
{
Console.WriteLine(i);
}
if (i == 11)
{
Console.WriteLine("Select an option B - Break, C - Continue");
string option = Console.ReadLine();
if (option == "B")
{
Console.WriteLine("You have chosen to break out of the for b
lock and stop printing");
Console.WriteLine("");
Console.WriteLine("This program will terminate now");
break;
}
if (option == "C")
{
Console.WriteLine("Program will continue printing even numbe
rs from 11 to 20");
continue;
Console.WriteLine("This statement will not be printed"); //c
ontinue statement will
} //s
kip this statement and
} //c
ontinue executing from
} //t
he first line of the loop
}
}
Njoy Programming
ByPrasad Cherukuri

Loops in C# 10/1/2008 11:26:43 AM - By Swetha Naidu


Give a sample program to print numbers in Pyramid format using loops
Loops in C# with examples - while, do while, for, foreach 10/1/2008 11:37:28 AM
- By Prasad Cherukuri
The following program shows how to print numbers in a Pyramid format using loops
.
using System;
namespace DemoSample
{
class Program
{
static void Main()
{
PrintNumbers();
}
private static void PrintNumbers()
{
Console.WriteLine("Enter the number of star lines you wish to print"
);
int NumberOfLines = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < NumberOfLines; i++)
{
int stars = 1 + 2 * (i);
int space = NumberOfLines - i;
for (int j = 0; j < space; j++)
{
Console.Write(" ");
}
for (int k = 0; k < stars; k++)
{
Console.Write(i+1 + " ");
}
Console.WriteLine();
}
}
private void PrintPyramid()
{
Console.WriteLine("Enter the number of star lines you wish to print"
);
int NumberOfLines = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < NumberOfLines; i++)
{
int stars = 1 + 2 * (i);
int space = NumberOfLines - i;
for (int j = 0; j < space; j++)
{
Console.Write(" ");
}
for (int k = 0; k < stars; k++)
{
Console.Write("*" + " ");
}
Console.WriteLine();
}
}
}
}

Loops in C# with examples - while, do while, for, foreach 1/6/2009 12:25:43 AM -


By Sandesh
XPathNavigator x = MainDataSource.CreateNavigator();
XPathNodeIterator rows = x.Select("/my:myFields/my:gvBranchC
hange/my:BranchChange/my:gvBranchChangeOption", NamespaceManager);
XPathNodeIterator Newrows = x.Select("/my:myFields/my:gvBran
chChange/my:BranchChange/my:gvBranchChangeOption", NamespaceManager);
int row = Convert.ToInt32(rows.Count.ToString());
string strBranchName = "";
int newRow = Convert.ToInt32(Newrows.Count.ToString());
string strBranchID = "";
string strNewBranchID = "";
while (rows.MoveNext())
{
strBranchID = Convert.ToString((rows.Current.SelectS
ingleNode("my:ddlBranch", NamespaceManager).Value));
Newrows.MoveNext();
while (Newrows.MoveNext())
{
strNewBranchID = Convert.ToString((Newrows.Curre
nt.SelectSingleNode("my:ddlBranch", NamespaceManager).Value));
if (strBranchID == strNewBranchID)
{
return;
}
}
}
I have repeting table and there is 2 column, 1.Option and another Branch Name wh
ich is dropdown . I got branch id from dropdown, In above loop I am checking sam
e branch id is not contain two rows , when it same it return but I am not unders
tand how to set Message?
code in c# to print pyramid 1/19/2009 5:51:25 AM - By Anamika
Hi
please help me for printing pyramin in c# for console application

Loops in C# with examples - while, do while, for, foreach 2/23/2009 2:02:13 AM -


By vijay
hiii to all
am very new to C# progming so i got stuck in print the number using for loop
and am also using the threading concept in that the code is...
class threadtest
{
static void main(string [] args)
{
thread t = new thread( new threadstart(go));
t.start();
}
static void go()
{
for(int i=0;i<=20;i++)
Console.WriteLine('i');
Console.ReadLine();
}
}
}
]

Loops in C# with examples - while, do while, for, foreach 3/4/2009 12:25:56 AM -


By Ather
class threadtest
{
static void main(string [] args)
{
thread t = new thread( new threadstart(go));
t.start();
}
static void go()
{
for(int i=0;i<=20;i++)
Console.WriteLine('i');
Console.ReadLine();
}
}
}
]

Loops in C# with examples - while, do while, for, foreach 6/2/2009 4:28:22 AM -


By shahr
how can i print in one consol writeline many numbers on c#
Loops in C# with examples - while, do while, for, foreach 6/18/2009 4:38:09 AM -
By sijo a j
i want learn more about loops in c# and also like to to be experienced with some
programms that uses these loops.iam An ACCP student at APTECH
loops in c# 6/18/2009 4:41:33 AM - By sijo
Sir
please send me more programms for loops . like patterns and some programms th
at may use in the industry

Loops in C# with examples - while, do while, for, foreach 6/19/2009 10:48:28 AM


- By sijo
sir
how can i create this pattern
1111
1122
1133
....

and 22 11
2222
2233

printing numbers to form a so called square shape using the first 20 natural num
bers 8/29/2009 1:23:11 PM - By Neeraj Kumar Pandey
Please help me to solve the problems that display the following output the forma
t are as follows 1 2 3 4 5 14 15 16 17 6 13 20 19 18 7 12 11 10 9 8
loop programs 9/1/2009 10:30:54 AM - By Trishita Borthakur
sir
i want to print these numbers but cant do it. can u pls help
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

1 1 1 1 1
1 1 1 2
1 1 3
1 4
5

Loops in C# with examples - while, do while, for, foreach 9/9/2009 5:54:42 AM -


By Dhananjaya Hiremath
To print Numbers like
12345
1234
123
12
1

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The number");
int a = 12345;
Console.WriteLine(a);
while (a>1)
{
a = a / 10;
Console.WriteLine(a);

}
}
}
}

How to print numbers in triangle shape 9/9/2009 6:03:03 AM - By Dhananjay hirema


th
how to print values like thiss in c#
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

how to print such formet in C... 9/10/2009 9:13:21 AM - By firen


help pls how to print this in C, C++ or VB. any of those language...
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Loops in C# with examples - while, do while, for, foreach 9/21/2009 12:24:44 AM
- By R.Chitra
Dear Sir/Madam
I want to print below output using loops concept in C#.Pls help me
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Loops in C# with examples - while, do while, for, foreach 9/24/2009 7:29:39 PM -
By jlyumul
how to create this program? can you please help me?
1
121
12321
1234321

Loops in C# with examples - while, do while, for, foreach 10/2/2009 6:53:08 AM -


By usman
Q1: *
* * *
* * * * *
* * *
*

Q2:1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1

S
Q3: T
R
I
N
G
N
I
R
T
S

Loops in C# with examples - while, do while, for, foreach 10/24/2009 6:40:36 AM


- By mandeep
can u help me for printing this output
1
2 3
4 5 6
Difference between "do while" and "for" loop in vb.net 11/1/2009 1:30:50 PM - By
prashant
Can u help me to find out the Difference between "do while" and "for" loop in vb
.net
Loops in C# with examples - while, do while, for, foreach 11/2/2009 3:34:16 AM -
By Rushang
How do i convert comma delimited number into only number?
have u any function that do this or anything else?
for example if i have 1,2,3,4 then it will give me output 1234

Loops in C# with examples - while, do while, for, foreach 11/15/2009 2:30:42 AM


- By lara
how i can use while to applied in 'boo' && 'int' in the same statment
like this code
bool[] intr = {true,false};
int turn=1;
while (intr[0] && turn=1) //here error

Loops in C# with examples - while, do while, for, foreach 11/15/2009 1:51:00 PM


- By Nadeem Mughal,Pakistan
This page n examples are very helping for beginners of C#
how to print such formet in C... 9/10/2009 9:13:21 AM - By firen 11/20/2009 1:14
:55 AM - By Raheel
Plz give me code to print * * * * * * * * * * * * * * * * Thanks.
Loops in C# with examples - while, do while, for, foreach 12/2/2009 8:07:38 AM -
By prajktagaikwad
want odd numbers between 1 to 100 bby using while loop
Loops in C# with examples - while, do while, for, foreach 12/2/2009 6:50:10 PM -
By salma
i want to make apyramide of 7numbers i succseded doing it with for loop but i ca
n,t do it with while loop i want some help
thanks all

Loops in C# with examples - while, do while, for, foreach 12/8/2009 9:57:26 AM -


By durga shankar mishra
while do-while loop for in a c# plz help on my mail
Loops in C# with examples - while, do while, for, foreach 1/3/2010 7:14:43 AM -
By Irfan
hello sir/madam
i want to represent 1234 as 1 2 3 4

please tell me what 'll be the code in turbo c


thanks

Loops in C# with examples - while, do while, for, foreach 1/6/2010 6:52:54 PM -


By Encarnavion Rojas
sample programs in c++ using loops that can display the first even numbers
Loops in C# with examples - while, do while, for, foreach 1/12/2010 2:17:59 AM -
By Mark Francis
How Can I Play/Move Automatically This Output:
Enter Name:Superman
Output is Superman will move at the end of the Right Side of the screen then at
the end of the bottom going left then up like a cycle around the monitor in Turb
o C....

Please Help Me!...


Thanks:Mark
Loops in C# with examples - while, do while, for, foreach 1/18/2010 7:39:47 AM -
By gouse
write a programe to print the
1
2 2
3 3 3
4 4 4 4

Loops in C# with examples - while, do while, for, foreach 1/25/2010 12:13:15 AM


- By Amit Srivastava
Q1. A g M s
B h N t
C i O u
D j P v
E k Q w
F l R x

Q2. A I
B H
C G
D F
E
Q3. 1
12
124
1246
12468
1357
135
13
1

Q4. * * * * * * *
* * *
*

Q5. []

Q6. X

for loop concept in c# 1/25/2010 12:21:43 AM - By Amit Srivastava


plz help me code to print this
Thanks:

Q1. A g M s
B h N t
C i O u
D j P v
E k Q w
F l R x

Q2. A I
B H
C G
D F
E

Q3. 1
12
124
1246
12468
1357
135
13
1

Q4. * * * * * * *
* * *
*

Q5. []

Q6. X

c# for loop concept 1/25/2010 12:37:10 AM - By Amit Srivastava


plz sir help me i am not understanding this type of output how to display plz gi
ve me code to print these type of output plz mail it on my email address.

Thanks

Loops in C# with examples - while, do while, for, foreach 2/11/2010 9:40:50 AM -


By deepa
pls give the code for :
1
2 2
3 3 3
4 4 4 4

Loops in C# with examples - while, do while, for, foreach 2/13/2010 12:38:18 AM


- By yasmine
write the program that print the shape in console c#
*
* *
* * *
* * * *
* * * * *

Loops in C# with examples - while, do while, for, foreach 2/19/2010 8:58:03 PM -


By rose salvador
write the program that print the shape in console c#
*
* *
* * *
* * * *
* * * * *

Dear Sir/Madam
I want to print below output using loops concept in C#.Pls help me
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Loops in C# with examples - while, do while, for, foreach 2/19/2010 10:54:20 PM


- By rinku
sir,
help me and provide me the code

*
* *
* * *
* * * *
* * * * *

1
2 2
3 3 3
4 4 4 4

Loops in C# with examples - while, do while, for, foreach 2/20/2010 2:29:29 AM -


By abhi
1 121 12321 1234321 pls give me the codes thanks
Loops in C# with examples - while, do while, for, foreach 2/27/2010 2:35:27 AM -
By raf
plss give me a program like this output!!! plss help me!!
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

any kind of loop!!!using turbo C 2/27/2010 2:38:17 AM - By RAf


plss give me a program like this output using turbo C!!! pls help me
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Loops in C# with examples - while, do while, for, foreach 3/2/2010 11:47:08 PM -
By Laveena
sir,
help me and provide me the code

*
* *
* * *
* * * *
* * * * *

1
2 2
3 3 3
4 4 4 4

1
1 2
1 2 3
1 2 3 4
Loops in C# with examples - while, do while, for, foreach 3/5/2010 10:37:54 PM -
By franz
Dear Sir/Madam I want to print below output using loops concept in C#.Pls help m
e 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Loops in C# with examples - while, do while, for, foreach 3/21/2010 1:28:23 PM -
By sumit kumar
hello sir, Kindly suggest so that i could achieve an output as follows * * * * *
* * * * * * * *
Loops in C# with examples - while, do while, for, foreach 3/22/2010 5:56:20 AM -
By navya
write a c-program in while loop for the following;
1+1/2!+1/3!+1/4!+.......1/n!
1+1/2-1/4+1/6-.............1/nterms

Loops in C# with examples - while, do while, for, foreach 3/25/2010 2:47:23 AM -


By jefferson
how to write a while statement program that will display this:
*
**
***
****
*****
****
***
**
*

loops in c# with examples - while,do while,for,foreach 5/9/2010 9:11:34 AM - By


Ashmeet
Q1. Print all numbers between two numbers entered by the user?
Q2. Enter ten numbers show how many are even and how many are odd numbers?
Q3. Print series 1,1,2,3,5,8.
Q4. Write a program to find out the product of an natural number?
Q5. Write a program to find out the sum of the square of the first n natural num
ber which are divisible by 3?
Q6. Two numbers are entered through a keyboard,write a program to find the value
of one number raise to power of another number?

Loops in C# with examples - while, do while, for, foreach 6/4/2010 1:31:04 AM -


By Elizabeth nega
write a program that displays 6 names assigned to an array using a for loop
Loops in C# with examples - while, do while, for, foreach 6/17/2010 5:03:27 AM -
By Nasir Khan
how to print like this
* *
* * *
* * *
* * *
* * *
* * *
* * *
* *
* *
* *
* *
* *
* *
* **
*
*
*
*
*

Loops in C# with examples - while, do while, for, foreach 6/18/2010 7:05:12 AM -


By Chahat
Dear Sir/Madam
I want to print below output using loops concept in C#.Pls help me

1
2 2
3 3 3
4 4 4 4
Loops in C# with examples - while, do while, for, foreach 6/23/2010 1:48:40 AM -
By kulla
Really good to understand
Loops in C# with examples - while, do while, for, foreach 7/4/2010 10:45:43 PM -
By shilpa
write the program to print the table of given numbers
write the dispaly the formate dear mam/ sir 7/4/2010 10:59:35 PM - By shilpa sha
rma
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

1 1 1 1 1
1 1 1 2
1 1 3
1 4
5

Loops in C# with examples - while, do while, for, foreach 7/11/2010 12:51:23 AM


- By Manali
write a programm for print odd number from 1 to 30 by using for loop
Loops in C# with examples - while, do while, for, foreach 7/23/2010 9:58:25 AM -
By I want to print below out
123454321 1234 4321 123 321 12 21 1 1
Loops in C# with examples - while, do while, for, foreach 7/24/2010 3:18:27 AM -
By sana zulfiqar
kindly help me how to make a program of the following output... 1 12 123 1234 12
345 123456 1234567 12345678 123456789
Loops in C# with examples - while, do while, for, foreach 7/31/2010 5:37:16 AM -
By ilesh
write the program that print the shape in console c#
*
* *
* * *
* * * *
* * * * *

Loops in C# with examples - while, do while, for, foreach 8/2/2010 5:16:05 AM -


By
printing 12345,1234,123,12,1 in c language. 8/2/2010 5:56:23 AM - By abhinav
/* source code*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i , j ; // comment integers i &j
clrscr(); // comment for clear screen
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",j)
}
printf("\n");
}
getch();
}
output
12345
1234
123
12
1

Using for loop what will be the c programming to display * * * * * * * * * * 8/2


/2010 3:11:26 PM - By Dramjen
I'l be thnkful to take tis answer soon
can u please help me on creating a program that if u enter a number it will coun
t from o upto the nu 8/9/2010 7:55:16 PM - By niJoe
output will be like this... enter a number: 10 0 1 2 3 4 5 6 7 8 9 10
can u pls give me a program using loop statement that would give an output like
this 8/11/2010 3:10:21 AM - By vea
4 3 2 1
4 3 2
43
4
Loops in C# with examples - while, do while, for, foreach 8/13/2010 2:00:50 AM -
By suji
can u pls give me a program using loop statement that would give an output like
this 8/11/2010 3:10:21 AM - By vea
4 3 2 1
4 3 2
43
4

Loops in C# with examples - while, do while, for, foreach 8/17/2010 8:45:47 AM -


By ycel arbes
good eve, sir.. how I can program in c# 2008 about pyramid like this
#
##
###
####
#####
######
Loops in C# with examples - while, do while, for, foreach 8/18/2010 4:09:08 AM -
By Jabir khan
Write a program using a while loop that will ask the user the following
Enter how many numbers you would like to add > 3
Please enter the 1 number> 24
Please enter the 2 number> 55
Please enter the 3 number > 100

The total of 3 numbers = 179

Your program should consist of comments to the program and a snapshot of the out
put.

Write aboutloops used in visual basic 8/22/2010 4:51:37 AM - By lalremruata


i have no idea couse iask u please answer me
What is datatype control ? Explaine write example 8/22/2010 4:59:14 AM - By lalr
emruata
couse i have no idea i ask u please answer me
Loops in C# with examples - while, do while, for, foreach 8/26/2010 12:10:25 AM
- By shakti
1
2 3
4 5 6
7 8 9 10
Loops in C# with examples - while, do while, for, foreach 8/26/2010 3:33:08 AM -
By pritesh
how to print this out put
1
2
three
4
.
.
.
.
10
11
12
1three
14
.
.
.
.
20
21
22
2three
.
.
.
three0
three1
three2
threethree
.
.
.

Loops in C# with examples - while, do while, for, foreach 9/7/2010 9:15:38 AM -


By suchita
Write a program to display a star using c#.
Write a program to display a star using c#. 9/7/2010 9:18:32 AM - By suchita
plz help me out..
Loops in C# with examples - while, do while, for, foreach 9/10/2010 8:36:29 AM -
By s
s
Loops in C# with examples - while, do while, for, foreach 9/17/2010 8:56:46 AM -
By vinod
what is the main difference while &do while loops?
Loops in C# with examples - while, do while, for, foreach 9/18/2010 5:39:54 AM -
By sunil kumar
sir ,
1.i want to find out add and divison without using '-'and,'/'operators.
2.check given number is odd or enen without using '%' operator.

what is the code for this output. reply ASAP.... 9/20/2010 10:20:50 AM - By jobe
l
@@@@@ @ @ @ @ @ @ @@@@@
Loops in SQL Server2005 with examples - while, do while, for, foreach 9/24/2010
6:04:03 AM - By S.Rramakrishnan
Write a statement that can find out and display each number that is divisible by
3, between 1 and 30.
Loops in C# with examples - while, do while, for, foreach 9/25/2010 4:59:07 AM -
By prashant purwar
respected sir ,
thank you for u this . and thank you for pyramid example. solve my program in
your example.
Loops in C# with examples - while, do while, for, foreach 9/25/2010 11:40:52 AM
- By neha
how to print name neha in stars
like this
* * * * * * * *
** * * * * *
* * * * * * * * * ***
* ** * * * * *
* * * * * * * * *

Loops in C# with examples - while, do while, for, foreach 9/28/2010 4:11:25 AM -


By harish
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 how write the program in C#.net & VB.NET

write a programme to print pyramid . 9/29/2010 1:50:30 PM - By nikhil


Loops in C# with examples - while, do while, for, foreach 10/4/2010 3:47:09 PM -
By Leslie
1
2
3
4
5
5
4
3
2
1
Send me an example code for the above output

Loops in C# with examples - while, do while, for, foreach 10/6/2010 1:29:19 AM -


By bineet
//c program to printing the following pattern #include int main() { int i,j; for
(i=1;i<=5;i++) { for(j=1;j<=i;j++) {printf("%d",j);} printf("\n"); } return(0);
} /* output: 1 12 123 1234 12345
Loops in C# with examples - while, do while, for, foreach 10/11/2010 11:49:41 PM
- By Firoz Raza
Hi
I want to print out the pyramid as below type shape:
A
ABC
ABCDE
ABC
A
&
Like arrow:-
*
* *
* * * *
* *
* *
* *
* * * *

INTRODUCTION:
The object oriented programming will give the impression very unnatural to a pro
grammer with a lot of procedural programming experience. In Object Oriented prog
ramming Encapsulation is the first pace. Encapsulation is the procedure of cover
ing up of data and functions into a single unit (called class). An encapsulated
object is often called an abstract data type. In this article let us see about i
t in a detailed manner.
NEED FOR ENCAPSULATION:
The need of encapsulation is to protect or prevent the code (data) from accident
al corruption due to the silly little errors that we are all prone to make. In O
bject oriented programming data is treated as a critical element in the program
development and data is packed closely to the functions that operate on it and p
rotects it from accidental modification from outside functions.
Encapsulation provides a way to protect data from accidental corruption. Rather
than defining the data in the form of public, we can declare those fields as pri
vate. The Private data are manipulated indirectly by two ways. Let us see some e
xample programs in C# to demonstrate Encapsulation by those two methods. The fir
st method is using a pair of conventional accessor and mutator methods. Another
one method is using a named property. Whatever be the method our aim is to use t
he data with out any damage or change.
ENCAPSULATION USING ACCESSORS AND MUTATORS:
Let us see an example of Department class. To manipulate the data in that class
(String departname) we define an accessor (get method) and mutator (set method).
using system;
public class Department
{
private string departname;
.......
// Accessor.
public string GetDepartname()
{
return departname;
}
// Mutator.
public void SetDepartname( string a)
{
departname=a;
}
}

Like the above way we can protect the private data from the outside world. Here
we use two separate methods to assign and get the required data.
public static int Main(string[] args)
{
Department d = new Department();
d.SetDepartname("ELECTRONICS");
Console.WriteLine("The Department is :"+d.GetDepartname());
return 0;
}
In the above example we can't access the private data departname from an object
instance. We manipulate the data only using those two methods.
ENCAPSULATION USING PROPERTIES:
Properties are a new language feature introduced with C#. Only a few languages s
upport this property. Properties in C# helps in protect a field in a class by re
ading and writing to it. The first method itself is good but Encapsulation can b
e accomplished much smoother with properties.
Now let's see an example.
using system;
public class Department
{
private string departname;
public string Departname
{
get
{
return departname;
}
set
{
departname=value;
}
}
}
public class Departmentmain
{
public static int Main(string[] args)
{
Department d= new Department();
d.departname="Communication";
Console.WriteLine("The Department is :{0}",d.Departname);
return 0;
}
}
From the above example we see the usage of Encapsulation by using properties. Th
e property has two accessor get and set. The get accessor returns the value of t
he some property field. The set accessor sets the value of the some property fie
ld with the contents of "value". Properties can be made read-only. This is accom
plished by having only a get accessor in the property implementation.
READ ONLY PROPERTY:
using system;
public class ReadDepartment
{
private string departname;
public ReadDepartment(string avalue)
{
departname=avalue;
}
public string Departname
{
get
{
return departname;
}
}
}
public class ReadDepartmain
{
public static int Main(string[] args)
{
ReadDepartment d= new ReadDepartment("COMPUTERSCIENCE");
Console.WriteLine("The Department is: {0}",d.Departname);
return 0;
}
}
In the above example we see how to implement a read-only property. The class Rea
dDepartment has a Departname property that only implements a get accessor. It le
aves out the set accessor. This particular class has a constructor, which accept
s a string parameter. The Main method of the ReadDepartmain class creates a new
object named d. The instantiation of the d object uses the constructor of the Re
adDepartment that takes a string parameter. Since the above program is read-only
, we cannot set the value to the field departname and we only read or get the va
lue of the data from the field. Properties can be made also Write-only. This is
accomplished by having only a set accessor in the property implementation.
WRITE ONLY PROPERTY:
using system;
public class WriteDepartment
{
private string departname;
public string Departname
{
set
{
departname=value;
Console.WriteLine("The Department is :{0}",departname);
}
}
}
public class WriteDepartmain
{
public static int Main(string[] args)
{
WriteDepartment d= new WriteDepartment();
d.departname="COMPUTERSCIENCE";
return 0;
}
}
In the above example we see how to implement a Write-only property. The class Wr
iteDepartment has now has a Departname property that only implements a set acces
sor. It leaves out the get accessor. The set accessor method is varied a little
by it prints the value of the departname after it is assigned.
CONCLUSION:
The Encapsulation is the first footstep towards the object-oriented programming.
This article gives you a little bit information about Encapsulation. Using acce
ssor and mutator methods we can make encapsulation. Another one method is using
a named property. The benefit of properties is that the users of your objects ar
e able to manipulate the internal data point using a single named item.

Vous aimerez peut-être aussi