Methods and Conditions

Methods let you name a piece of work and reuse it instead of repeating code, and conditions let your program make decisions. You will organize code into methods with parameters and return values, then branch with if, else, switch, and switch expressions.

Updated

Introduction to Methods

  1. Introduction to Methods: an overview (Novice): A method is a named block of reusable code; you've been calling them since your first program.
  2. Practice: The Need for Methods (Novice): Plan a program that prints college football scores in pseudo-code, extend it by copying and pasting its code, and see the problems copying causes.
  3. Organizing Code into Methods (Apprentice): Write your own C# methods with parameters and return values, call them with arguments, and keep each one to a single job.
  4. Practice: Creating Methods (Apprentice): Replace the football program's copied blocks with one method that takes each team's scores as parameters.
  5. Passing Values to a Method: by Value and by Reference (Apprentice): Learn what a method can and can't change in the code that calls it, passing by value, with ref and out, and passing an array.

Conditional Logic in C#

  1. If (Novice): Run code only when a condition is true, with if, if-else and else if, and write conditions with C#'s comparison operators.
  2. Where a Variable Lives: Scope and Code Blocks (Novice): A variable can be used only inside the block where it's declared, so you'll learn to declare each one where the code that needs it can see it, and how blocks, methods, classes and namespaces nest.
  3. Multiple Conditions (Apprentice): Combine conditions with && (and), || (or) and ! (not), and group them with parentheses so they mean what you intend.
  4. Switch (Apprentice): Pick one of many blocks of code by the value of a single variable with C#'s switch statement, including shared cases and a default.
  5. Switch Expressions and the Conditional Operator (Apprentice): Choose a value, rather than run a block, with C#'s conditional operator for two choices and a switch expression for many.

Exercise: Methods and Conditions

  1. Exercise: Methods and Conditions (Apprentice): Write 15 methods for three small businesses, using parameters, return values, ref and out, and conditions, and check each one with tests.