The Conditional Structures with Example

What is control structure? 

List different types of control structures for writing programs.

A statement used to control the flow of execution in a program is called control structure. The basic control structures for writing programs are sequential, conditional and iterative structure.

State three basic control structures with the help of examples? 

Three basic control structures include sequential, conditional and iterative structure. The sequential structure executes the statements in the same order in which they are written in the program.

Conditional Structures

 The conditional structure executes the statement based on a condition. The examples are if, if else and switch. The iteration structure executes the statement repeatedly for a specific number of times. The examples are for, while and do while

When you must use curly braces || with a selection or iterative structure?

You must use the curly braces with a selection or iterative statement. when the body of the structure consists of multiple statements to be executed.

How does a conditional statement select a statement to execute?

A conditional statement selects a statement or set of statements to execute on the basis of a condition.

Define compound statement.

A set of two or more statements written in curly brackets [] is called compound statement.

What is a condition?

A condition is an expression that evaluates to true (1) or false (0).

What is relational operator? List six relational operators in C++.

 The relational operators are used to specify conditions in programs. They and produce result as true or false. The six relational operators in C ++ are >, <, compare two values >=, <=, and !=.

What is the meaning of A! =B?

The statement A!=B is a relational expression that checks if the value of A is not equal to the value of B. The result will be true if both values are not equal.

Differentiate between assignment operator (=) and equal to operator (==).

The symbol is an assignment operator that assigns a value to a variable. The symbol is a relational operator that checks if two values are equal or not.

What is relational expression? Give some examples.

A type of expression that consists of constants, variables and relational operators called relational expression. Some examples of relational expressions are A > B, A < B, A <= B , A > = B.

What are logical operators in C ++? Give examples.

Logical operators are used to evaluate compound conditions or reverse the logic of an expression. C++ provides three logical operators called AND (&&) operator. OR (|I) operator and NOT (1) operator. Some examples: are (a > 50) \&\&(b>50) (x > 50) ||(y<90) and (a>b) Q.12. Differentiate between relational and logical operators.

The relational operators compare two values and produce result as true or false. The logical operators combine multiple conditions or reverse a condition. The number of relational operators is six and the number of logical operators is three.

What is the use of AND operator? Give an example.

The symbol used for AND operator is (c). It is used to evaluate two conditions. It produces true if both conditions are true. It produces false result if any one condition is false f(a > 50k * dea < 100) cout<< "Good";

What is the use of OR operator? Give an example.

The symbol used for OR operator is (||). It is used to evaluate two conditions. It produces true if either condition is true. It produces false result if both conditions are false if (a>50|| a < 100 ) cout<< "Good";

Differentiate between if and if else statement.

The if statement executes a statement or set of statements if the condition is true. It executes nothing if condition is false. The if else statement is used to make two-way decisions. It executes one statement or set of statements if condition is true. It executes another statement or set of statements if condition is false.

Differentiate between if...else and if else... if statements.

if else statement executes one block of statement(s) when the condition is true and the other when it is false. It is used when there are two options and only one block of statements should be executed on the basis of a condition. 

If else if statement can be used to choose one block of statements from many blocks of statements. It is used when there are many options and only one block of statements should be executed on the basis of a condition.

What is "switch" statement?

The switch statement is a good alternate of if else if it can be used easily. when there are many choices available and only one should be executed. The switch construct is useful in the cases where selection is based on the value.

Distinguish between if...else and switch statement.

if else can evaluate int, float and other data types but switch can only evaluate char or int. if else statement can test for equality as well as for logical expression but switch only tests for equality if else uses one expression for two choices but switch uses one expression for multiple choices if else can check a range of values but switch cannot check a range.

Which data types can be used in the expression in the switch statement?

The data types that can be used in the expression in switch statement are int and char.

Write three advantages of switch statement in C++.

The switch statement is easier to use when there are multiple choices. It is easier to debug and understand. It is also easier to maintain.

Write some rules of using switch case in C++ program.

The case label must be an integer or a character Each case label must be unique. A switch statement can have only one default label. The default label can be used anywhere in switch statement The case label must end with colon. The default label is optional.

What happens if break is missed in case block?

If break is missed, all case blocks coming after matching case will also be executed.

Write the alternative of if...else statement in C++.

The conditional operator is an alternative of if else statement in C++ It is a decision-making structure. It is also called ternary operator as it uses three operands.

Why break statement is used in a switch statement?

The break statement in each case label is used to exit from switch body. If break is not used, all case blocks coming after matching case will also be executed.

Why should you use a default label in a switch statement?

The default block is used to execute a statement or set of statements. when the result of expression in switch statement does not match with any case one similarity and one difference between if...else and conditional operator.

Write The similarity is that both select a statement depending on a condition.

The difference is that conditional operator can choose between two values but if else can be extended to check multiple conditions to select one statement from many available statements.

What is conditional operator? Write its syntax/general form.

Conditional operator is a decision-making structure. It can be used in place of simple if else structure. It is also called ternary operator because it uses three operands. The syntax of conditional operator is as follows.

(condition) ? true-case statement: false-case statement;

Write C++ expressions. Assume that all variables have been properly defined and initialized.

1. x is even and y is odd (x and y are integers).

(x%20) &de (y%2==1)

2. x is not between-47.5 and +132.0 (x is double).

((-47.5 <x) bebe (x < 132.0))

3. x is either greater than 10, or less than or equal to -142.

(x>10) || (x=-142)

Post a Comment

1 Comments