Sunday

C# If condition tutorial examples.

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().
static void Main() {
Console.WriteLine("A simple C# program.");
}
}

listing 2
// This version does not include "using System;".



class Example {

// A C# program begins with a call to Main().
static void Main() {

// Here, Console.WriteLine is fully qualified.
System.Console.WriteLine("A simple C# program.");
}
}

listing 3
// This program demonstrates variables.

using System;

class Example2 {
static void Main() {
int x; // this declares a variable
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);
}
}

listing 4
/*
This program illustrates the differences
between int and double.
*/

using System;

class Example3 {
static void Main() {
int ivar; // this declares an int variable
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);
}
}

listing 5
// Compute the area of a circle.

using System;

class Circle {
static void Main() {
double radius;
double area;

radius = 10.0;
area = radius * radius * 3.1416;

Console.WriteLine("Area is " + area);
}
}

listing 6
// Demonstrate the if.

using System;

class IfDemo {
static void Main() {
int a, b, c;

a = 2;
b = 3;

if(a < b) Console.WriteLine("a is less than b"); // 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");
}
}

listing 7
// Demonstrate the for loop.

using System;

class ForDemo {
static void Main() {
int count;

for(count = 0; count < 5; count = count+1)
Console.WriteLine("This is count: " + count);

Console.WriteLine("Done!");
}
}

listing 8
// Demonstrate a block of code.

using System;

class BlockDemo {
static void Main() {
int i, j, d;

i = 5;
j = 10;

// The target of this if is a block.
if(i != 0) {
Console.WriteLine("i does not equal zero");
d = j / i;
Console.WriteLine("j / i is " + d);
}
}
}

listing 9
// Compute the sum and product of the numbers from 1 to 10.

using System;

class ProdSum {
static void Main() {
int prod;
int sum;
int i;

sum = 0;
prod = 1;

for(i=1; i <= 10; i++) {
sum = sum + i;
prod = prod * i;
}
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Product is " + prod);

}
}

listing 10
// Demonstrate an @ identifier.

using System;

class IdTest {
static void Main() {
int @if; // use if as an identifier

for(@if = 0; @if < 10; @if++)
Console.WriteLine("@if is " + @if);
}
}

No comments:

Post a Comment