An expression refers to a combination of variables, operators, and constants. A combination of expressions, known as compound expression, is useful in conditional and looping construct. Furthermore, conditional and looping constructs play an important role in computer science.
Conditional control statements, also known as selection or branching statements, facilitate the execution of one group of instructions depending on the outcome of a decision. As far as a conditional construct is concerned, the python programming language provides three types of decision making statements as shown below:
if statements: It involves a Boolean expression that is followed by one or more statements.
If…….else statement: It is possible to follow this statement by an optional else statement, the execution of which takes place when the Boolean expression turns out to be false.
Nested if statement: There is a possibility to use one if or else if statement inside another such statement(s).
Selection structures would be choosing among alternative courses of action. For example, suppose that the passing grade for a particular examination is 40. In such a case, the pseudo-code statement would be
If student’s grade is equal to 40 or greater than it
It leads to the determination of whether the condition “student’s grade is equal to or greater than 40” is true or false. If the condition is true, then “Passed” gets printed.
If the condition is true, then one must do the indented statements. Moreover, if the condition is not true, then one must skip the indented statements.
if balance < 0:
transfer = -balance
Most noteworthy, backupAccount = backupAccount – transfer # take enough from the backup acct.
balance = balance + transfer
In the examples above, the choice is between doing something or nothing. Furthermore, one must do something if the condition is true or nothing if the condition turns out to be false. There may often be occasions that offer a choice between two possibilities, only one of which will be done depending on the truth of a condition.
Browse more Topics Under Conditional Constructs and Looping
The looping construct mean repetition of a set of statements on the basis of a condition test. Furthermore, till the time a condition turns out to be true or false depending upon the loop, the repetition of a set of statements takes place again and again. Most noteworthy, the repetition ceases as soon the condition becomes true or false.
Two types of looping constructs exist in Python– while loop and for loop.
The ‘while statement’ allows one to perform repeated execution of a block of statements as long as a condition is true. Furthermore, a ‘while statement’ is a perfect example of a looping statement. Moreover, a ‘while statement’ can also be characterized by an optional else clause.
A while loop is a major type of looping construct. Some examples of while loop are:
num = 5
while num != O:
num = num — 1
This loop prints a num whose starting is from 5 and it goes down to 1, until the num becomes 0 which is the loops’s terminating condition.
The for..in statement is another looping statement whose iteration takes place over any sequence of objects i.e. go through each item in a particular sequence like a string or a list. This a major type of looping construct. It is one of the two main looping constructs examples.
for a in [1,4,7]:
FAQs For Use of compound expression in conditional and looping construct
Question 1: What is the concept of loop in computer science?
Answer 2: In computer science, a loop refers to a programming structure that facilitates the repetition of a sequence of instructions until the meeting of a specific condition. Furthermore, programmers make use of loops to cycle through values, perform addition of the sums of numbers, repeat functions, and other things.
Question 2: Name the four elements of the python while loop?
Answer 2: The four elements of the while loop in python are the initialization of usually a loop control variable, a continuation test to check whether to do another iteration, an update step, and the element of the loop body.