[go: up one dir, main page]

0% found this document useful (0 votes)
127 views6 pages

PST - Unit 3

Unit 3 discusses selection structures and repetition structures in programming. Selection structures like if-else statements and elseif structures allow a program to make decisions and select between alternatives. Relational and logical operators return true or false and are used to evaluate conditions for selection. Repetition structures like for, while, and do-while loops repeat a block of code while a condition is true or for a set number of iterations, and are useful for tasks like input validation or running totals. Nested loops allow loops within other loops. Selection and repetition structures enhance programming by enabling decisions and repetitive tasks.

Uploaded by

cs235114414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views6 pages

PST - Unit 3

Unit 3 discusses selection structures and repetition structures in programming. Selection structures like if-else statements and elseif structures allow a program to make decisions and select between alternatives. Relational and logical operators return true or false and are used to evaluate conditions for selection. Repetition structures like for, while, and do-while loops repeat a block of code while a condition is true or for a set number of iterations, and are useful for tasks like input validation or running totals. Nested loops allow loops within other loops. Selection and repetition structures enhance programming by enabling decisions and repetitive tasks.

Uploaded by

cs235114414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 3

3.1 SELECTION STRUCTURES


Use to make a decision or comparison and then, based on the result of that decision or
comparison, to select one of two paths. The condition must result in either a true (yes)
or false (no) answer. If the condition is true, the program performs one set of tasks.
Three Types of Selection Structures
 if-end selection structures are used when only one boolean condition is necessary.
 if-else selection structures are used when only one boolean condition is necessary.
 elseif structures are a way to combine multiple boolean conditions into a single
selection structure.
if-end selection structures
if-end selection structures are used when only one boolean condition is necessary. In
if-end structures, a process will be only be performed if the boolean condition is true.

if-else selection structures


if-else selection structures are used when only one boolean condition is necessary. In
if-else structures, a specific action will be performed if the boolean condition is true
and another action, if the condition is false.
Elseif Structure

if-else selection structures are used when only one boolean condition is necessary. In
if-else structures, a specific action will be performed if the boolean condition is true
and another action, if the condition is false.

3.2 RELATIONAL AND LOGICAL OPERATORS


Relational operators compare values and return either TRUE or FALSE. Logical
operators perform logical operations on TRUE and FALSE. Values used with a
logical operator are converted into booleans prior to being evaluated. For numerical
values, zero will be interpreted as FALSE, and other values will be TRUE.
Relational Operators

Relational operators are used to compare two values in C language. It checks the
relationship between two values. If relation is true, it returns 1. However, if the
relation is false, it returns 0.
Here is the table of relational operators in programming language

Logical Operators

Logical operators are used to perform logical operations. It returns 0 or 1 based on the
result of condition, whether its true or false. These operators are used for decision
making in C language.
Here is the table of logical operators in programming language,

3.3 SELECTING FROM SEVERAL ALTERNATIVES


Conditional Control Statements allows the program to select between the alternatives
during the program execution. They are also called as decision-making statements or
selection statements.

3.4 APPLICATIONS OF SELECTION STRUCTURES


Selection Structure. Use to make a decision or comparison and then, based on the
result of that decision or comparison, to select one of two paths. The condition must
result in either a true (yes) or false (no) answer. If the condition is true, the program
performs one set of tasks.

Selections enable including more than one route in a program. Many solutions require
multiple choices or decisions, and these choices result in various possible routes
which the program can take. These routes signify the outcome of making a choice.
Repetition Structures
Repetition structures are used to repeat statements or blocks of code. The decision
whether to repeat the code is based on the evaluation of a logical expression. If the
expression is true, the code is executed.
Examples of such repetition include continual checking of user data entries until an
acceptable entry, such as a valid password, is made; counting and accumulating
running totals; and recurring acceptance of input data and recalculation of output
values that only stop upon entry of a designated value.

3.5 COUNTER CONTROLLED LOOPS


A counter controlled loop is also known as definite repetition loop, since the number
of iterations is known before the loop begins to execute. The counter-controlled loop
has the following components: a control variable.

A count-controlled loop is used when the number of iterations to occur is already


known. A count-controlled loop is so called because it uses a counter to keep track of
how many times the algorithm has iterated.

For Loops

As discussed above, a For Loop is an entry controlled Loop. The general syntax of a
For Loop is given below.

for(initialization; condition; incrementation or decrementation)


{
Body of Loop;
}
The variables required for the control statements can be initialized in the For Loop
itself. This variable initialized in the For Loop is called the counter and is incremented
or decremented with every iteration of the Loop. The condition is a boolean statement
that compares the value of the counter to a fixed value, at every iteration, and
terminates the Loop when the condition is not satisfied. The increment or decrement
value is set in the Loop.

The while Loop

A while loop available in C Programming language has the following syntax −


while ( condition )
{
/*....while loop body ....*/
}
The above code can be represented in the form of a flow diagram as shown below –

The following important points are to be noted about a while loop −


 A while loop starts with a keyword while followed by a condition enclosed in ( ).
 Further to the while() statement, you will have the body of the loop enclosed in
curly braces {...}.
 A while loop body can have one or more lines of source code to be executed
repeatedly.
 If the body of a while loop has just one line, then its optional to use curly
braces {...}.
 A while loop keeps executing its body till a given condition holds true. As soon as
the condition becomes false, the while loop comes out and continues executing
from the immediate next statement after the while loop body.
 A condition is usually a relational statement, which is evaluated to either true or
false. A value equal to zero is treated as false and any non-zero value works like
true.

The do...while Loop


A while loop checks a given condition before it executes any statements given in the
body part. C programming provides another form of loop, called do...while that
allows to execute a loop body before checking a given condition. It has the following
syntax −
do {
/*....do...while loop body ....*/
}
while ( condition );
The above code can be represented in the form of a flow diagram as shown below −

Nested Loops
A nested loop is a loop inside another loop. Although all kinds of loops can be nested,
the most common nested loop involves for loops. These loops are particularly useful
when displaying multidimensional data. When using these loops, the first iteration of
the first loop will initialize, followed by the second loop.
Syntax:

for ( initialization; condition; increment )


{
for ( initialization; condition; increment )
{
// statement of inside loop
}

// statement of outer loop


}

3.6 APPLICATION OF REPETITION STRUCTURES


Repetition allows the programmer to efficiently use variables.
Can structure programming statements to be repeated as long as specific conditions
are met. For example: can input, add, and average multiple numbers using limited
number of variables.

To conclude, repetition further enhances the capabilities of the programs we write by


allowing us repeatedly execute instructions. Repeating instructions saves us from
having to repeatedly type instructions in cases where we know the number of times
we want an instruction to be executed.

You might also like