Question

In: Computer Science

There are various selection structures that can be used in programming. What are the different relational...

There are various selection structures that can be used in programming. What are the different relational operators used in selection structures in the Python programming language? Also, explain what short-circuiting is with respect to compound conditional expressions in Python. Provide examples to support your response in addition to constructive feedback on the structures and circumstances posted by your peers. Provide at least one reference to support your findings.

Solutions

Expert Solution

a) Relational operators used in selection structures are:

  • Less than <,
    • Example:
      if age < 18:
          print("Cannot vote")
  • greater than >,
    • Example:
      if age > 60:
          print("Senior citizen")
  • less-than-or-equal-to <=,
    • Example:
      if age <= 60:
          print("Not senior citizen")
  • greater than or equal to >=,
    • Example:
      if age >= 18:
          print("Can vote")
  • Equality operators: equal to ==, or not-equal to !=.
    • if i % 2 == 0:
          print("Even")
    • if i mod 2 != 0:
          print("Odd")

By short circuiting we mean the stoppage of execution of boolean operation if the truth value of expression has been determined already. The evaluation of expression takes place from left to right. In python, short circuiting is supported by various boolean operators and functions.

Short Circuiting in conditional operators

Conditional operators also follow short circuiting as when expression result is obtained, further execution is not required.

Example

# python code to demonstrate short circuiting
# using conditional operators

# helper function
def check(i):
   print ("geeks")
   return i

# using conditional expressions
# since 10 is not greater than 11
# further execution is not taken place
# to check for truth value.
print( 10 > 11 > check(3) )

print ("\r")

# using conditional expressions
# since 11 is greater than 10
# further execution is taken place
# to check for truth value.
# return true as 11 > 3
print( 10 < 11 > check(3) )


print ("\r")


# using conditional expressions
# since 11 is greater than 10
# further execution is taken place
# to check for truth value.
# return false as 11 < 12
print( 10 < 11 > check(12) )

Output:

False

geeks
True

geeks
False

Related Solutions

Which are the three control structures used in java programming. Which of these three control structures...
Which are the three control structures used in java programming. Which of these three control structures is used in every program or application
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at...
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at least 2 examples of relational expressions with enumeration types. Explain your examples
Discuss the various types of foundations and their selection with respect to different situations.
Discuss the various types of foundations and their selection with respect to different situations.
how can object -relational database be used to implement an SDBMS?
how can object -relational database be used to implement an SDBMS?
What are the different types of projects and organization structures used in supply chain environments? Also,...
What are the different types of projects and organization structures used in supply chain environments? Also, how do you go about calculating the critical path and the duration of a project?
What are 4 techniques that can be used for seller selection? Please explain them
What are 4 techniques that can be used for seller selection? Please explain them
Describe what a database system is and how it is used. What is a relational database...
Describe what a database system is and how it is used. What is a relational database system and how used in a company to benefit an organization? What does it mean to run a query?
• Relational Schema Create a relational database schema consisting of the four relation schemas representing various...
• Relational Schema Create a relational database schema consisting of the four relation schemas representing various entities recorded by a furniture company.   Write CREATE TABLE statements for the following four relation schemas. Define all necessary attributes, domains, and primary and foreign keys. Customer(CustomerID, Name, Address) FullOrder(OrderID, OrderDate, CustomerID) Request(OrderID, ProductID, Quantity) Product(ProductID, Description, Finish, Price) You should assume the following: Each CustomerID is a number with at most three digits, each OrderID is a number with at most five digits,...
Selection methods used for international assignments are probably not much different from the methods used in...
Selection methods used for international assignments are probably not much different from the methods used in domestic staffing decisions. However, culture has an impact in terms of how these procedures are applied and in the focus of each procedure.   True False Think and act globally means that a global enterprise must think about and prepare for a presence in a favored few select markets, not just its home region. True False Repatriating refers to the _________ of bringing the international...
What are the main types of organizational structures? What organizational structures are used to implement the...
What are the main types of organizational structures? What organizational structures are used to implement the multidomestic, global, and transnational international strategies?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT