![]() |
VOOZH | about |
Conditional statements help a program make decisions. They check whether a condition is true or false and execute different blocks of code based on the result. This allows programs to behave differently in different situations.
The if statement checks a condition and executes a block of code only when the condition is true.
x is positive
The if-else statement checks a condition and runs one block of code if the condition is true, and another block of code if the condition is false.
Flowchart of If-Else Statement
x is positive
The if-else if statement is used to check multiple conditions. The program evaluates each condition one by one and executes the block of code for the first condition that is true.
Flowchart of If-Else if Statement
x is zero
The switch statement checks a variable against multiple possible values. Each option is written as a case, and the program executes the matching case. A break statement is usually used to stop execution after a case runs.
Flowchart of Switch Statement
👁 switch-case-in-cRules of the Switch Statement
When using switch statements, there are a few important rules.
Tuesday
1. Constant Expression: A switch expression must evaluate to a constant value. This can include constants or arithmetic operations.
Result is 15.
2. Limited to Certain Types: Switch statements are mainly designed for int, char, or string values depending on the language.
Good!
The ternary operator is a short way to write an if-else statement. It evaluates a condition and returns one value if the condition is true, and another value if the condition is false.
Flowchart of Ternary Condition
👁 conditional or ternary operator in c
x is positive
Nested Ternary Condition: A nested ternary condition is a ternary operator placed inside another ternary operator. It is used when you need to check multiple conditions in a single line. The inner ternary executes only if required by the outer condition.
Flowchart of Nested Ternary Condition
x is zero