6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures...

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

Transcript of 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures...

Page 1: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

6.s096 Lecture 2

1 Thursday, January 10, 13

Page 2: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Today

•Control Structures

•Variables and Functions

•Scope

•Uninitialized Memory - and what to do about it!

3

The core of the language

Thursday, January 10, 13

Page 3: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Control Structures

4 Thursday, January 10, 13

Page 4: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Basic Control Structures You’ve probably seen these…

while ��� � � ��

do…while ��������� � ��� ���������� �� ���

for � if […else if], […else]

�� � � �

5 Thursday, January 10, 13

Page 5: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Basic Control Structures You’ve probably seen these…

while ��� � � �� do…while �� �

���������� �� ��� for � ��������� � ���

if […else if], […else] �� � � � �

6 Thursday, January 10, 13

Page 6: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Basic Control Structures You’ve probably seen these…

while �� ��������� do…while ������� � � �� � � �� �����

���������� �� ��� for �

if […else if], […else] �� � � �

7 Thursday, January 10, 13

Page 7: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Basic Control Structures You’ve probably seen these…

��� � � �� while ���� � ��� do…while ���������� ���� ������

� ���� ���� �� ��� for ����������������

if […else if], […else] � ���� � ������������� ��������

� 8

Thursday, January 10, 13

Page 8: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Slight variations

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

• Empty ��� loop is an “infinite” �����: ������� �����������

9 Thursday, January 10, 13

Page 9: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

������

� ���������� ���� ��

������������ ������� ������

���� �� ������������ �������� ������

�������� ������������ ��������� ����������

10 Thursday, January 10, 13

Page 10: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

����

����������� ��������������������������������Jumps ������������������������� ������������������� ������������������ ��������������������� �������������������������� ������������������

Output: ��������� ������������������������

������������� ����������������������������� ��������� ������������������������ ����������� ������������������������

11 Thursday, January 10, 13

Page 11: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

����

����������� ��������������������������������Jumps ������������������������� ������������������� ������������������ ��������������������� �������������������������� ������������������

Output: ��������� ������������������������

������������� ����������������������������� ��������� ������������������������ ����������� ������������������������

12 Thursday, January 10, 13

Page 12: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

����

����������� ��������������������������������Jumps ������������������������� ������������������� ������������������ ��������������������� �������������������������� ������������������

Output: ��������� ������������������������

������������� ����������������������������� ��������� ������������������������ ����������� ������������������������

13 Thursday, January 10, 13

Page 13: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

����

����������� ��������������������������������Jumps ������������������������� ������������������� ������������������ ��������������������� �������������������������� ������������������

Output: ��������� ������������������������

������������� ����������������������������� ��������� ������������������������ ����������� ������������������������

14 Thursday, January 10, 13

Page 14: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

����

����������� ��������������������������������Jumps ������������������������� ������������������� ������������������ ��������������������� �������������������������� ������������������

Output: ��������� ������������������������

������������� ��������������������������������� ����������������������������� ���������� ���������

� ����������� ������������������������

������������� ������������������������

15 Thursday, January 10, 13

Page 15: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

The ���� statement in detail

• Syntax: ����������� … where label refers to an earlier or later labelled section of code.

• Target label must be in the same function as the ���� statement.

• Notorious for creating hard-to-read code, but the concept is critical to how computers operate.

16 Thursday, January 10, 13

Page 16: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Variables and Functions

17 Thursday, January 10, 13

Page 17: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Variables and constants

int a = 1; ���������������� a = 2; // cool �������

���������� ���������������������� ��������������������

18 Thursday, January 10, 13

Page 18: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

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

����������� ��������������������������� ������������������������� ���������� ��������������������������� �

Output: ���������

19 Thursday, January 10, 13

Page 19: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Functions in Variables We’ll examine part of this syntax in more depth in later lectures.

���������������������� ����������������������������� ����������������� ������������������������ � ���������������������������

���� ���������������������� ������������ ����������������� �������������������� � ��������������������������

20 Thursday, January 10, 13

Page 20: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Scope

21 Thursday, January 10, 13

Page 21: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Scope A variable has a scope in which it is said to be defined.

����������� �������������� �

In ��� and �� � is “in scope” f

the entire functi ���� � is “in scope” only with� the if statement’s block in ��

22

����������� �������������� �������������� ������������������ ���������������������� ����� ���������������� ������������������� ������������������������� ������������������������� �

� �

�, or

on. in �.

Thursday, January 10, 13

Page 22: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Anonymous Blocks Anonymous blocks demonstrate the concept of block scope.

����������� ������������������ ����� ��������������������������������������� �������� ��������� ��������������������������������������������� ��������� ����� ����������������������������������� �

23 Thursday, January 10, 13

Page 23: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Uninitialized Memory

24

When you see that gibberish output…

Thursday, January 10, 13

Page 24: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

Program memory, simplified…

����������

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

25 Thursday, January 10, 13

Page 25: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

(Common)

Sources Avoid these situations if you can help it!

• Uninitialized variables: ������ ����������������

• Out-of-bounds array access: ������������������ ����������������������������������

• Variables passed out of their defining function’s scope.

• �������(coming up in a later lecture)

26 Thursday, January 10, 13

Page 26: 6.S096 Lecture 2: Control Structures, Variables, Scope ......Today •Control Structures •Variables and Functions •Scope •Uninitialized Memory - and what to do about it! 3 he

MIT OpenCourseWarehttp://ocw.mit.edu

6.S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.