Section A.5. Chapter 5: Branching


A.5. Chapter 5: Branching

A.5.1. Quiz



Solution to Question 51 .

if , else , switch .



Solution to Question 52 .

False. In C#, an if statement's condition must evaluate to a Boolean expression.



Solution to Question 53 .

The braces make maintenance easier. If you add a second statement later, you are less likely to create a logic error because it is obvious what "block" of statements the if refers to.



Solution to Question 54 .

Either a numeric value or a string.



Solution to Question 55 .

False. If the statement has no body, then you can fall through. For example:

 case morning: case afternoon:       someAction(  );       break; 



Solution to Question 56 .

Two uses of goto are:

  • To go to a label in your code

  • To go to a different case statement in a switch statement



Solution to Question 57 .

do...while evaluates its condition is at the end of the statement rather than at the beginning, and thus is guaranteed to run at least once.



Solution to Question 58 .

In a loop, it causes the remainder of the body of the loop to be skipped and the next iteration of the loop to begin immediately.



Solution to Question 59 .

Two ways of creating an infinite loop are:

 for (;;) while(true) 

A.5.2. Exercises



Solution to Exercise 5-1 .

Create a method that counts from 1-10 using each of the while , do...while , and for statements.

 using System; class Exercises {    static void Main(  )    {       Console.WriteLine( "while" );       int counter = 1;       while ( counter <= 10 )       {          Console.Write( counter );          if ( counter < 10 )          {             Console.Write( ", " );          }          ++counter;       }       Console.WriteLine( "\nDo..while" );       counter = 1;       do       {          Console.Write( counter );          if ( counter < 10 )          {             Console.Write( ", " );          }          ++counter;       } while ( counter <= 10 );       Console.WriteLine( "\nfor" );       for ( int ctr = 1; ctr <= 10; ctr++ )       {          Console.Write( counter );          if ( counter < 10 )          {             Console.Write( ", " );          }       }       Console.WriteLine( "\nDone" );    } } 



Solution to Exercise 5-2 .

Create a program that evaluates whether a given input is odd or even, a multiple of 10, or too large (over 100) by using four levels of if statement. Then expand the program to do the same work with a switch statement.

 using System; class Exercises {     enum numericCondition     {         even,         multiple,         odd,         tooBig,         unknown,         zero,     };     static void Main()    {           // possible input conditions        while ( true )       {          // entering any symbol except a number will throw an exception          // -- no error handling in this simple example          Console.Write( "Enter a number please: " );          string theEntry = Console.ReadLine();          int theNumber = Convert.ToInt32(theEntry) ;          Console.Write( "NestedIf {0}: ", theNumber );          // Logic: if the number is greater than 100, say it is too big          // if it is even but not a multiple of 10 say it is even          // if it is a multiple of ten, say so          // if it is not even, say it is odd          if ( theNumber <= 100 )          {            if ( theNumber % 2 == 0 )            {               if ( theNumber == 0 )               {                  Console.WriteLine( "zero is not even or odd nor a multiple of 10" );               }               else               {                  if ( theNumber % 10 == 0 )                  {                     Console.WriteLine( "You have picked a multiple of 10" );                  }                  else                  {                     Console.WriteLine( "Your number is even" );                  }  // end else not a multiple of 10               }  // end else not zero            }  // end if even            else            {               Console.WriteLine( "What an odd number to enter" );            }         }  // end if not too big         else         {            Console.WriteLine( "Your number is too big for me." );         }         Console.Write( "SwitchMethod {0}: ", theNumber  );    // same logic, different implementation       // set the enumerated condition       numericCondition condition = numericCondition.unknown;  // initialize       condition = ( theNumber  % 2 == 0 ) ?             numericCondition.even : numericCondition.odd;       if ( theNumber % 10 == 0 ) condition = numericCondition.multiple;       if ( theNumber == 0 ) condition = numericCondition.zero;       if ( theNumber > 100 ) condition = numericCondition.tooBig;       // switch on the condition and display the correct message       switch ( condition )       {          case numericCondition.even:             Console.WriteLine( "Your number is even" );             break;          case numericCondition.multiple:             Console.WriteLine( "You have picked a multiple of 10" );             break;          case numericCondition.odd:             Console.WriteLine( "What an odd number to enter" );             break;          case numericCondition.tooBig:             Console.WriteLine( "Your number is too big for me." );             break;          case numericCondition.zero:             Console.WriteLine( "zero is not even or odd nor a multiple of 10" );             break;          default:             Console.WriteLine( "I'm sorry, I didn't understand that." );             break;       }    }   } } 



Solution to Exercise 5-3 .

Create a program that initializes a variable i at 0 and counts up, and initializes a second variable j at 25 and counts down. Use a for loop to increment i and decrement j simultaneously . When i is greater than j , end the loop and print out the message "Crossed over!".

 using System; class Exercises {    static void Main(  )    {       int i = -1;       int j = -1;       for (i = 0, j = 25; i < j; ++i, --j )       {          Console.WriteLine("i: {0}; j: {1}", i, j);       }       Console.WriteLine( "Crossed over! i: {0}; j: {1}", i, j );    } } 

The output looks like this:

 i: 0; j: 25 i: 1; j: 24 i: 2; j: 23 i: 3; j: 22 i: 4; j: 21 i: 5; j: 20 i: 6; j: 19 i: 7; j: 18 i: 8; j: 17 i: 9; j: 16 i: 10; j: 15 i: 11; j: 14 i: 12; j: 13 Crossed over! i: 13; j: 12 



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net