![]() |
VOOZH | about |
Decision-making statements allow programmers to determine which statements should be executed under different conditions. There are four primary types of decision-making statements:
This type of statement simply checks the condition and if it is true then the statements within it are executed but if it is not then the statements are simply ignored in the code.
Syntax:
if ( condition ){
// body of if
}
Example:
Condition is trueThis type of statement simply checks the condition and if it is true, the statements within are executed but if not then else statements are executed.
Syntax:
if ( condition ){
// body of if
}
else {
// body of else
}
Example:
Condition is falseThis type of statement simply checks the condition and if it is true the statements within it are executed but if it is not then other if conditions are checked, if they are true then they are executed, and if not then the other if conditions are checked. This process is continued until the ladder is completed.
Syntax:
if ( condition1 ){
// body of if
}
else if ( condition2 ){
// body of if
}
.
.
.
else {
// statement
}
Example:
Condition 3 is trueThis type of statement checks the condition and if it is true then the if statement inside it checks its condition and if it is true then the statements are executed otherwise else statement is executed.
Syntax:
if ( condition1 ){
if ( condition2 ){
// Body of if
}
else {
// Body of else
}
}
Example:
All the conditions are falseCondition false
6
6
In programming, decision-making statements are crucial because they help dictate how a program runs based on certain conditions. There are four main types of these statements:
These decision-making structures are essential for creating logical flows in programs, ensuring they execute efficiently and respond appropriately to different situations.