Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.

Similar presentations


Presentation on theme: "Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4."— Presentation transcript:

1 Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4

2 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1  Comments are very important in computer programming.  Comments are a plain natural language (English) description attached to a chunk of code to explain what the code does.  Comments are not executed by the computer. They are intended to make code more readable. Why? For whom? Comments

3 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng2  Comments are very important in computer programming.  Comments are a plain natural language (English) description attached to a chunk of code to explain what the code does.  Comments are not executed by the computer. They are intended to make code more readable. Comments To document rationale/intent & highlight difficulties: document inputs/outputs & in general, identifiers For ALL users - including the developer(s)

4 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng3 Comments  In visual studio, code that the compiler considers to be a comment is written in an ugly shade of green.  If code that shouldn't be a comment is this colour, there's a problem with your syntax.

5 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng4 //Assign value to textbox TextBox1.Text = "Comment Example"; a = x; //Comment on same line as code!! Comments  There are two syntaxes for comments that can be used in C#.  First - the text between two //'s and the end of a line is a comment.

6 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng5 /* This is my super long comment. "Comment Example" is getting written to TextBox1. TextBox1 is an output textbox that should have been given a better name */ TextBox1.Text = "Comment Example"; Comments  Second - multiple line comments can be written between /* and */

7 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng6 Comments  Visual studio has a tool to make lines into a comment.  Highlighting a section, and clicking the Comment out Selected Lines button turns entire lines into comments.

8 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng7 Comments  Highlighting a section and clicking the Uncomment the selected lines button will remove the comment marks put on by the other button.

9 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng8 Comments  It's important to write a modest amount of comments.  Too many comments clutter the code.  Too few makes it very difficult to understand what code does.  Do not simply restate what is obvious from reading the code  Comment on what (interface) not on how (implementation)

10 Conditional Statements Continued © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng9

11 10 double pnAbsSlope(double x1, double y1, double x2, double y2) { double slope = (y2 - y1) / (x2 – x1); if (slope > 0) textBox1.Text = "Positive Slope"; else { textBox1.Text = "Negative Slope"; slope = -slope; } return slope } Recap: If Statements

12 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng11 The Else-If  There are a few more variations of the if- else statement.  What if we want our slope calculator to also output “Zero slope” if the slope is zero?

13 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng12 double pnAbsSlope(double x1, double y1, double x2, double y2) { double slope = (y2 - y1) / (x2 – x1); if (slope > 0) textBox1.Text = "Positive Slope"; else if (slope < 0) { textBox1.Text = "Negative Slope"; slope = -slope; } else textBox1.Text = "Zero Slope"; return slope; } Positive/Negative Slope

