Week 4 Control Structures PDF
Document Details
Uploaded by ArdentAbundance
ICDT
Tags
Summary
This document is an educational resource detailing control structures in computer programming. It introduces concepts such as sequential, conditional, and repeating flows. The document also showcases problem-solving methods and includes relevant Python code examples. It is suitable for undergraduate computer science students.
Full Transcript
ICDT 1201Y_Computer Programming Control Structures (Week 4) ICDT 1201Y Computer Programming Learning Objectives Understand the objectives of decision in programs Illustrate a given problem using flowchart and pseudocodes Write programs using if… elif …else statement in Py...
ICDT 1201Y_Computer Programming Control Structures (Week 4) ICDT 1201Y Computer Programming Learning Objectives Understand the objectives of decision in programs Illustrate a given problem using flowchart and pseudocodes Write programs using if… elif …else statement in Python ICDT 1201Y Computer Programming Control Structures The three main types of flow in a computer program: ○ sequential, in which instructions are executed successively, ○ conditional, in which the blocks “instructions 1” and “instructions 2” are executed if the Condition is True or False, respectively, and ○ repeating, in which instructions are repeated over a whole list. ICDT 1201Y Computer Programming Control Structures ICDT 1201Y Computer Programming 1.Decision Structure ICDT 1201Y Computer Programming Decision making statements Python programming language provides the following types of decision making statements. ○ if statements ○ if....else statements ○ if..elif..else statements ○ nested if statements ICDT 1201Y Computer Programming Simple Decisions Sequencing is not sufficient to solve every programming problem. To alter the sequence of flow in a program, a decision structure/control structure can be used to suit the needs of a particular situation. Conditional constructs are used to incorporate decision making into programs. The result of this decision making determines the sequence in which a program will execute instructions. ICDT 1201Y Computer Programming Decision in Programs Consider the following Problem: Draw the flowchart and write the program to calculate the salary of a person at the end of a month. The program should take as inputs the no_of_days and the rate_per_day. Does this problem involve any decision? ICDT 1201Y Computer Programming Flowchart for the program ICDT 1201Y Computer Programming The program in Python # Input num of days and rate per day no_of_days = int(input("Enter the number of days worked: ")) rate_per_day = float(input("Enter the rate per day: ")) # Calculate the salary total_salary = no_of_days* rate_per_day # Display the result print(f"The salary at the end of the month is: {total_salary:.2f}") ICDT 1201Y Computer Programming Problems involving decisions Consider the problem below: Draw the flowchart and write the program to calculate the salary of a person. You know the no_of_days and the rate_per_day. If the no_of_days is 25: bonus = 2000 else: bonus = 0 total_pay = salary + bonus # Display the result print(f"The total pay at the end of the month is: Rs. {total_pay}") ICDT 1201Y Computer Programming Example 5.2 Write a Program that takes, as input, a temperature in fahrenheit, converts it to celsius and displays: ○ The value of the temperature in Celsius ○ The message “It’s very hot” if the celsius value exceeds 35. ○ The message “Program Over” at the end of the program Write the pseudocodes design of the program, draw the flowchart and write the Python program. Note: Conversion: celsius = (fahrenheit –32) x 5/9 ICDT 1201Y Computer Programming Exercise 5.3 Modify the program in Exercise 5.2 so that it still displays the previous outputs, but additionally, if the temperature is below 12C, it displays “It’s quite cold”. Again write the pseudocode design, draw the flowchart and write the Python program ICDT 1201Y Computer Programming Using two way decisions The simple decision is used when we have no processing in case the condition evaluates to false. However, the more general case is when there is different processing depending on whether the condition is true or not. ICDT 1201Y Computer Programming Using two way decisions Consider the following example: Example 5.2 ○ A program allows the input of a student’s marks in a module. If the marks are 40 or above, the program displays “Passes”. If the marks are below 40, the program displays “Fails”. ○ Draw the flow chart. ICDT 1201Y Computer Programming Flowchart- example 5.2 ICDT 1201Y Computer Programming If … else statement: An if statement can be combined with an else statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. The else statement is an optional statement and there could be at most one else statement following an if. ICDT 1201Y Computer Programming The form of the if…else statement if Then else end if The condition is evaluated first. If the condition is true then the statement(s) in the if block are executed. Otherwise, the statement(s) in the else block is executed ICDT 1201Y Computer Programming Pseudocode if….else (example 5.2) Input marks If marks >= 40 Then output “Passes” else output “Fails” End if ICDT 1201Y Computer Programming Syntax of if..else in Python if condition: #block of codes else : #block of codes ICDT 1201Y Computer Programming Program in Python example 5.2 # Taking input from the user marks = float(input("Enter the student's marks: ")) # Checking the condition and displaying result if marks >= 40: print("Passes") else: print("Fails") ICDT 1201Y Computer Programming Exercise:5.4 A man stitches garments for a particular factory. If he stitches 100 pieces or more in a day, he is paid at the rate of Rs 35 per garment. Otherwise, he is paid at the rate of Rs 20 per garment. Write the pseudocode design, draw the flowchart, and write the Python program to calculate the total amount of money earned by the man for a given day. The input to the program should be the number of garments stitched ICDT 1201Y Computer Programming Multi way decision In some cases, our decision can take more that two possible paths. In such cases we have to make use of the elif statement ICDT 1201Y Computer Programming The form of if…elif and else statement If Then elif Then else End if ICDT 1201Y Computer Programming Example for illustration if x == 3: print “X equals 3.” elif x == 2: print “X equals 2.” else: print “X equals something else.” print “This is outside the ‘if’.” Note: Use of indentation for blocks Colon (:) after boolean expression ICDT 1201Y Computer Programming Another if form An alternative if form returns a value This can simplify your code Example: ○ return x+1 if x < 0 else x -1 ○ return ‘hold’ if delta==0 else sell if delta < 0 else ‘buy’ ICDT 1201Y Computer Programming The elif statement in Python The if/elif statement is a chain of if statements. They perform their tests, one after the other, until one of them is found to be true. The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if. ICDT 1201Y Computer Programming If elif else :Example 5.3 Consider the following problem. A student is asked to input his marks. If his score is >= 70, he passes with Grade A. Else If his score is below 70, but >= 40 he passes with Grade B. Else if his score < 40 he fails with Grade F. Write the pseudocode for the above problem to output the Grade of the student depending on his marks. ICDT 1201Y Computer Programming Pseudocode for example 5.3 Input marks If marks >= 70 Then output “Grade A” Else If marks >=40 Then Output “Grade B” Else Output “Grade F” End if ICDT 1201Y Computer Programming Program in Python # Input the student's marks marks = float(input("Enter the student's marks: ")) # Determine the grade based on the marks if marks >= 70: grade = 'A' elif marks >= 40: grade = 'B' else: grade = 'F' # Output the grade print(f"The student's grade is: {grade}") ICDT 1201Y Computer Programming Key points Conditional statements, such as if, elif, and else, enable us to execute specific blocks of code based on certain conditions. They help us create branching paths in our program. ICDT 1201Y Computer Programming