0
Loop in c#
how can we have the result of x is 1 to 5 when we initialize the x = 1 and the condition of x is < 5?
2 odpowiedzi
0
int x = 1;
while(x<=5) //This will print 1 to 5, use the < operator if you want 1 to 4
{
Console.WriteLine(x)
x++
}
// or you could place the iterator in a different position and do
// however this would mean starting from 0
int x = 0;
while(x < 5)
x++
Console.WriteLine(x)
Hope this helps
0
if you declare x before the for loop its value is accessible after the for loop . the for loop will terminate when x becomes 5 and only print up to 4 but the WriteLine after the for loop will print 5 as illustrated below.
int x=0;
for(x=1;x<5;x++)
{
Console.WriteLine(x);
}
Console.WriteLine(x);