14 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng13 if (i > 5) doIt2("Greater than 5”); else if (i > 2) doIt2("Greater than 2"); else if (i > -25) doIt2("Greater than -25"); else doIt2("A Very Small Number"); Positive/Negative Slope  If i = 30, which branch(es) will be run?

15 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng14 if (i > 5) doIt2("Greater than 5"); else if (i > 2) doIt2("Greater than 2"); else if (i > -25) doIt2("Greater than -25"); else doIt2("A Very Small Number");  Only the first one. Positive/Negative Slope  If i = 30, which branch(es) will be run?

16 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng15 Positive/Negative Slope  Only one branch can be entered in an if- else statement even if the condition evaluates to true for more than one if / else-if.  In this way, an else-if is different from multiple if statements.

17 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng16 int absolute (int value) { ….? } Absolute Value  The last version of an if statement we are going to look at is the “lonely if”.  Example: Build a method that takes an integer and returns its absolute value.

18 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng17  There is no else branch for this if. int absolute (int value) { if (value < 0) value = -value; return value; } Absolute Value

19 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng18  If statements can be written without else branches.  Else branches will bind to the closest if statement unless brackets say otherwise. The Lonely If

20 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng19  Does this code work? void foo(int input) { int i; if (input > 5) { i = 42; } Console.WriteLine(Convert.ToString(i)); } Common Mistakes

21 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng20  Does this code work? void foo(int input) { int i; if (input > 5) { i = 42; } Console.WriteLine(Convert.ToString(i)); } Common Mistakes

22 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng21  What does i equal if input is < 5? void foo(int input) { int i; if (input > 5) { i = 42; } Console.WriteLine(Convert.ToString(i)); } Common Mistakes

23 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng22  We have not convinced the compiler that the variable i will always have a value before we use it.  We can solve this by initializing it. void foo(int input) { int i = 0; if (input > 5) { i = 42; } Console.WriteLine(Convert.ToString(i)); } Common Mistakes aside: what do you think this method does?

24 Loops © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng23

25 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng24 Pyramid of Golf Balls  You are going to build a pyramid with golf balls. It needs to be 3 layers high. How many golf balls do you need?

26 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng25 Pyramid of Golf Balls Algorithm  We need to add the number of golf balls in each level to find our solution. 12122 3232 ++

27 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng26 int buildPyramid () { return (1 + 4 + 9); } Pyramid of Golf Balls

28 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng27 Pyramid of Golf Balls  What happens if we want to build a pyramid 25 layers tall?  That’s a lot of numbers to manually type into a computer program or calculator.  How do we repeat something until a condition is reached?

29 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng28 While Loop while (condition) { Loop Body Statements }  The condition gets evaluated at the beginning of the loop every time through the loop.  The body of the loop keeps running until the condition becomes false.

30 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng29 int i = 0; while(i < 4) { i = i + 1; } 0 i While Loop

31 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng30 int i = 0; while(i < 4) { i = i + 1; } 0 i 0 < 4 True While Loop

32 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng31 int i = 0; while(i < 4) { i = i + 1; } 1 i While Loop

33 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng32 int i = 0; while(i < 4) { i = i + 1; } 1 i 1 < 4 True While Loop

34 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng33 int i = 0; while(i < 4) { i = i + 1; } 2 i While Loop

35 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng34 int i = 0; while(i < 4) { i = i + 1; } 2 i 2 < 4 True While Loop

36 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng35 int i = 0; while(i < 4) { i = i + 1; } 3 i While Loop

37 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng36 int i = 0; while(i < 4) { i = i + 1; } 3 i 3 < 4 True While Loop

38 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng37 int i = 0; while(i < 4) { i = i + 1; } 4 i While Loop

39 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng38 int i = 0; while(i < 4) { i = i + 1; } 4 i 4 < 4 False While Loop

40 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng39 int i = 0; while(i < 4) { i = i + 1; } 4 i Done While Loop

41 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng40 int i = 0; while(i < 4) { i = i + 1; } int i = 0; while(i < 4) { i++; } aside: Short Form for Increment

42 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng41 int i = 0; while(i < 4) { i = i + 1; } int i = 0; while(i < 4) { i++; } aside: Short Form for Increment or Decrement i = i - 1; i--;

43 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng42 Layer 1 Layer 2 Layer 3 Pyramid of Golf Balls  Let's give the layers in our pyramid numbers. The top layer is given the number 1, the second layer from the top is given the number 2, etc.

44 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng43 1 2 + 2 2 + 3 2 +…+ 25 2 Pyramid of Golf Balls  If we square the layer number, we have the total number of golf balls in that layer.  We have to loop through all the layers between 1 and 25 and add the numbers of balls in each layer to our total sum.

45 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng44 Pyramid of Golf Balls 0 totalGolfBalls 1 layer 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2

46 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng45 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 1 layer 1 totalGolfBalls

47 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng46 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 2 layer 1 totalGolfBalls

48 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng47 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 2 layer 5 totalGolfBalls

49 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng48 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 3 layer 5 totalGolfBalls

50 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng49 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 3 layer 14 totalGolfBalls

51 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng50 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2 & 3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 4 layer 14 totalGolfBalls

52 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng51 Pyramid of Golf Balls  Don't underestimate the importance of the first step: Start at the Top.  It is very important to initialize variables before starting a loop.  In our case, Start at the Top means:  No golf balls have been counted yet. totalGolfBalls = 0;  We are starting on the first layer: layer = 1

53 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng52 Pyramid of Golf Balls 1.Start at the top. 2.Add all the golf balls from the current layer. 3.Move to the next layer. 4.Repeat steps 2&3 until 25 layers of golf balls have been added. 1 2 + 2 2 + 3 2 +…+ 25 2 0 totalGolfBalls 1 layer

54 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng53 int buildPyramid () { int layer = 1; int totalGolfBalls = 0; while(layer <= 25) { totalGolfBalls = totalGolfBalls + layer*layer; layer++; } return totalGolfBalls; } Pyramid of Golf Balls

55 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng54 Pyramid of Golf Balls  Some people take a more mathematical approach.  The number of golf balls in a pyramid can be written with summation notation:  We can then convert directly from summation notation to C# code.

56 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng55 int buildPyramid () { int i = 1; int sum = 0; while(i <= 25) { sum = sum + i*i; i++; } return sum; } Pyramid of Golf Balls see any similarity with the previous version?

57 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng56 Alternative Loops  The while loop is the most commonly used and simplest loop in C#  This program could also be written with a for loop.

58 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng57 int buildPyramid () { int i; int sum = 0; for(i = 1; i <= 25; i++) { sum = sum + i*i; } return sum; } Pyramid of Golf Balls

59 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng58 For Loop for (initialization; condition; advance) { Loop Body Statements }  Lets examine the for loop by using an example.

60 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng59  A loop condition variable is assigned a starting value within the initialization section of the loop.  This variable will be used to control the loop. for (i = 0; i < 5; i++) { //Do something inside } For Loops in More Depth

61 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng60 For Loops in More Depth  The condition is dependent on the initialization and advance.  The condition is checked at the beginning of the loop. for (i = 0; i < 5; i++) { //Do something inside }

62 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng61 For Loops in More Depth  The advance statement gets executed at the end of the loop.  It’s used to advance the loop condition variable. for (i = 0; i < 5; i++) { //Do something inside }

63 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng62 for (i = 0; i < 5; i++) { //Do something inside } i = 0; while (i < 5) { //Do something i++; } For Loops in More Depth  For loops can always be transformed into while loops.

64 Another Alternative  Another option for loops in C# is the do while loop  The while and for loops are pretest loops  The do while loop is a posttest loop © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng63 do { statement; } while (conditional expression)

65 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng64 Loop Comparisons  For loops, while loops and do loops, can always be interchanged. However, they each have their own purpose.  For loops are normally used for repeating something a set number of times. Like the pyramid builder program  While loops are normally used as sentinel control loops, i.e. looping until a condition is satisfied  Do loops are used when the statement is executed at least once

66 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng65 Pyramid of Golf Balls  Regardless of how you solve the problem, there are 5525 golf balls required for a 25 level pyramid.


Download ppt "Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4."

Similar presentations


Ads by Google