Demonstrate the if

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the if.

using System;

public class IfDemo {
public 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"); } } [/csharp]

Determine if a value is positive, negative, or zero

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Determine if a value is positive, negative, or zero.
using System;

public class PosNegZero {
public static void Main() {
int i;

for(i=-5; i <= 5; i++) { Console.Write("Testing " + i + ": "); if(i < 0) Console.WriteLine("negative"); else if(i == 0) Console.WriteLine("no sign"); else Console.WriteLine("positive"); } } } [/csharp]

Determine if a value is positive or negative

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Determine if a value is positive or negative.
using System;

public class PosNeg {
public static void Main() {
int i;

for(i=-5; i <= 5; i++) { Console.Write("Testing " + i + ": "); if(i < 0) Console.WriteLine("negative"); else Console.WriteLine("positive"); } } } [/csharp]

If else for int

image_pdfimage_print

/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;

namespace Client.Chapter_4___Program_Control
{
public class ifelse
{
static void Main(string[] args)
{
int a = 5, b = 5, c = 10;

if (a == b)
Console.WriteLine(a);

if ((a > c) || (a == b))
Console.WriteLine(b);

if ((a >= c) && (b <= c)) Console.WriteLine(c); } } } [/csharp]

the goto statement

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_15.cs illustrates the use of
the goto statement
*/

public class Example4_15
{

public static void Main()
{

int total = 0;
int counter = 0;

myLabel:
counter++;
total += counter;
System.Console.WriteLine(“counter = ” + counter);
if (counter < 5) { System.Console.WriteLine("goto myLabel"); goto myLabel; } System.Console.WriteLine("total = " + total); } } [/csharp]

Goto Tester

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class GotoTester
{

public static void Main()
{
int counterVariable = 0;

repeat: // the label

Console.WriteLine(
“counterVariable: {0}”,counterVariable);

// increment the counter
counterVariable++;

if (counterVariable < 10) goto repeat; // the dastardly deed } } [/csharp]

Demonstrate the goto

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the goto.

using System;

public class Use_goto {
public static void Main() {
int i=0, j=0, k=0;

for(i=0; i < 10; i++) { for(j=0; j < 10; j++ ) { for(k=0; k < 10; k++) { Console.WriteLine("i, j, k: " + i + " " + j + " " + k); if(k == 3) goto stop; } } } stop: Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k); } } [/csharp]