1 / 19

Introduction to Computers and Programming

Introduction to Computers and Programming. Class 7 Introduction to C Professor Avi Rosenfeld. Control Structures. Control the flow of a program Normally, in C, statements are executed in sequential order

hanh
Télécharger la présentation

Introduction to Computers and Programming

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. Introduction to Computers and Programming Class 7 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. Grade Program Logic (revisited) #include <stdio.h> 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"); }

  4. #include <stdio.h> void main() { int grade; printf("Please enter a grade\n"); scanf("%d",&grade); if (grade>=90) printf("You got an A\n"); if (grade >= 80 && grade <90) printf("You got a B\n"); if (grade >= 70 && grade < 80) { printf("You got a C\n"); printf("Sorry, you'll have to repeat the course\n"); } if (grade >= 65 && grade < 70) { printf("You got a d\n"); printf("Sorry, you'll have to repeat the course\n"); } if (grade < 65) printf("Sorry, you failed an will have to repeat the course\n"); }

  5. What are the Differences • Number of comparisons • Style (which is better?)

  6. && - Logical AND (section 4.10) if (grade >= 80 && grade <90) printf("You got a B\n"); With &&, both expressions must be true for the compound statement to be true

  7. Or Sample Program #include <stdio.h> void main() { int x,y; printf("Please type 2 numbers\n"); scanf("%d%d",&x,&y); if (x == 100 || y == 100) printf("One number is 100\n"); else printf("Sorry, no 100's this time!\n"); }

  8. || - Logical OR (section 4.10) if (total > 50) || (status == 0) { printf(“Your shipping will be free\n”); } • With ||, at least one expression must be true for the compound statement to be true truth table

  9. The Logical “not”aka 2b || !2b #include <stdio.h> void main() { int x; printf("Please type 1 number\n"); scanf("%d",&x); if (x > 5) printf("Your number is more than 5\n"); if (!(x < 5)) printf("Your number is more than 5, but why!\n"); }

  10. #define • The #define preprocessor directive creates symbolic constants #define PI 3.14159 • Would replace all subsequent occurrences of the symbolic constant PI with the numeric constant 3.14159, in the code • Should be in ALL CAPS

  11. #define Program #include <stdio.h> #define PI 3.14159 #define RADIUS 2.5 void main() { printf("The area of circle with radius %.2f is %.2f\n", RADIUS, RADIUS * RADIUS * PI); }

  12. Grade Program Logic (revisited) #include <stdio.h> void main() { int grade; printf ("Please type a grade\n"); 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"); }

  13. switch Multiple-Selection Structure • Used when testing a variable or expression for EQUALITY (i.e. no >, <, >=, <= tests) separately for several different values • Note: The grade program won’t work with a switch. • Allows you to perform different actions for each test

  14. switch cont’d • Preferred over if else if in situations where you are testing the same expressions for equality with many different values. • If you find yourself with an if else if longer than 3 or 4 clauses, and you’re testing for equality, probably should be using switch

  15. switch Multiple-Selection Structure keyword switch switch (expression) { case value1: action(s); break; case value2: action(s); break; … case default: actions(s); break; } expression can be a variable or a more complicated expression could use more than one case; if the same actions are required actions within a single case do not need brackets the default case will be executed in the event that no other case is

  16. beware of “fall through” • If you forget to use the break keyword between cases, unexpected things may happen • Once a case tests true, all the statements following that case will be executed until the next break

  17. case default: • Is the catch-all of last resort • If none of your specified cases are satisfied then the default will watch your back • That is, it will perform the actions you specify under case default: if there wasn’t a match on specific cases you provided • Always use it

  18. Case with no Breaks #include <stdio.h> void main() { char value; printf ("Press (A)pple (B)oy or (C)at\n"); scanf("%c",&value); switch(value) { case ('A'): printf("A is for Apple\n"); case ('B'): printf("B is for Boy\n"); case ('C'): printf("C is for Cat\n"); default: printf("Not a valid entry\n"); } }

  19. #include <stdio.h> void main() { char value; printf ("Press (A)pple (B)oy or (C)at\n"); scanf("%c",&value); switch(value) { case ('A'): case ('a'): printf("A is for Apple\n"); break; case ('B'): printf("B is for Boy\n"); break; case ('C'): printf("C is for Cat\n"); break; default: printf("Not a valid entry\n"); } }

More Related