Conditional control in computer science refers to the ability of a program to make decisions and alter its behavior based on certain conditions. It allows programmers to create flexible and dynamic applications that can respond to different inputs and situations. Conditional control is a fundamental concept in programming and is implemented through conditional statements, loops, and branching structures.
1. **Conditional Statements**: Conditional statements are used to execute certain code blocks based on whether a specified condition evaluates to true or false. The most common conditional statement is the "if" statement. In its simplest form, an "if" statement checks a condition and executes a block of code if the condition is true. For example:
```python
x = 10
if x > 5:
print("x is greater than 5")
```
In this example, the code inside the "if" statement will only execute if the condition "x > 5" evaluates to true.
2. **Else and Else If**: In addition to the "if" statement, there are "else" and "else if" (or "elif" in some languages) statements that allow for more complex decision-making. The "else" statement is used to execute a block of code if the condition of the preceding "if" statement is false. "Else if" statements provide an alternative condition to check if the preceding "if" condition is false. For example:
```python
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
```
In this example, if the condition "x > 5" is false, the code inside the "else" block will execute.
3. **Nested Conditionals**: Conditional statements can be nested within each other to create more complex decision structures. This allows for multiple conditions to be checked sequentially. For example:
```python
x = 10
if x > 5:
if x < 15:
print("x is between 5 and 15")
else:
print("x is greater than or equal to 15")
else:
print("x is not greater than 5")
```
In this example, the second "if" statement is nested within the first "if" statement, creating a hierarchical decision structure.
4. **Loops**: Loops are another form of conditional control that allow a block of code to be executed repeatedly as long as a specified condition is true. There are different types of loops, including "while" loops and "for" loops.
```python
# Example of a while loop
x = 0
while x < 5:
print(x)
x += 1
# Example of a for loop
for i in range(5):
print(i)
```
In the first example, the code inside the "while" loop will execute repeatedly as long as the condition "x < 5" is true. In the second example, the "for" loop will iterate over the range of values from 0 to 4 and execute the code inside the loop for each value.
5. **Switch Statements**: Some programming languages support switch statements, which allow for multi-way branching based on the value of an expression. Switch statements can be more concise and readable than long chains of "if-else" statements when dealing with multiples
2. Conditional Control
Flow
• Conditional control flow is also referred to as conditional logicor
selection logic.
• It is one of the order in which the program instructions are executed.
• Its execution order differs from the sequential logic.
• It executes the instructions on the basis of one or more conditions.
• If the conditions are satisfied it will execute the instructions else
the instructions will not be executed (skipped).
3. Conditional Control
Flow
In conditional logic:
• Statements are executed on the basisof conditions.
• If conditions are satisfied the statements are executed.
• If conditions are not satisfied the statements are
skipped.
4. Conditional Control Structures in
C++
• Conditional control structures are used to execute set of statements on
the basis of one or more conditions.
• The statements that are needed to be executed conditionally are placed
with in the conditional structures and one or more conditions are
specified.
• The conditional control structures implement the
conditional/selection logic in C++.
5. if Statement in
C++
• if statement implements one-way selection logic.
• It executes the statement(s) on the basis of one
condition.
• If the condition is true, it executes if block.
• If the condition is false, it does not execute if block.
8. if Statement –
Syntax
• There are two syntaxes of if statement.
• In first syntax we have multiple statements inside the if statement.
• In this case it is compulsory to enclose all the statements in the braces
{ }
.
• In second syntax we have just one statement inside the if statement.
• In this case it is optional to enclose the statement in thebraces { } .
• All the statements enclosed in {} is called as the block.
11. if Statement -
Example
Problem Statement 2: If the number is multiple of 5 then add 1 to it
and display the resultant number.
12. If-else Statement in
C++
• If-else statementimplements two-way selection logic.
• It executes the statement(s) on the basis of one
condition.
• If the condition is true, it executes if block.
• If the condition is false, itexecutes else block.
13. If-else Statement –
Syntax
if ( condition )
{
statement set 1
;
}
else
{
statement set 2
;
if ( condition )
statement set 1
;
else
statement set 2
;
16. If-else Statement –
Syntax
• There are two syntaxes of if-else statement.
• In first syntax we have multiple statements inside the if-else statement.
• In this case it is compulsory to enclose all the statements in the braces
{ }
.
• In second syntax we have just one statement inside the if-else
statement.
• In this case it is optional to enclose the statement in thebraces { } .
19. switch Statement in
C++
• switch implements choice-way selection logic.
• It executes the statement(s) on the basis of expression and available choices.
• It first evaluates the expression and matches the value of expression to
available choices (cases).
• If the value matches with first case, then case 1 body is executed.
• If the value matches with second case, then case 2 body is executed.
• And the process continues up to nth case.
• If the value does not matches with any of the case, then default case
body is executed.
20. switch Statement –
Syntax
switch ( expression )
{
case 1 :
statement set 1
;
break ;
case 2 :
statement set 2
;
break ;
case 3 :
statement set 3
;
break ;
default :
22. switch Statement –
Syntax
• In switch statement, first the expression is evaluated to a single value.
• In expression we can only use the value of data types: char, short,int
and
long. No any other data types is accepted.
• We cannot use relational operation in switch statement.
• switch statement always contain one expression and multiple cases.
Where each case is ended by break statement.
• Last case is the default case (it is optional), it is executed when the
evaluated value does not match with any of thecase.