0% found this document useful (0 votes)
22 views

Whiletest: Using Namespace Class Static Void Int While

The document contains code examples demonstrating the use of various looping and control flow structures in C#, including while, foreach, do-while, for, continue, break, throw, return, and if/else. It provides the code and expected output for each example.

Uploaded by

Triples Henecia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Whiletest: Using Namespace Class Static Void Int While

The document contains code examples demonstrating the use of various looping and control flow structures in C#, including while, foreach, do-while, for, continue, break, throw, return, and if/else. It provides the code and expected output for each example.

Uploaded by

Triples Henecia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

namespace Vonglap
{
class WhileTest
{
static void Main()
{
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}
}
}
/*
Output:
Current value of n is 1
Current value of n is 2
Current value of n is 3
Current value of n is 4
Current value of n is 5
*/
class ForEachTest
{
static void Main(string[] args)
{
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
// Compare the previous loop to a similar for loop.
for (int i = 0; i < fibarray.Length; i++)
{
System.Console.WriteLine(fibarray[i]);
}
System.Console.WriteLine();

}
//
//
//
//

// You can maintain a count of the elements in the collection.


int count = 0;
foreach (int element in fibarray)
{
count += 1;
System.Console.WriteLine("Element #{0}: {1}", count, element);
}
System.Console.WriteLine("Number of elements in the array: {0}", count);
Output:
0
1
1

//
//
//
//
//

2
3
5
8
13

//
//
//
//
//
//
//
//

0
1
1
2
3
5
8
13

//
//
//
//
//
//
//
//
//

Element #1: 0
Element #2: 1
Element #3: 1
Element #4: 2
Element #5: 3
Element #6: 5
Element #7: 8
Element #8: 13
Number of elements in the array: 8

}
public class TestDoWhile
{
public static void Main()
{
int x = 0;
do
{
Console.WriteLine(x);
x++;
} while (x < 5);
}
}
/*
Output:
0
1
2
3
4
*/
class ForLoopTest
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
}
}
/*
Output:
1

2
3
4
5
*/
class ContinueTest
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i < 9)
{
continue;
}
Console.WriteLine(i);
}
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();

}
}
/*
Output:
9
10
*/
class BreakTest
{
static void Main()
{
for (int i = 1; i <= 100; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}

// Keep the console open in debug mode.


Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/*
Output:
1
2
3
4
*/
class ThrowTest2
{
static int GetNumber(int index)
{

int[] nums = { 300, 600, 900 };


if (index > nums.Length)
{
throw new IndexOutOfRangeException();
}
return nums[index];
}
static void Main()
{
int result = GetNumber(3);
}
/*
*/
{

}
Output:
The System.IndexOutOfRangeException exception occurs.
class ReturnTest
static double CalculateArea(int r)
{
double area = r * r * Math.PI;
return area;
}
static void Main()
{
int radius = 5;
double result = CalculateArea(radius);
Console.WriteLine("The area is {0:0.00}", result);

}
}

// Keep the console open in debug mode.


Console.WriteLine("Press any key to exit.");
Console.ReadKey();

class Program
{
static void Main()
{

}
}

Console.WriteLine("so score hien tai : " );


int score = Convert.ToInt32(Console.ReadLine());
if (score == 100)
{ // Any code between the curly braces gets executed by the program only when
// the condition in the parentheses is true.
Console.WriteLine("Perfect score!");
Console.ReadLine();
}
else
Console.WriteLine("you won");
Console.ReadLine();
}

You might also like