6.s096 lecture 2: control structures, variables, scope ......today •control structures...
TRANSCRIPT
![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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/9.jpg)
�
������
� ���������� ���� ��
������������ ������� ������
���� �� ������������ �������� ������
�������� ������������ ��������� ����������
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/10.jpg)
����
�
����������� ��������������������������������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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/11.jpg)
����
�
����������� ��������������������������������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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/12.jpg)
����
�
����������� ��������������������������������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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/13.jpg)
����
�
����������� ��������������������������������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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/14.jpg)
����
�
����������� ��������������������������������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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/18.jpg)
�
������ 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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/25.jpg)
(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](https://reader033.vdocuments.net/reader033/viewer/2022042414/5f2f1eefb601a10c4728e50e/html5/thumbnails/26.jpg)
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.