Estruturas Condicionais 2 - Curso de Algoritmos #08 - Gustavo Guanabara
Conditional Structures in Algorithms: Part 2
Introduction to Nested Conditionals
- Gustavo Guanabara introduces the lesson on conditional structures, focusing on nested conditionals and choice structures, building upon previous lessons.
- The importance of following the course sequentially is emphasized for better understanding.
Understanding Conditional Scenarios
- A simple example of a conditional structure is presented: "If I have a lot of money, I'll go to Disney; otherwise, I'll stay home."
- Real-life scenarios often involve multiple choices. For instance, having different amounts of money leads to various travel options beyond just two outcomes.
Implementation of Nested Conditionals
- The concept of nested conditionals is introduced with an algorithmic approach using Visualg. It illustrates how one conditional can exist within another.
- An example code snippet demonstrates how to check financial conditions and decide between going to Disney or staying home based on available funds.
Practical Application: Student Evaluation
- A practical scenario involving student evaluation (approved, disapproved, or in recovery) is discussed as a real-world application of nested conditionals.
- The algorithm begins by asking for two student grades and calculating their average while ensuring proper order in operations.
Evaluating Student Performance
- Conditions are set for determining if a student passes (average ≥ 7), fails, or is in recovery (average between 5 and 7).
- The logic flow includes checking multiple conditions using nested statements to accurately reflect the student's status based on their average grade.
Conclusion and Further Exploration
- The importance of indentation in coding for clarity when dealing with nested structures is highlighted.
Understanding BMI Calculation and Conditional Structures in Programming
Introduction to BMI Ranges
- The previous class focused on calculating Body Mass Index (BMI) to determine if individuals fall within the ideal weight range, highlighting various categories from underweight to morbid obesity.
- The current session aims to refine the algorithm by eliminating unnecessary conditional structures while maintaining accurate BMI calculations.
Implementing Nested Conditionals
- The calculation of BMI is defined as weight divided by height squared. Initial focus will be on categorizing individuals based on their BMI values.
- A nested conditional structure will be used, with tips for code organization:
- Keep code indented for clarity.
- Always close an 'if' statement with 'End If' to avoid errors.
Defining Weight Categories
- Conditions are set for different BMI ranges:
- Very Underweight: BMI < 17 results in "Very underweight".
- Underweight: 17 ≤ BMI ≤ 18.5 results in "Under weight".
- Additional conditions include:
- Ideal Weight: 18.5 ≤ BMI < 25 results in "ideal weight".
- Overweight: 25 ≤ BMI < 30 results in "Overweight".
Advanced Obesity Classifications
- Further classifications include:
- Obesity: 30 ≤ BMI < 35.
- Severe Obesity: 35 ≤ BMI < 40.
- Finally, any individual with a BMI ≥ 40 is classified as having morbid obesity.
Testing the Algorithm
- The instructor demonstrates testing the algorithm using various weights and heights, confirming that it accurately categorizes individuals based on their calculated BMIs.
- Examples include:
- A person weighing 40 kg at a height of two meters has a low BMI.
- A person weighing over200 kg at one meter tall falls into morbid obesity.
Exploring Alternative Conditional Structures
- An introduction to another useful conditional structure called "choice" is presented, which simplifies decision-making processes in programming.
- This structure allows for selecting cases based on variable input without complex nested conditions.
Practical Application of Choice Structure
- An example program titled “Child Hope” illustrates how donations can be processed using the choice structure:
- Users select donation amounts or cancel their donation through simple case statements.
Conclusion and Program Execution
- The program concludes by thanking users for their contributions and displaying the total amount donated based on user input selections.
Conditional Structures in Programming
Understanding Conditional Structures
- The choice of structure in programming does not test a range of values but rather checks specific conditions, such as greater than or equal to, or less than. This is primarily applicable for integer values.
- It is advised to use only integer values with conditional structures; they are not universally applicable across all situations.
Example Algorithm: Employee Salary Calculation
- An example algorithm prompts the user for an employee's name, salary, and number of dependents. Variables include
name(character type),salary(real type), anddependents(integer).
- If an employee has no dependents, their salary increases by 5%. The calculation involves multiplying the salary by 5% and adding it to the original salary.
- For employees with 1 to 3 dependents, there is a 10% increase; with 4 to 6 dependents, a 15% increase; and more than 6 dependents results in an 18% increase.
Salary Calculation Examples
- Using a fixed salary of $1000 for simplicity:
- José has three dependents and receives a new salary of $1100 after a 10% increase.
- Maria has no dependents and only receives a $50 raise (5%).
- André earns $1000 with five dependents resulting in a new salary of $1150 due to the 15% increase.
- Claudia earns $1000 with nine dependents, leading to an increased salary of $1180 after an 18% raise.
Summary of Conditional Structures
- The lesson concludes by summarizing various conditional structures discussed throughout the course: simple conditionals, composite conditionals, lined conditionals, and case choices.
Proposed Exercises
Student Grade Classification Exercise
- An exercise involves calculating student grades based on averages:
- A: Average between 9 and 10
- B: Average between 8 and <9
- C: Average between 7 and <8
- D: Average between 6 and <7
- E: Average between 5 and <6
- F: Below average of <5
Football Match Analysis Exercise
- Another exercise analyzes football match outcomes based on goals scored:
- Different scenarios are presented based on goal differences that determine win/loss/tie situations.
Conclusion & Next Steps
- The lesson wraps up by emphasizing understanding conditional structures before moving on to repetition structures in future lessons.
Sign Up and Course Access
Course Enrollment Process
- To enroll in the course, users need to click on a specific link to sign up. This process includes authorizing the receipt of emails.
- After signing up, students gain access to all classes organized into playlists available on cursoemvideo.com.
- The course "Algorithm" is highlighted, with answers provided one week after each lesson's availability.
Practice and Answer Availability
- Students have a week to practice before viewing the answers; this encourages independent problem-solving.
- Answers for class 8 will be released alongside those for class 7, ensuring that students who follow the weekly schedule can benefit from timely assistance.