P&L Review 3

Loops

  • Loops
    • Allows repeated execution of a statement or block of statements (within curly braces)
  • Types
    • While loop – used to execute the statements 0 or more times, depending on whether the loop control condition is true or false.
      • while (userEntry != “quit”)
      • while (contStr == “Y”)
      • while (counter < userCount)
      • while (num < 999)
    • Do loop – used when you need to ensure that the statements execute at least one time
      • uses the same type of loop control conditions as while loops
      • loop control conditions is tested at the bottom of the loop
    • For loop – used to execute the statements when you know how many times you want the loop to execute.
      • the three sections of the for loop initialize, test and increment the loop control variable
      • for (int i = 0; i < count; i++)
      • for (int j = 30; j > 0; j- -)
      • foreach (element in myArray) – variation used with an array

Loop control

  • Loop control variables must be:
    • initialized before you enter the loop (either by coding an initial value, or asking the user to input a value)
    • tested at the beginning or end of the loop
    • altered in the loop body (incremented, decremented, asking the user to respond)

Understand how a loop updates the loop control variable

  • a loop that never ends is called an infinite loop
  • a loop placed within another loop is said to be a nested loop
  • Example
[code language=”csharp”]

for (int i = 0; i < 5; i++)

Console.Write(“{0} “, i);

[/code]

output would be 0 1 2 3 4

  • Be able to write a simple loop
  • Example – write a loop that outputs “Happy Halloween” 6 times.
[code language=”csharp”]

for (int j = 0; j < 6; j++)

Console.WriteLine(“Happy Halloween”);

[/code]

Basic facts about coding

  • Variables must be assigned a value or initialized before you can use them in processing or output.
  • When reading data from the Console, you use Console.ReadLine(), which returns data of type string.
  • To input numbers the string returned by Console.ReadLine() must be converted to an integer or double as required by your variable. The methods of the Convert class can be used to do this.
  • Everything in C# is case sensitive. That means that an uppercase C and a lowercase c are not the same thing.
  • Arrays are an ordered group of variables, all of the same data type.
  • if statements and while statements are both followed by Boolean conditions in parentheses, for example (a == b), (c < count),  (exam >=80).
  • These types of conditions can be combined using && (and) and || (or),
    • if ((gpa >= 3.0) && (exam >= 60)) is only true when both conditions are true
    • if ((temp <= 32) || (heater == true)) is true if either or both of the conditions are true
  • Use parentheses in Boolean and Mathematical expressions to be certain that the evaluation of the expression performs as expected.

Windows Form and Object-oriented Programming

  • Classes are used to model real-world objects, example, Employee or StudentRecord
  • Objects are specific instances of a class.
  • A class defines the attributes or properties of the objects in the class.
  • A class defines the methods that can be used to manipulate the objects in the class.
  • Windows Form Applications use a form designed by the developer for input and output, instead of using a Console Window.
  • Windows Form Applications are event-driven, such as a button click or a selection on a radio button or checkbox.
  • Typical controls put on a form include buttons, labels, checkboxes, radio buttons and text boxes. Items on a form can be grouped together.
  • Each control on the form has properties, that can be set when the form is designed and then manipulated in the code.
  • Program logic is put into the methods that execute when an event takes place.