6 s096
play

6.s096 Lecture 2 1 Thursday, January 10, 13 Administrative Notes - PowerPoint PPT Presentation

6.s096 Lecture 2 1 Thursday, January 10, 13 Administrative Notes Assignment 1 due at midnight (11:59pm) tonight. Please try to use Piazza for asking questions when possible: h t ps://piazza.com/mit/spring2013/6s096/home Let us


  1. 6.s096 Lecture 2 1 Thursday, January 10, 13

  2. Administrative Notes… • Assignment 1 due at midnight (11:59pm) tonight. • Please try to use Piazza for asking questions when possible: h t ps://piazza.com/mit/spring2013/6s096/home • Let us know if you’re still having compiler setup issues. 2 Thursday, January 10, 13

  3. Today T e core of the language • Control Structures • Variables and Functions • Scope • Uninitialized Memory - and what to do about it! 3 Thursday, January 10, 13

  4. Control Structures 4 Thursday, January 10, 13

  5. Basic Control Structures You’ve probably seen these… while int ¡i ¡= ¡0; while(i++ ¡< ¡3){ do…while ¡ ¡ ¡ ¡printf(“%d ¡”, ¡i); for } if […else if], […else] => ¡1 ¡2 ¡3 ¡ 5 Thursday, January 10, 13

  6. Basic Control Structures You’ve probably seen these… while int ¡i ¡= ¡0; do ¡{ do…while printf(“%d ¡“, ¡i); for } ¡while(i++ ¡< ¡3); if […else if], […else] => ¡0 ¡1 ¡2 ¡3 6 Thursday, January 10, 13

  7. Basic Control Structures You’ve probably seen these… while // ¡C99-­‑style for(int ¡i ¡= ¡0; ¡i ¡< ¡3; ¡++i){ do…while ¡ ¡ ¡ ¡printf(“%d ¡“, ¡i); for } if […else if], […else] => ¡0 ¡1 ¡2 7 Thursday, January 10, 13

  8. Basic Control Structures You’ve probably seen these… int ¡i ¡= ¡0; while if(i ¡< ¡3){ ¡ ¡ ¡ ¡printf(“It ¡sure ¡is.”); do…while } ¡else ¡if(i ¡== ¡3){ for ¡ ¡ ¡ ¡printf(“Nope.”); } ¡else ¡{ if […else if], […else] ¡ ¡ ¡ ¡printf(“Still ¡nope.”); } 8 Thursday, January 10, 13

  9. Slight variations • Blocks / braces o f en optional (if, while, for): if(condition) ¡expression; • Empty for loop is an “in fi nite” while : for(;;) ¡expression; 9 Thursday, January 10, 13

  10. switch • switch(i){ ¡ ¡ ¡ ¡case ¡1: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf(“It’s ¡one!”); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡case ¡2: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf(“It’s ¡two!!”); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡default: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf(“It’s ¡something ¡else!!!”); } 10 Thursday, January 10, 13

  11. void ¡foo(){ Jumps ¡ ¡ ¡ ¡for(int ¡i ¡= ¡0; ¡i ¡< ¡10; ¡++i){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(i ¡== ¡2){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡3; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; Output: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} 0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); ¡ ¡ ¡ ¡end: ¡ ¡ ¡ ¡ ¡printf("the ¡end\n"); ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡printf("or ¡is ¡it?"); } 11 Thursday, January 10, 13

  12. void ¡foo(){ Jumps ¡ ¡ ¡ ¡for(int ¡i ¡= ¡0; ¡i ¡< ¡10; ¡++i){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(i ¡== ¡2){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡3; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; Output: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} 0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); ¡ ¡ ¡ ¡end: ¡ ¡ ¡ ¡ ¡printf("the ¡end\n"); ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡printf("or ¡is ¡it?"); } 12 Thursday, January 10, 13

  13. void ¡foo(){ Jumps ¡ ¡ ¡ ¡for(int ¡i ¡= ¡0; ¡i ¡< ¡10; ¡++i){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(i ¡== ¡2){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡3; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; Output: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} 0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); ¡ ¡ ¡ ¡end: ¡ ¡ ¡ ¡ ¡printf("the ¡end\n"); ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡printf("or ¡is ¡it?"); } 13 Thursday, January 10, 13

  14. void ¡foo(){ Jumps ¡ ¡ ¡ ¡for(int ¡i ¡= ¡0; ¡i ¡< ¡10; ¡++i){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(i ¡== ¡2){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡3; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; Output: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} 0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); ¡ ¡ ¡ ¡end: ¡ ¡ ¡ ¡ ¡printf("the ¡end\n"); ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡printf("or ¡is ¡it?"); } 14 Thursday, January 10, 13

  15. void ¡foo(){ Jumps ¡ ¡ ¡ ¡for(int ¡i ¡= ¡0; ¡i ¡< ¡10; ¡++i){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(i ¡== ¡2){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡3; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; Output: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} 0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); int ¡main(int ¡argc, ¡char ¡** ¡argv){ ¡ ¡ ¡ ¡end: ¡ ¡ ¡ ¡ ¡foo(); ¡ ¡ ¡ ¡printf("the ¡end\n"); ¡ ¡ ¡ ¡return ¡0; ¡ ¡ ¡ ¡return; } ¡ ¡ ¡ ¡printf("or ¡is ¡it?"); } 15 Thursday, January 10, 13

  16. T e goto statement in detail • Syntax: goto ¡ label ; … where label refers to an earlier or later labelled section of code. • Target label must be in the same function as the goto statement. • Notorious for creating hard-to-read code, but the concept is critical to how computers operate. 16 Thursday, January 10, 13

  17. Variables and Functions 17 Thursday, January 10, 13

  18. Variables and constants int a = 1; const ¡int ¡b ¡= ¡1; a = 2; // cool b ¡= ¡2; ¡ // ¡error: ¡ ¡ ¡ ¡read-­‑only ¡variable ¡ ¡ ¡ ¡is ¡not ¡assignable 18 Thursday, January 10, 13

  19. static Variables Static variables retain their value throughout the life of the program. void ¡foo(){ for(int ¡i ¡= ¡0; ¡i ¡< ¡5; ¡++i){ ¡ ¡ ¡ ¡static ¡int ¡count ¡= ¡0; ¡ ¡ ¡ ¡foo(); ¡ ¡ ¡ ¡printf("%d ¡", ¡count++); } } Output: 0 ¡1 ¡2 ¡3 ¡4 19 Thursday, January 10, 13

  20. Functions in Variables We’ll examine part of this syntax in more depth in later lectures. int ¡foo(int ¡a, ¡int ¡b){ int ¡(*func)(int, ¡int) ¡= ¡&foo; ¡ ¡ ¡ ¡return ¡a ¡+ ¡b; int ¡result ¡= ¡func(2, ¡2); } printf("%d ¡", ¡result); ¡// ¡4 ¡ ¡ ¡ ¡ int ¡bar(int ¡c, ¡int ¡d){ func ¡= ¡&bar; ¡ ¡ ¡ ¡return ¡c ¡-­‑ ¡d; result ¡= ¡func(2, ¡2); } printf("%d", ¡result); ¡// ¡0 20 Thursday, January 10, 13

  21. Scope 21 Thursday, January 10, 13

  22. Scope A variable has a scope in which it is said to be de fi ned. void ¡bar(){ void ¡foo(){ ¡ ¡ ¡ ¡int ¡a ¡= ¡0; ¡ ¡ ¡ ¡int ¡a ¡= ¡0; ¡ ¡ ¡ ¡if(3 ¡> ¡0){ } ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡b ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡b ¡= ¡2; ¡// ¡okay ¡ ¡ ¡ ¡} In foo and bar , ¡ ¡ ¡ ¡a++; ¡// ¡okay a is “in scope” for ¡ ¡ ¡ ¡b++; ¡// ¡error: ¡ the entire function. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡use ¡of ¡undeclared ¡ b is “in scope” only within ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡identifier ¡'b' ¡ the if statement’s block in bar . } 22 Thursday, January 10, 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend