Algorithm A step by step approach to solving
											Algorithm • A step by step approach to solving a problem. • A properly designed algorithm must contain both: – the actions to be executed, and – the order in which these actions are to be executed 1
											Pseudocode • An artificial and informal language that helps programmers develop algorithms. • Only actions statements are used. – For example, you do not put declarations or #includes in pseudocode • It is easy to convert a program from psuedocode to c. 2
											Control Structures • Control the flow of a program • Normally, in C, statements are executed in sequential order • Control structures allow the programmer to specify that a statement other than the next be executed – i. e. programmer can control what’s executed in which order 3
											Go away from goto • goto statements allow the program to jump to a specified line before or after the current statement • Old programs used goto’s extensively, resulting in code that was buggy and extremely hard to follow • You should never use a goto. In fact, this is the last time we will even speak about goto. 4
											Three Basic Control Structures • All programs can be written with just these types of structures – sequence structure • i. e. one after the other – selection structure • e. g. if, if else, switch, etc. – repetition structure • i. e. repeat some actions 5
											The if structure (revisited) • If some condition is true do this • Example: if ( x == y ) { printf(“ x is equal to y!n“ ) ; } • Every programming language has some form of an if statement. 6
											Another if example if ( grade >= 60 ) { printf( “You passed!n” ); } 7
											if with a twist: if else • Basically is this: if some condition is true do this otherwise do something else • Example: if ( grade >= 60 ) printf( “You passed!n” ); else printf( “You failed!n” ); 8
											What is a “Flow Chart? ” • a visual tool that helps you understand the flow of your program • You can think of flow charts as pipes of water: – You pour water in the top. – Water pours through the pipes and is pulled downward 9
											Flow Chart Basics 1 Diamonds (decision symbol) contain conditions flow line Rectangles represent statements of work. For example: printf() 10
											Flow Chart Basics 2 Connector symbol grade >=60 true print “passed” false 11
											if/else Flow Chart False Print “You failed” True grade >=60 Print “You passed” 12
											Blocks • are started with a left brace { } and ended with a right brace • You can declare variables immediately after a left brace (as long as it’s before executable statements). The variables will only be available within that block of code. 13
											Blocks, continued • Remember, an if only looks at one statement. In order to force more than one statement to be dependent on the if, you need to use a block of code. • For example: if ( grade >= 60 ) { printf ( "You passed!!!n“ ); printf ( "Congratulations!n“ ); } 14
											Indentation • Everything within the block of code should be indented – helps you see the block at a quick glance. • Avoid writing code like this: if (grade >= 60) { printf ("You passed!!!n"); printf ("Congratulations!n"); } • This is valid C code, but it is not easy to view the block: bad style 15
											Example /* Introduction to If/Else Statements #include <stdio. h> */ int main () { int grade; /* variable to hold user input */ printf ( "Enter your course grade: “ ); scanf ( "%d", &grade ); if ( grade >= 60 ) { printf ( "You passed!!! n“ ); printf ( "Congratulations!n“ ); Note the indentation} else { printf ("You failed!n"); } } /* end main program */ 16
											Relational Operators (revisited) Operator Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not Equal to 17
											Testing for Equality • To test for equality, use the == operator. if ( grade == 100 ) { printf ( “Perfect Score!” ) ; } • To test for inequality, use the != operator if (grade != 100) { printf (“You messed something up”); } 18
											Equality v. Assignment (revisited) • Remember Gets not Equals! if ( grade = 100 ) printf( “Perfect Score!” ); • In this case, we are using a single = character. (We really want to use ==) • What will it actually print? – Depends on whether grade is TRUE or FALSE 19
											Understanding Truth • In C (unlike life) truth is very simple. – 0 is False – Anything else is True • For example: – -1 is true – 299 is true – 0 is false 20
											Truth Example /* Understanding Truth */ #include <stdio. h> int main() { int x = -100; int y = 299; int z = 0; if ( x ) printf ("x: if ( y ) printf ("y: if ( z ) printf ("z: } This is a completely valid program. Output: x: -100 y: 299 %dn", x); %dn", y); %dn", z); Note: z is not printed because the if condition fails. 21
											Equality v. Assignment • Given: if (grade = 100) printf (“Perfect Score!”); • This statement does the following: – Grade is assigned the value 100. – Because 100 is true (i. e. non-zero!), the condition is always true. • No matter what the student grade, it always says “Perfect Score!” 22
- Slides: 22