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

Control Structures

The document provides an overview of control structures in programming, focusing on conditional statements, pattern matching, and loops in Python and C++. It highlights the differences in syntax and usage between the two languages, including the structure of if-elif-else and switch-case statements, as well as for and while loops. Additionally, it outlines when to use each type of loop based on specific programming scenarios.

Uploaded by

harisamser27
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Control Structures

The document provides an overview of control structures in programming, focusing on conditional statements, pattern matching, and loops in Python and C++. It highlights the differences in syntax and usage between the two languages, including the structure of if-elif-else and switch-case statements, as well as for and while loops. Additionally, it outlines when to use each type of loop based on specific programming scenarios.

Uploaded by

harisamser27
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CONTROL STRUCTURES

CONDITIONAL AND SWITCH STATEMENTS


CONDITIONALS: A BRIEF
REFRESHER
• Simplest form of control structure
• if block is tested first
• Any elif statements are tested when preceding conditions are false
• else block is used as last resort if it exists
• Compound conditions can be created using logical operators
CONDITIONALS – PYTHON VS C+
+ (1 OF 2)

Python C++
x = 5 int x = 5;
y = 8 int y = 8;
if x>y:
if(x>y){
print("x is grater.")
elif x<y: cout<< "x is greater.\n";
print("y is greater.") }
else: else if(x<y){
print("x and y are equal.") cout<< "y is greater.\n";
}
else{
cout<< "x and y are equal.\n";
}
CONDITIONALS – PYTHON VS C+
+ (2 OF 2)

Python C++
• Parentheses only used for • Conditions must be in parentheses
grouping • Statement ends with opening brace
• Statement ends with a colon • Block must be contained in braces
• Block must be indented • Block ends with closing brace
• Block ends with last indented line • else if denotes secondary condition
• elif denotes secondary condition • Logical operators: &&, ||, !

• Logical operators: and, or, not


PATTERN MATCHING: A QUICK
REFRESHER
• Quickly checks numerous simple conditions
• Can be faster than a long if-elif-else chain
• Each condition block is a case
• Cases enclosed in match block
• Limitation: Can only check the value of one variable
PATTERN MATCHING – PYTHON
VS C++ (1 OF 2)

Python C++
match x: switch(x){
case 1: case 1:
print("one") cout<< “one\n”;
case 2: break;
print(“two")
case 2:
case _:
print(“????") cout<< “one\n”;
break;
default:
cout<< “????\n”;
break;
}
PATTERN MATCHING – PYTHON
VS C++ (2 OF 2)

Python C++
• match-case • switch-case
• Tested variable is not In parentheses • Tested variable in parentheses
• Tested variable followed by colon • Tested variable followed by brace
• Cases must be indented • Cases are all in the same block
• Case blocks must also be indented
• Case indentation optional
• Cases end with last indented line
• Cases usually ends with break
• Multiple values separated by pipe
• Each value require its own case
LOOPS
ITERATION: A BRIEF
REFRESHER
• Core principle of DRY code
• Don’t Repeat Yourself
• Repeats single block of code until…
• End of a sequence
• Condition becomes false
• C++ implementation of loops different from
Python
FOR LOOPS – PYTHON VS C++
(1 OF 2)

Python C++
squares = [] vector<int> squares;
for i in range(0,11): for(int i=0; i<=10; i++){
squares.append(i**2) squares.push_back(i*i);
for index, value in enumerate(squares): }
print(f”#{index} = {value}”) for(int i=0; i<squares.size(); i++){
cout<< “\n#” << i << “ = “ <<
squares[i];
}
FOR LOOPS – PYTHON VS C++
(2 OF 2)
• Python has three “types” of for loop
1: for index in expression: #Iteration with integer index
2: for singular in plural: #Iterate over container/sequence
3: for index, singular in expressionOfPlural: #Iteration over container/sequence with
int index
• C++ has two types of for loop
1: for(type index=start; condition; modifyIndex) //Index-based
2: for(type iterator : collection) //Range-based
WHILE LOOPS – PYTHON VS C++

Python C++
x = 7 int x = 7;
result = 1 int result = 1;
while x > 1: while(x > 1){
result *= x result *= x;
x -= 1 x -= 1;
print(result) }
cout<< result << endl;
DO-WHILE LOOPS
• Not a feature of Python int x;
do{
• Same behavior as a while loop cout<< “Enter a number (0 to exit): “;
• Condition checked after iteration cin>> x;
cout<< “You entered “ << x << endl;
}while(x != 0);
WHEN TO USE EACH LOOP TYPE

For While Do-While


Searching a collection for a given Pulling elements from a collection Game loops
value until that collection is of a given
size Command line menus
Iterating over a collection or
sequence to perform some Reading a file line-by-line Repeatedly taking user input
operation such as sum or average
Calculating a recursive formula such
as a factorial

You might also like