320 likes | 448 Vues
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?
E N D
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? • need to be able to create new recipes • need to understand how ingredients interact • need a vision of the culinary experience they want
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
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
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”
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!
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
Limitations of Assembly Languages • They are machine specific
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
Multiple Target Machines • One program can be translated by two different compilers to run on two different machines
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
Example 1 int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; }
Keywords • Words the compiler understands int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; }
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
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.
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
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:
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
x = 32; • Resulting memory diagram: • Notice that we have no idea what value y has yet x: 32 y:
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
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
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
y = x + 4; • Resulting memory diagram: x: 32 y: 36
Output the Result int main() { int x; int y; x = 32; y = x+4; cout << "y = " << y; } Will print out y = 36
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
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
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
While Loops • This is the general form of a while loop: while (<condition>) { // do something }
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”
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
If Statements • This is the general form of an if statement: if(<condition>) { // do something }
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.