Computer Science, asked by mishrakartik139, 6 months ago

Control structures are broadly categorized into?Computer​

Answers

Answered by kajalamrita09
2

Explanation:

Flow of Control:

Flow of control through any given function is implemented with three basic types of control structures:

Sequential: default mode. Sequential execution of code statements (one line after another) -- like following a recipe

Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In C++, these are the types of selection statements:

if

if/else

switch

Repetition: used for looping, i.e. repeating a piece of code multiple times in a row. In C++, there are three types of loops:

while

do/while

for

The function construct, itself, forms another way to affect flow of control through a whole program. This will be discussed later in the course.

Some useful tools for building programs or program segments

pseudocode - helps "think" out a problem or algorithm before trying to code it

flowcharting - graphical way to formulate an algorithm or a program's flow

stepwise refinement (top-down design) of algorithms

True and False

Selection and repetition statements typically involve decision steps. These steps rely on conditions that are evaluated as true or false

C++ has a boolean data type (called bool) that has values true and false. Improves readability.

Most functions that answer a yes/no question (or a true/false situation) will return a boolean answer (or in the case of user-defined functions, they should be coded that way)

Important: ANY C++ expression that evaluates to a value (i.e. any R-value) can be interpreted as a true/false condition. The rule is:

If an expression evaluates to 0, its truth value is false

If an expression evaluates to non-zero, its truth value is true

Logical Operators:

The arithmetic comparison operators in C++ work much like the symbols we use in mathematics. Each of these operators returns a true or a false.

x == y // x is equal to y

x != y // x is not equal to y

x < y // x is less than y

x <= y // x is less than or equal to y

x > y // x is greater than y

x >= y // x is greater than or equal to y

We also have Boolean operators for combining expressions. Again, these operators return true or false

x && y // the AND operator -- true if both x and y are true

x || y // the OR operator -- true if either x or y (or both) are true

!x // the NOT operator (negation) -- true if x is false

These operators will be commonly used as test expressions in selection statements or repetition statements (loops).

Examples of expressions

(x > 0 && y > 0 && z > 0) // all three of (x, y, z) are positive

(x < 0 || y < 0 || z < 0) // at least one of the three variables is negative

( numStudents >= 20 && !(classAvg < 70))

// there are at least 20 students and the class average is at least 70

( numStudents >= 20 && classAvg >= 70)

// means the same thing as the previous expression

Short Circuit Evaluation:

The && and || operators also have a feature known as short-circuit evaluation.

In the Boolean AND expression (X && Y), if X is false, there is no need to evaluate Y (so the evaluation stops). Example:

(d != 0 && n / d > 0)

// notice that the short circuit is crucial in this one. If d is 0,

// then evaluating (n / d) would result in division by 0 (illegal). But

// the "short-circuit" prevents it in this case. If d is 0, the first

mark me as brainliest

Similar questions