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

6 s096
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

6.s096

Lecture 2

1

Thursday, January 10, 13
slide-2
SLIDE 2

Administrative Notes…

  • Assignment 1 due at midnight (11:59pm) tonight.
  • Please try to use Piazza for asking questions when possible:

htps://piazza.com/mit/spring2013/6s096/home

  • Let us know if you’re still having compiler setup issues.

2

Thursday, January 10, 13
slide-3
SLIDE 3

Today

  • Control Structures
  • Variables and Functions
  • Scope
  • Uninitialized Memory - and what to do about it!

3

Te core of the language

Thursday, January 10, 13
slide-4
SLIDE 4

Control Structures

4

Thursday, January 10, 13
slide-5
SLIDE 5

Basic Control Structures

5

You’ve probably seen these… while do…while for if […else if], […else]

int ¡i ¡= ¡0; while(i++ ¡< ¡3){ ¡ ¡ ¡ ¡printf(“%d ¡”, ¡i); } => ¡1 ¡2 ¡3 ¡

Thursday, January 10, 13
slide-6
SLIDE 6

Basic Control Structures

6

You’ve probably seen these… while do…while for if […else if], […else]

int ¡i ¡= ¡0; do ¡{ printf(“%d ¡“, ¡i); } ¡while(i++ ¡< ¡3); => ¡0 ¡1 ¡2 ¡3

Thursday, January 10, 13
slide-7
SLIDE 7

Basic Control Structures

7

You’ve probably seen these… while do…while for if […else if], […else]

// ¡C99-­‑style for(int ¡i ¡= ¡0; ¡i ¡< ¡3; ¡++i){ ¡ ¡ ¡ ¡printf(“%d ¡“, ¡i); } => ¡0 ¡1 ¡2

Thursday, January 10, 13
slide-8
SLIDE 8

Basic Control Structures

8

You’ve probably seen these… while do…while for if […else if], […else]

int ¡i ¡= ¡0; if(i ¡< ¡3){ ¡ ¡ ¡ ¡printf(“It ¡sure ¡is.”); } ¡else ¡if(i ¡== ¡3){ ¡ ¡ ¡ ¡printf(“Nope.”); } ¡else ¡{ ¡ ¡ ¡ ¡printf(“Still ¡nope.”); }

Thursday, January 10, 13
slide-9
SLIDE 9

Slight variations

  • Blocks / braces ofen optional (if, while, for):

if(condition) ¡expression;

  • Empty for loop is an “infinite” while:

for(;;) ¡expression;

9

Thursday, January 10, 13
slide-10
SLIDE 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
slide-11
SLIDE 11

Jumps

11

void ¡foo(){ ¡ ¡ ¡ ¡for(int ¡i ¡= ¡0; ¡i ¡< ¡10; ¡++i){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(i ¡== ¡2){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡3; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); ¡ ¡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¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡continue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡else ¡if(i ¡== ¡6){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡goto ¡end; ¡ ¡ ¡ ¡printf("near ¡the ¡end\n"); ¡ ¡ ¡ ¡end: ¡ ¡ ¡ ¡ ¡printf("the ¡end\n"); ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡printf("or ¡is ¡it?"); }

Output:

0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end

Thursday, January 10, 13
slide-14
SLIDE 14

Jumps

14

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

Output:

0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end

Thursday, January 10, 13
slide-15
SLIDE 15

Jumps

15

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

Output:

0 ¡1 ¡2 ¡4 ¡5 ¡6 ¡the ¡end

int ¡main(int ¡argc, ¡char ¡** ¡argv){ ¡ ¡ ¡ ¡foo(); ¡ ¡ ¡ ¡return ¡0; }

Thursday, January 10, 13
slide-16
SLIDE 16

Te 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
slide-17
SLIDE 17

Variables and Functions

17

Thursday, January 10, 13
slide-18
SLIDE 18

Variables and constants

18

int a = 1; a = 2; // cool const ¡int ¡b ¡= ¡1; b ¡= ¡2; ¡

// ¡error: ¡ ¡ ¡ ¡read-­‑only ¡variable ¡ ¡ ¡ ¡is ¡not ¡assignable

Thursday, January 10, 13
slide-19
SLIDE 19

static Variables

19

for(int ¡i ¡= ¡0; ¡i ¡< ¡5; ¡++i){ ¡ ¡ ¡ ¡foo(); } void ¡foo(){ ¡ ¡ ¡ ¡static ¡int ¡count ¡= ¡0; ¡ ¡ ¡ ¡printf("%d ¡", ¡count++); }

Output: 0 ¡1 ¡2 ¡3 ¡4

Static variables retain their value throughout the life of the program.

Thursday, January 10, 13
slide-20
SLIDE 20

Functions in Variables

20

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

Thursday, January 10, 13
slide-21
SLIDE 21

Scope

21

Thursday, January 10, 13
slide-22
SLIDE 22

Scope

22

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

Thursday, January 10, 13
slide-23
SLIDE 23

Anonymous Blocks

23

Anonymous blocks demonstrate the concept of block scope. void ¡foo(){ ¡ ¡ ¡ ¡{ ¡int ¡a ¡= ¡0; ¡} ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡double ¡a ¡= ¡3.14; ¡// ¡no ¡problem! ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡char ¡* ¡a ¡= ¡"3.14"; ¡// ¡no ¡problem! ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡// ¡no ¡'a' ¡defined ¡in ¡this ¡scope }

Thursday, January 10, 13
slide-24
SLIDE 24

Uninitialized Memory

24

When you see that gibberish output…

Thursday, January 10, 13
slide-25
SLIDE 25

Program memory, simplified…

25

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

int ¡a ¡= ¡0;

Thursday, January 10, 13
slide-26
SLIDE 26

26

  • Uninitialized variables:

int ¡i; printf(“%d”, ¡i);

  • Out-of-bounds array access:

char ¡reversed[20]; char ¡out_of_bounds ¡= ¡reversed[21];

  • Variables passed out of their defining function’s scope.
  • malloc ¡(coming up in a later lecture)

Sources

Avoid these situations if you can help it! (Common)

Thursday, January 10, 13