190 likes | 319 Views
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
E N D
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 • 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
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"); }
#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"); }
What are the Differences • Number of comparisons • Style (which is better?)
&& - 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
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"); }
|| - 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
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"); }
#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
#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); }
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"); }
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
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
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
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
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
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"); } }
#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"); } }