Select Structures PDF
Document Details
Uploaded by Deleted User
Strathmore University
2024
Tags
Summary
This PDF document details programming concepts in C++, focusing on conditional statements like 'if' and 'switch', relational operators, and logical expressions. It provides explanations, examples, and pseudocode for better understanding. The document appears to be university lecture notes or a similar resource for introduction to programming.
Full Transcript
Select Structures Outline Introduction Representing conditions : relational operators, logical expressions if statements switch - case statement 11/1/2024 Introduction to Programming Introduction Practical progr...
Select Structures Outline Introduction Representing conditions : relational operators, logical expressions if statements switch - case statement 11/1/2024 Introduction to Programming Introduction Practical programs should always be implemented to allow a user to make a selection/Decision among available items/options/functionality. Eg – Male or Female – Single or Married – Age greater than 18 or Less than 18 – Single Player or Multiplayer This option/functionality is usually a condition that must be evaluated to either True or False. Each selection implements different instructions from the other 11/1/2024 Introduction to Programming Example Write an algorithm to find the smallest number between two numbers entered by a user and display the results. In pseudocode form we get if condition is true Then do task A else (condition is false) Do Task-B endif 11/1/2024 Introduction to Programming Pseudocode and Flowchart to find the smallest of two numbers (Selection/Decision/Branching) 11/1/2024 Introduction to Programming Logical expressions and the relational operators In C++, a condition is represented by a logical expression /Boolean expression – has a value of either true or false. Suppose i and j are integers. Consider the expression: i > j 11/1/2024 Introduction to Programming Comparison Operators Manipulating and Combining Logical expressions Logical /Boolean operators enable you to combine logical expressions. C++ has three logical /Boolean operators 11/1/2024 Introduction to Programming If and switch Statements In C++, there are two selections, or branch control structures: – if statements – variations - if, if..else, if-else if, Nested if – switch structure. 11/1/2024 Introduction to Programming if statement if- One way selection if.. else – Two way selection extended if /if..else..if – multiple selections Nested if 11/1/2024 Introduction to Programming Syntax IF – One way Selection 11/1/2024 Introduction to Programming If…else – Two Way Selection 11/1/2024 Introduction to Programming Extended If – Multiple Selections 11/1/2024 Introduction to Programming Nested if 11/1/2024 Introduction to Programming If statement example Switch selection Structure C++’s switch structure gives the computer the power to choose from among many alternatives. Unlike the IF selection structure, the switch structure does not necessarily require a logical expression to be passed directly in the switch body. 11/1/2024 Introduction to Programming Switch statement example break The break statement may or may not appear after each statement. The break statement, when executed in a switch structure, provides an immediate exit from the switch structure. 11/1/2024 Introduction to Programming Any Questions?