Académique Documents
Professionnel Documents
Culture Documents
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
}
}
}
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
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);
}
}
}
}
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
Q2. A I
B H
C G
D F
E
Q3. 1
12
124
1246
12468
1357
135
13
1
Q4. * * * * * * *
* * *
*
Q5. []
Q6. X
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
Thanks
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
*
* *
* * *
* * * *
* * * * *
1
2 2
3 3 3
4 4 4 4
*
* *
* * *
* * * *
* * * * *
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
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
Your program should consist of comments to the program and a snapshot of the out
put.
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
* * * * * * * *
** * * * * *
* * * * * * * * * ***
* ** * * * * *
* * * * * * * * *
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.