Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 3 - Friday.  What did we talk about last time?  Bitwise practice  Precedence  Control flow.

Similar presentations


Presentation on theme: "Week 3 - Friday.  What did we talk about last time?  Bitwise practice  Precedence  Control flow."— Presentation transcript:

1 Week 3 - Friday

2  What did we talk about last time?  Bitwise practice  Precedence  Control flow

3

4

5 Unix is simple. It just takes a genius to understand its simplicity. Dennis Ritchie Unix is simple. It just takes a genius to understand its simplicity. Dennis Ritchie

6

7  Loops can go on forever if you aren’t careful int n = 40; int i = 1; while( i <= 40 ) { printf("%d", i); //supposed to print all the numbers //less than 40, but i never increases } int n = 40; int i = 1; while( i <= 40 ) { printf("%d", i); //supposed to print all the numbers //less than 40, but i never increases }

8  Infinite for loops are unusual, but possible:  This situation is more likely: for( ; ; ) printf("Hey!"); for( ; ; ) printf("Hey!"); int i; for(i = 0; i < 10; i++ ) { printf("%d", i); //lots of other code i--; //whoops, maybe changed from while? } int i; for(i = 0; i < 10; i++ ) { printf("%d", i); //lots of other code i--; //whoops, maybe changed from while? }

9  Overflow and underflow will make some badly written loops eventually terminate int i; for( i = 1; i <= 40; i-- ) //whoops, should have been i++ printf("%d", i); int i; for( i = 1; i <= 40; i-- ) //whoops, should have been i++ printf("%d", i);

10  Being off by one is a very common loop error int i; for( i = 1; i < 40; i++ ) //runs 39 times printf("%d", i); int i; for( i = 1; i < 40; i++ ) //runs 39 times printf("%d", i);

11  If the condition isn’t true to begin with, the loop will just be skipped int i; for( i = 1; i >= 40; i++ ) //oops, should be <= printf("%d", i); int i; for( i = 1; i >= 40; i++ ) //oops, should be <= printf("%d", i);

12  A misplaced semicolon can cause an empty loop body to be executed  Everything looks good, loop even terminates  But, only one number will be printed: 41  Misplaced semicolon usually makes a while loop infinite int i; for( i = 1; i <= 40; i++ ); //semicolon is wrong { printf("%d", i); } int i; for( i = 1; i <= 40; i++ ); //semicolon is wrong { printf("%d", i); }

13

14

15

16  Systems programming concepts  Functions and prototypes

17  Read LPI chapter 3 and K&R chapter 4  Project 1 due tonight by midnight!


Download ppt "Week 3 - Friday.  What did we talk about last time?  Bitwise practice  Precedence  Control flow."

Similar presentations


Ads by Google