1 / 32

A Spiral Approach to C/C++

A Spiral Approach to C/C++. Experiments in Reading Code, Writing Code, and Problem Solving Using Test Driven Development. Cooks vs. Chefs. How are cooks trained? learn about ingredients, cooking techniques, and how to follow a recipe What’s the difference with training a chef?

tab
Télécharger la présentation

A Spiral Approach to C/C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Spiral Approach to C/C++ Experiments in Reading Code, Writing Code, and Problem Solving Using Test Driven Development

  2. Cooks vs. Chefs • How are cooks trained? • learn about ingredients, cooking techniques, and how to follow a recipe • What’s the difference with training a chef? • need to be able to create new recipes • need to understand how ingredients interact • need a vision of the culinary experience they want

  3. How Do We Become Chefs? • Learn about the language • Learn about the tools used by engineers • Learn about general, language independent problem-solving strategies • Practice, practice, practice

  4. Algorithms • An algorithm is a sequence of steps that accomplish a particular task • Example for starting a car: • Enter the car and sit in the driver’s seat • Fasten your seatbelt • Adjust the mirrors, steering wheel, and your seat • Put your foot on the brake • Insert the key into the ignition • Turn the key as far as you can • When the car starts, let go of the key

  5. Issues with Specifying Algorithms • Who is the target? Who will follow the steps? • We need the target to understand the algorithm. • It must be in a language that the target understands • It must be specified at a level of detail appropriate to the target • “Adjust the mirrors, steering wheel, and your seat”

  6. Machine Languages • Computers only store ones and zeros, so the instructions they execute are sequences of ones and zeros. • Writing in machine language is very tedious and error prone. • When all else fails, make the computer do the hard part!

  7. Assembly Languages • Each machine instruction has a “name” and we write the program using those mneumonics. • Assemblers are programs that translate programs written in assembly language into machine language • So, in assembly languages, each assembly instruction translates into one machine instruction

  8. Limitations of Assembly Languages • They are machine specific

  9. Higher Level Languages • Language constructs are closer to English • Compilers are programs that translate programs written in higher level languages into machine code. • Compiler is machine specific, but the language in not. • One higher level language instruction may translate into many machine instructions

  10. Multiple Target Machines • One program can be translated by two different compilers to run on two different machines

  11. Limitations of This Strategy • Things compiled for a PC cannot run on a mac. • Makes it really hard to develop web-based solutions! • Companies spend significant resourses porting code because things like graphics may still be operating system specific

  12. Example 1 int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; }

  13. Keywords • Words the compiler understands int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; }

  14. Variables • Places where we store information. • Each variable has • name • it cannot be a keyword • It must start with a letter, or an underscore (_) • It cannot contain any arithmetic operators (+,-, etc.) • it cannot contain any white space and • it must be unique • value • memory location where that value is stored

  15. Declaring a variable • Before we can use a variable, the compilerhas to give it a memory location. We do that by “declaring the variable.” • This instruction must tell the compiler the name of the variable and the type of information the variable will hold so that it can allocate the necessary space in memory.

  16. Declaring a Variable int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; } Declares variable named x These two statements each declare a variable that can hold an integer Declares variable named y

  17. Memory Diagrams • Used to show the state of memory as our program executes x: Space for the value it will hold Name of the variable y:

  18. Storing Values Into Variables • Assignment statements int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; } Assigns x the value 32

  19. x = 32; • Resulting memory diagram: • Notice that we have no idea what value y has yet x: 32 y:

  20. Reading an Assignment Statement • When we see this statement: x = 32; • We read it as: • x is assigned the value 32 or • x gets the value 32 • Do not read it “x equals 32” because that sounds like a comparison. • It’s important to remember that the purpose of an assignment statement is putting a value into a variable

  21. Assignment Statement form • Assignment statements are always of this form: • <variable name> = <expression>; • Executing this statement will cause this sequence of events: • the expression on the right hand side will be evaluated (i.e. it’s value will be computed) • the resulting value will be stored in the variable on the left hand side • Notice that the left and right hand sides of the statement have very different purposes

  22. Another Example int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; } • Evaluates “x+4” so it will get the value stored in x (32) and add 4 to it • Stores the value (36) into y

  23. y = x + 4; • Resulting memory diagram: x: 32 y: 36

  24. Output the Result int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; } Will print out y = 36

  25. cout << “y=“ << y; • Outputs a sequence of characters (which we call a “String”) to the console • Things in quotes are printed exactly as they appear • “y = “ • Things not in quotes are evaluted and the resulting value is output • y • Things are connected using the “<<” operator

  26. Examples • The output from this code: • is cout << 6 + 8 << endl; cout << 6 + 8 << " is fourteen" << endl; cout << 6 << 8 << " is sixty eight" << endl; endl marks the end of one line. Subsequent output will go on the next line 14 14 is fourteen 68 is sixty eight

  27. Doing Something More Than Once • This code • produces this output int count; count = 0; while (count < 4) { cout << count << endl; count = count + 1; } 0 1 2 3

  28. While Loops • This is the general form of a while loop: while (<condition>) { // do something }

  29. Things to Remember About While Loops • The condition is only checked at the beginning of the loop. • If the condition never becomes true, the loop will run forever. We call these “infinite loops”

  30. Simple Conditionals • Sometimes we want to change the behavior of the system depending on its state (the values in one or more variables) This compares the value in the variable named “potatoNumber” to 4 if (potatoNumber != 4) { cout << " potato"; } The code in the brackets will only happen if the comparison is true

  31. If Statements • This is the general form of an if statement: if(<condition>) { // do something }

  32. Things to Remember About Conditionals • They are NOT “if loops.” • The term “loop”implies something that can happen many times. • The body of an if statement happens only zero or one times – there is no repetition • If the condition is false, the body of the statement is skipped and execution continues on the next statement.

More Related