Flylib.com

Books Software

 
 
 

Beginning Visual C#supAND#174;/sup 2005 - page 34


Summary

In this chapter, you have developed your programming knowledge by considering various structures that you can use in your code. The proper use of these structures is essential when you start making more complex applications, and you will see them time and again throughout this book.

First, you spent some time looking at Boolean logic, with a bit of bitwise logic thrown in for good measure. Looking back on this after working through the rest of the chapter confirms the initial assumption that this topic is very important when it comes to implementing branching and looping code in your programs. It is essential to become very familiar with the operators and techniques detailed in this section.

Branching enables you to conditionally execute code, which, when combined with looping, allows you to create convoluted structures in your C# code. When you have loops inside loops inside if structures inside loops, you start to see why code indentation is so useful! If you shift all your code to the left of the screen, it instantly becomes difficult to parse by eye, and even more difficult to debug. It is well worth making sure you've got the hang of indentation at this stage — you'll appreciate it later on! VS does a lot of this for you, but it's a good idea to indent code as you type it anyway.

In this chapter, you learned:

  • How to use Boolean logic

  • Techniques for structuring your code

In the next chapter, you look at variables in more depth.



Exercises

  1. If you have two integers stored in variables var1 and var2 , what Boolean test can you perform to see if one or the other (but not both) is greater than 10?

  2. Write an application that includes the logic from Exercise 1, obtains two numbers from the user , and displays them, but rejects any input where both numbers are greater than 10 and asks for two new numbers .

  3. What is wrong with the following code?

    
    int i;
    
    
    for (i = 1; i <= 10; i++)
    
    
    {
    
    
    if ((i % 2) = 0)
    
    
    continue;
    
    
    Console.WriteLine(i);
    
    
    }
    
    
  4. Modify the Mandelbrot set application to request image limits from the user and display the chosen section of the image. The current code outputs as many characters as will fit on a single line of a console application; consider making every image chosen fit in the same amount of space to maximize the viewable area.



Chapter 5: More about Variables

Overview

Now that you've seen a bit more of the C# language, it's time to go back and tackle some of the more involved topics concerning variables.

The first topic you look at in this chapter is type conversion , where you convert values from one type into another. You've already seen a bit of this, but you look at it formally here. A grasp of this topic gives you a greater understanding of what happens when you mix types in expressions (intentionally or unintentionally) and tighter control over the way that data is manipulated. This helps you to streamline your code and avoid nasty surprises .

Once you've covered this, you will look at a few more types of variable that you can use:

  • Enumerations: Variable types that have a user -defined discrete set of possible values that can be used in a human-readable way

  • Structs: Composite variable types made up of a user-defined set of other variable types

  • Arrays: Types that hold multiple variables of one type, allowing index access to the individual values

These are slightly more complex than the simple types you've been using up to now, but they can make your life much easier.

Once you've covered these topics, you look at another useful subject concerning strings — basic string manipulation.