0% found this document useful (0 votes)
2 views

MIT 6.0001 Introduction to Comp Sci and Programming in Python 2

The document covers control flow, decision-making, and loops in programming, using examples like navigating the MIT campus and the Legend of Zelda's Lost Woods. It explains key programming structures such as if, if-else, and if-elif-else statements, as well as while and for loops, including their syntax and use cases. Additionally, it compares for loops, which are used for known iterations, with while loops, which are better for unpredictable tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MIT 6.0001 Introduction to Comp Sci and Programming in Python 2

The document covers control flow, decision-making, and loops in programming, using examples like navigating the MIT campus and the Legend of Zelda's Lost Woods. It explains key programming structures such as if, if-else, and if-elif-else statements, as well as while and for loops, including their syntax and use cases. Additionally, it compares for loops, which are used for known iterations, with while loops, which are better for unpredictable tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MIT Computer Science Lecture Notes

Topic: Control Flow, Decisions, and Loops

1. Control Flow and Decision Making

Example: Navigating MIT campus for free food.


Algorithm: Keep right hand on a wall and navigate.
Steps:
If no wall on the right, turn right.
If wall on the right but path forward, move forward.
If walls on right and front, turn left.
If surrounded by walls, turn around.

Keywords in Programming:
if, else, elif (else-if)
Computers don't make decisions. Programmers build decisions into programs.

2. Basic Control Structures

If Statement:
Checks a condition. If true, executes a block of code.
Syntax:
python
Copy code
if condition:
# code block

If-Else Statement:
Checks a condition. If true, executes one block of code, otherwise executes another.
Syntax:
python
Copy code
if condition:
# code block 1
else:
# code block 2

If-Elif-Else Statement:
Multiple decision points.
Syntax:
python
Copy code
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3

3. Loops

Example: Legend of Zelda's Lost Woods.


Problem: Infinite loop if user keeps moving in one direction.
Solution: Use loops to handle repetitive tasks.

While Loop:
Repeats a block of code as long as a condition is true.
Syntax:
python
Copy code
while condition:
# code block
Can lead to infinite loops if not careful.

For Loop:
Iterates over a sequence (range, list, etc.).
Syntax:
python
Copy code
for variable in range(start, stop, step):
# code block
range() function: Generates a sequence of numbers.
range(stop): Generates numbers from 0 to stop-1.
range(start, stop): Generates numbers from start to stop-1.
range(start, stop, step): Generates numbers from start to stop-1, incrementing by step.

Break Statement:
Exits the loop prematurely.
Syntax:
python
Copy code
if condition:
break

4. Comparison between For and While Loops

For Loops:
Used when the number of iterations is known.
Has an inherent counter.

While Loops:
Useful for unpredictable tasks, like user input.
Can use a counter, but must be initialized and incremented manually.
Can be rewritten as a for loop, but the reverse isn't always true.

You might also like