Conditional Constructs and Looping

Use of Compound Expression in Conditional and Looping Construct

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.

looping construct

Looping Construct

Conditional Construct

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

Print “Passed”

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.

Example:

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

Looping Construct

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.

while 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:

Example 1:

num = 5

while num != O:

print (num)

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.

for loop:

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.

Example 1: 

for a in [1,4,7]:
print (a)
output:
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.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

Which class are you in?
No thanks.