Writing Conditionals and Practicing TDD. TDD Refresher RED – write the test and watch it fail – Why do we watch it fail? GREEN – write the simplest code.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Marking Schema question1: 40 marks question2: 40 marks question3: 20 marks total: 100 marks.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Pseudocode and Algorithms
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2: Input, Processing, and Output
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
Introduction to C Programming
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Adding Automated Functionality to Office Applications.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Java Programming: From The ground Up  Chapter 4 Selection and Decision: if Statements.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 5: Structured Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Java Programming Fifth Edition Chapter 5 Making Decisions.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
TESTING FUNDAMENTALS BY K.KARTHIKEYAN.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
31/01/ Selection If selection construct.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Statements: Part1  if, if…else, switch 1.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
1 Structure of Simple C++ Program Chapter 1 09/09/13.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 4: Making Decisions.
Numbering System TODAY AND TOMORROW 11th Edition
Chapter 4 – Control Structures Part 1
Software Engineering Lecture #13.
Software Engineering Lecture #12.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Visual Basic – Decision Statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Program Flow.
Selection Structures: Single-alternative
Controlling Program Flow
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Topics Introduction to Repetition Structures
Unit 3: Variables in Java
REPETITION Why Repetition?
Presentation transcript:

Writing Conditionals and Practicing TDD

TDD Refresher RED – write the test and watch it fail – Why do we watch it fail? GREEN – write the simplest code that can make it pass REFACTOR – cleanup – comments – variable names – eliminate duplication (pay attention to duplication between the values the test knows and the values the code knows)

Chapter Project This chapter’s project is a tax calculator You will write a series of methods that make a series of tests pass. The goal is to practice writing conditional statements

If Statements Used when we want something to happen only under certain conditions if (potatoNumber != 4) { System.out.print(" potato"); } condition then block

if-then-else Statements Used when we want to choose between two different behaviors long result = oneBack + twoBack; if (result < oneBack) { oneBack = 1; twoBack = 0; result = 1; } else { twoBack = oneBack; oneBack = result; } else block then block condition

Why have the curly brackets? Exactly one statement after the condition is considered to be inside the condition Those brackets make one statement out of a sequence of statements if (potatoNumber != 4) { System.out.print(" potato"); halfway = true; }

Watch the semi-colon What will this code do? if (potatoNumber != 4); { System.out.print(" potato"); } notice the semi-colon That semi-colon ends the one statement in the then block, so the rest of the code is NOT part of the if statement (and will always be executed).

Testing Conditionals Clearly, we cannot test every value that a system might see, so we need to target our tests on values that are likely to cause errors. Border case - A situation where a small change in input causes a fundamentally different behavior. Focus the tests on the values around that situation because those are the values where we are most likely to have a defect

Nested Conditionals if (grade > 90) { System.out.println("Excellent"); } else { if (grade > 80) { System.out.println("Average"); } else { System.out.println("Poor"); } What is the output for 95? 85? 75? 90? 80? What are the border cases?