Chapter 5. Getting Into the Program Flow

In This Chapter

  • Making decisions if you can

  • Deciding what else to do

  • Looping without going in a circle

  • Using the while and do . . . while loops

  • Using the for loop and understanding scope

Consider this simple program:

using System;
namespace HelloWorld
  public class Program
    // This is where the program starts.
    static void Main(string[] args)
      // Prompt user to enter a name.
      Console.WriteLine("Enter your name, please:");
      // Now read the name entered.
      string name = Console.ReadLine();
      // Greet the user with the entered name.
      Console.WriteLine("Hello, " + name);
      // Wait for user to acknowledge the results.
      Console.WriteLine("Press Enter to terminate . . . ");

Beyond introducing you to a few fundamentals of C# programming, this program is almost worthless. It simply spits back out whatever you entered. You can imagine more complicated program examples that accept input, perform some type of calculations, generate some type of output (otherwise, why do the calculations?), and then exit at the bottom. However, even a program such as this one can be of only limited use.

One key element of any computer processor is its ability to make decisions. When I say "make decisions," I mean that the processor sends the flow of execution down one path of instructions if a condition is true or down another path if the condition is not true. Any programming language must offer this fundamental capability to control the flow of execution.

The three ...

Get C# 2010 All-in-One For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.