Глава 2
listing 1
/*
This is a simple C# program.
Call this program Example.cs.
*/
using System;
class Example
{
// A C# program begins with a call to Main().
public static void Main()
{
Console.WriteLine("A simple C# program.");
}
}
A simple C# program.
Анализ:
Вверх
A simple C# program.
Анализ:
Вверх
x contains 100
Анализ:
Вверх
listing 4
Original value of ivar: 100
ivar after division: 33
Анализ:
Вверх
radius = 10.0;
Area is 314,16
Анализ:
Вверх
a = 2;
if(a < b) Console.WriteLine("a is less than b");
a is less than b
c contains -1
c contains 1
Анализ:
Вверх
for(count = 0; count < 5; count = count+1)
This is count: 0
Анализ:
Вверх
i = 5;
// the target of this if is a block
i does not equal zero
Анализ:
Вверх
sum = 0;
for(i=1; i <= 10; i++)
{
Sum is 55
Анализ:
Вверх
listing 10
@if is 0
Анализ:
listing 2
// This version does not include the using System statement.
class Example
{
public static void Main()
{
System.Console.WriteLine("A simple C# program.");
Результат:
listing 3
// This program demonstrates variables.
using System;
class Example2
{
int y; // this declares another variable
x = 100; // this assigns 100 to x
Console.WriteLine("x contains " + x);
y = x / 2;
Console.Write("y contains x / 2: ");
Console.WriteLine(y);
Результат:
y contains x / 2: 50
/*
This program illustrates the differences
between int and double.
*/
using System;
class Example3
{
double dvar; // this declares a floating-point variable
ivar = 100; // assign ivar the value 100
dvar = 100.0; // assign dvar the value 100.0
Console.WriteLine("Original value of ivar: " + ivar);
Console.WriteLine("Original value of dvar: " + dvar);
Console.WriteLine(); // print a blank line
// now, divide both by 3
ivar = ivar / 3;
dvar = dvar / 3.0;
Console.WriteLine("ivar after division: " + ivar);
Console.WriteLine("dvar after division: " + dvar);
Результат:
Original value of dvar: 100
dvar after division: 33,3333333333333
listing 5
// Compute the area of a circle.
using System;
class Circle
{
double area;
area = radius * radius * 3.1416;
Console.WriteLine("Area is " + area);
Результат:
listing 6
// Demonstrate the if.
using System;
class IfDemo
{
b = 3;
// this won't display anything
if(a == b) Console.WriteLine("you won't see this");
Console.WriteLine();
c = a - b; // c contains -1
Console.WriteLine("c contains -1");
if(c >= 0) Console.WriteLine("c is non-negative");
if(c < 0) Console.WriteLine("c is negative");
Console.WriteLine();
c = b - a; // c now contains 1
Console.WriteLine("c contains 1");
if(c >= 0) Console.WriteLine("c is non-negative");
if(c < 0) Console.WriteLine("c is negative");
Результат:
c is negative
c is non-negative
listing 7
// Demonstrate the for loop.
using System;
class ForDemo
{
Console.WriteLine("This is count: " + count);
Console.WriteLine("Done!");
Результат:
This is count: 1
This is count: 2
This is count: 3
This is count: 4
listing 8
// Demonstrate a block of code.
using System;
class BlockDemo
{
j = 10;
if(i != 0)
{
d = j / i;
Console.WriteLine("j / i is " + d);
Результат:
j / i is 2
listing 9
// Compute the sum and product of the numbers from 1 to 10.
using System;
class ProdSum
{
int sum;
int i;
prod = 1;
prod = prod * i;
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Product is " + prod);
Результат:
Product is 3628800
// Demonstrate an @ identier.
using System;
class IdTest
{
for(@if = 0; @if < 10; @if++)
Console.WriteLine("@if is " + @if);
Результат:
@if is 1
@if is 2
@if is 3
@if is 4
@if is 5
@if is 6
@if is 7
@if is 8
@if is 9