Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1

2 Introduction to Computers and Programming Class 9 Introduction to C Professor Avi Rosenfeld

3 Loop Structures Event Controlled –While Loops –Do While Loops Counter Controlled –Counter while loops –For loops (next time)

4 3 Parts of a while Loop Every while loop will always contain three main elements: –Priming: initialize your variables –Testing: test against some known condition –Updating: update the variable that is tested

5 4 Infinite Loop Infinite Loop: A loop that never ends –Generally, you want to avoid these! –There are special cases, however, when you do want to create infinite loops on purpose Common Exam Questions: –Given a piece of code, identify the bug in the code –You may need to identify infinite loops

6 5 Infinite Loop Example #1 #include main () { int index =1; while (index <= 10) { printf ("Index: %d\n", index); } Here, I have deleted part 3: the index = index + 1 statement. Index: 1 … [forever]

7 #include void main() { int number,EvenNumbers = 0, OddNumbers = 0; printf("\nEnter a number between 0 & 9 inclusive (999 to end program): "); scanf("%d", &number); /* why is this scanf needed? */ while (number != 999) { switch (number) { case 0: case 2: case 4: case 6: case 8: EvenNumbers++; /*what is the ++ ? */ printf("You entered a valid EVEN #"); break; case 1: case 3: case 5: case 7: case 9: OddNumbers++; printf("You entered a valid ODD #"); break; default: printf("\n*** You did not enter a # between 0 & 9"); break; } /* end of switch */ printf("\nEnter a number between 0 & 9 inclusive (999 to end program): "); scanf("%d", &number); } /* end of while loop */ printf("\n\nYou entered a total of %d EVEN #s and %d ODD #s\n", EvenNumbers, OddNumbers); }

8 #include void main() { int number,EvenNumbers = 0, OddNumbers = 0; do { printf("\nEnter a number between 0 & 9 inclusive (999 to end program): "); scanf("%d", &number); switch (number) { case 0: case 2: case 4: case 6: case 8: EvenNumbers++; printf("You entered a valid EVEN #"); break; case 1: case 3: case 5: case 7: case 9: OddNumbers++; printf("You entered a valid ODD #"); break; default: printf("\n*** You did not enter a # between 0 & 9"); break; } /* end of switch */ }while (number != 999); /* end of while loop */ printf("\n\nYou entered a total of %d EVEN #s and %d ODD #s\n", EvenNumbers, OddNumbers); }

9 Shortcuts C provides abbreviations for some common operations Assignment operators Increment/Decrement operators

10 Assignment Operators Abbreviations are provided for the basic binary operations –Addition –Subtraction –Multiplication –Division –Modulus (%)

11 Shortcut Heaven (or Hell?) #include void main() { int x = 2, y = 3; x *= 3; /* same as x = x * 3; x is 6 */ printf("X is now %d\n", x); y /= 2; /* same as y = y / 2; y is 1 */ printf("Y is now %d\n", y); y += 2; /* same as y = y + 2; y is 3 */ printf("Y is now %d\n", y); x -= 2; /* same as x = x - 2; x is 4 */ printf("X is now %d\n", x); x %= y; /* same as x = x % y; x is 1 */ printf("X is now %d\n", x); }

12 Samples Assume variable int c = 10 Operator Initial Value SampleMeaningAssigns +=10c += 7;c = c + 7;17 to c -=10c -= 3;c = c – 3;7 to c *=10c *= 6;c = c * 6;60 to c /=10c /= 5;c = c / 5;2 to c %=10c %= 6;c = c % 7;3 to c

13 Increment and Decrement Operators C provides unary increment operators ++ and decrement operators – - (no spaces between them) Increment operators add 1 Decrement operators subtract 1 Not for other assignment operators (*, /, %) Guess where C++ comes from???

14 Postincrement The ++ operator is after the variable Causes the initial value of the variable to be used in the expression where it appears AND THEN adds the 1 to the variable For example, int iCount = 5; printf( “ %d\n ”, iCount++ ); Would print 5… but iCount is worth 6 after the statement

15 Preincrement The ++ operator is before the variable Adds 1 to the initial value of the variable BEFORE it is used in the expression where it appears For example, int iCount = 5; printf( “ %d\n ”, ++iCount); Would print 6… and iCount is worth 6 after the statement

16 More examples ++ postincrement vs. preincrement int iTotal = 0; int iCount = 5; iTotal = iCount++ * 2; printf( “ %d\n ”, iTotal); printf( “ %d\n ”, iCount); Would print 10 for iTotal and then 6 for iCount iTotal = ++iCount * 2; printf( “ %d\n ”, iTotal); printf( “ %d\n ”, iCount); Would print 12 for iTotal and then 6 for iCount

17 Decrement Operator -- Similar to increment in syntax/operation Instead of writing iCount = iCount - 1; or iCount -= 1; You can write iCount-- ; or --iCount ; Subtle difference between the two new options call postdecrement and predecrement

18 Postdecrement The -- operator is after the variable Causes the initial value of the variable to be used in the expression where it appears AND THEN subtracts the 1 from the variable For example, int iCount = 5; printf( “ %d\n ”, iCount-- ); Would print 5… but iCount is worth 4 after the statement

19 Predecrement The -- operator is before the variable Subtracts 1 from the initial value of the variable BEFORE it is used in the expression where it appears For example, int iCount = 5; printf( “ %d\n ”, --iCount); Would print 4… and iCount is worth 4 after the statement

20 For our last trick… #include void main() { int x = 2, y = 3; printf("What is this number %d\n", x++ + ++y); printf("But x is still %d and y is %d\n", x, y); printf( “ What # %d ???\n", y / x-- * ++x % y); /* 0 */ printf("But x is still %d and y is %d\n", x, y); }

21 Summary Table OperatorSampleExplanation ++c++ Use value of c in expression THEN add 1 to c ++++c Add 1 to c THEN use the new value of c in expression --c-- Use value of c in expression THEN subtract 1 from c ----c Subtract 1 from c THEN use new value of c in expression

22 Introducing for loops #include void main() { int counter = 100; while (counter--> 0) printf("The counter is at %d ", counter); }


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

Similar presentations


Ads by Google