Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.

Similar presentations


Presentation on theme: "Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld."— Presentation transcript:

1 Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld

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 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

4 The if structure 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.

5 Another if example #include void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); if (grade >=65) printf("You passed!\n"); }

6 if with a twist: if else #include void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); if (grade >=65) printf("You passed!\n"); else printf("You failed!\n"); }

7 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

8 Flow Chart Basics 1 Rectangles represent statements of work. For example: printf() Diamonds (decision symbol) contain conditions flow line

9 Flow Chart Basics 2 print “passed” grade >=65 true Connector symbol false

10 if/else Flow Chart grade >=65 Print “You passed” Print “You failed” True False

11 Blocks are started with a left brace { } and ended with a right brace you can declare variables anywhere after a left brace (as long as it’s before executable statements)

12 Blocks, continued To run several lines of code together, you must include them within a block For example: if ( grade >= 65 ) { printf ( "You passed!!!\n “ ); printf ( "Congratulations!\n “ ); }

13 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

14 Example /* Introduction to If/Else Statements */ #include int main () { int grade; /* variable to hold user input */ printf ( "Enter your course grade: “ ); scanf ( "%d", &grade ); if ( grade >= 65 ) { printf ( "You passed!!! \n “ ); printf ( "Congratulations!\n “ ); } else { printf ("You failed!\n"); } } /* end main program */ Note the indentation

15 Relational Operators (revisited) Not Equal to!= Equal to== Less than or equal to<= Greater than or equal to>= Less than< Greater than> MeaningOperator

16 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 ( “ Not perfect! ” ); }

17 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

18 Understanding Truth (in the Boolean Sense) 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

19 The Conditional Operator #include void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); printf("%s\n", (grade>=65) ? "You passed" : "You failed"); } %s is the format specifier for text strings. The conditional Statement. True Option False Option ? short- cut operator

20 Truth Example /* Understanding Truth */ #include int main() { int x = -100; int y = 299; int z = 0; if ( x ) printf ("x: %d\n", x); if ( y ) printf ("y: %d\n", y); if ( z ) printf ("z: %d\n", z); } This is a completely valid program. Output: x: -100 y: 299 Note: z is not printed because the if condition fails.

21 Pseudocode The implementation of an algorithm Not code, not English Ex: –Read a number –If the number is greater than 90, print ‘A’ –Else, if the number is greater than 80, print ‘B’ –Else, if the number is greater than 70, print ‘C’

22 Code Implementation (Style #1) #include void main() { int grade; scanf("%d",&grade); if (grade>90) printf("You got an A\n"); else if (grade > 80) printf("You got a B\n"); else if (grade > 75) printf("You got a C\n"); else printf("Sorry, you'll have to repeat the course\n"); }

23 Code Implementation #2 #include void main() { int grade; scanf("%d",&grade); if (grade>90) printf("You got an A\n"); else if (grade > 80) printf("You got a B\n"); else if (grade > 75) printf("You got a C\n"); else printf("Sorry, you'll have to repeat the course\n"); }

24 Incorrect Code Implementation #include void main() { int grade; scanf("%d",&grade); if (grade>90) printf("You got an A\n"); if (grade > 80) printf("You got a B\n"); if (grade > 75) printf("You got a C\n"); else printf("Sorry, you'll have to repeat the course\n"); }


Download ppt "Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld."

Similar presentations


Ads by Google