0% found this document useful (0 votes)
29 views3 pages

Describe The Theory With Programming Examples The Selection Control Statements in C+ +. Answer

The selection control statements in C++ allow programmers to conditionally execute blocks of code based on the evaluation of logical conditions. There are two possible paths that can be taken: the true path if the condition is met, or the false path if it is not met. Common selection statements include if-else statements to assign grades based on test scores or take the square root of a number only if it is non-negative. Programmers can flexibly write conditions to control which block of code executes by changing the logical checks used.

Uploaded by

Rohma Surti
Copyright
© Attribution Non-Commercial (BY-NC)
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)
29 views3 pages

Describe The Theory With Programming Examples The Selection Control Statements in C+ +. Answer

The selection control statements in C++ allow programmers to conditionally execute blocks of code based on the evaluation of logical conditions. There are two possible paths that can be taken: the true path if the condition is met, or the false path if it is not met. Common selection statements include if-else statements to assign grades based on test scores or take the square root of a number only if it is non-negative. Programmers can flexibly write conditions to control which block of code executes by changing the logical checks used.

Uploaded by

Rohma Surti
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 3

2.

Describe the theory with programming examples the selection control statements in C+
+.
Answer-
The algorithm you are developing may need to do some actions based upon a decision.
For example, an algorithm that is evaluating a formula can check to see if a number is negative
before taking the square root of that number and then proceed differently depending on whether
the result will be a normal number or a complex number. Another example is processing grades.
If the grade is above 90%, an A is assigned to the student, between 80% and 90%, a B is
assigned to the student, and so on.

A selection-control statement controls whether or not a collection of code is executed or which


collection of code is executed. In the inset diagram, the diamond shape represents a decision that
when executed could result in an answer of Yes or No (True or False). Depending on what the
answer is, the flow of control will follow the appropriate path. In the example, either statement
2a or statement 2b will be executed. One of them will be executed, but not both. However,
regardless of which statement 2 was executed, statement 3 will always be executed.

There need not be a statement 2a or a statement 2b. Either path could be empty or could contain
several statements. It would be silly for both of them to be empty (or both have the exact same
statements), as your decision, Yes or No, would have no effect (nothing different would happen
based on the decision).

Selection Control examples


This section presents some concrete uses of selection control statements.

The exact decision you write down affects whether the Yes or No paths are taken. It is very easy
to change the decision to switch the yes and no paths. For example, on the left below is a simple
decision on whether a student’s GPA makes the grade for being on the Dean’s list. On the right
are two different rephrasings of the decision such that the No path is taken if the student is on the
Dean’s list. Some people prefer the version on the left and some the version on the top right.
Very few prefer the bottom right because of the double negative implicit in the decision.

F-1
F-2
F-3

You might also like