Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Conditional Statements

Paul
What is a conditional
statement
An "if statement" is a control structure in used
to make decisions based on certain conditions.
It allows the program to execute a specific
block of code only if a given condition is true.
How does it work?
C has the following conditional statements:
 Use if to specify a block of code to be executed, if a specified condition is true
 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false

Later we will look at the switch…

 Use switch to specify many alternative blocks of code to be executed


Structure of the “if statement”
Use the if statement to specify a block of code to be executed if a condition is true.

If(condition) //the condition can be true or false


{
//write the code here to execute when the statement is true then the program will continue
}
//program will continue here after executing the true and when the statement is false
If(condition) //the condition can be true or false
{
//write the code here to execute when the statement is true then the program will continue
}
Continue here
If(condition) //the condition can be true or false
{
//write the code here to execute when the statement is true then the program will continue
}
Continue here
If(condition) //the condition can be true or false
{
//write the code here to execute when the statement is true then the program will continue
}
Continue here
Structure of “if and else condition”
If(condition) //condition can be true or false
{
//the code will jump here when the condition is true then we will proceed to continue

}
//you cannot put anything here
else
{
//and we execute here when the condition is false then we will proceed to continue

}
// program will continue here after executing the true and when the statement is false
Structure of the “if else if condition”
If(condition 1) //condition can be true or false
{
//the code will jump here when the condition is true then we will proceed to continue

}
//you cannot put anything here
else if (condition 2)
{
//and we execute here when the condition is false then we will proceed to continue

}
// program will continue here after executing the true and when the statement is false
If(condition) //condition 1 can be true or false
{
//the code will jump here when condition 1 is true then will proceed to continue

}
else if (condition 2) // we will check condition 2 if 1 is false and it can be true or false
{
//and we execute here when the condition 2 is true then we will proceed to continue

}
else
{
//this part of the code will be executed when both condition 1 and 2 are false then the program will continue

}
// program will continue here after executing the true and when the statement is false

*NOTE!!! ONLY ONE OF THESE CONDITIONS WILL BE EXECUTED


If(condition 1) //condition 1 can be true or false
{
//the code will jump here when condition 1 is true then will proceed to continue

}
else if (condition 2){ //we execute here if the previous condition was false then we will proceed to continue }
else if (condition 3){ //we execute here if the previous condition was false then we will proceed to continue }
else if (condition 4){ //we execute here if the previous condition was false then we will proceed to continue }
else if (condition 5){ //we execute here if the previous condition was false then we will proceed to continue }
else if (condition X){ //we execute here if the previous condition was false then we will proceed to continue }
else (condition X+1)
{
//and we execute here if the previous condition was false then we will proceed to continue

}
// program will continue here after executing the true and when the statement is false
*NOTE!!! ONLY ONE OF THESE CONDITIONS WILL BE EXECUTED
Rules for Conditional Statements
1 Indentation Proper indentation is essential for code readability. Use consistent indentation (e.g., using tabs or spaces) to represent
nested if statements clearly
2 Syntax Ensure you use the correct syntax for if statements in C. The if statement should be followed by a condition in parentheses,
and the code block to be executed if the condition is true should be enclosed in curly braces.
3 Boolean The condition inside the if statement must be an expression that evaluates to a boolean value (0 for false, any non-zero
Expression value for true). Use comparison operators (e.g., `<`, `>`, `==`, etc.) or logical operators (e.g., `&&`, `||`, `!`) to form the
condition
4 Use of Use parentheses to group parts of the condition if needed for clarity and to ensure the correct evaluation order.
Parentheses
5 Proper Nesting Avoid excessive nesting of if statements. Keep the code as flat as possible and consider using else if or switch statements
for multiple conditions.
6 Consistent Use descriptive and consistent variable names for the condition and any variables used inside the if block.
Naming
7 Complete Make sure all possible cases are covered, especially when using if-else or if-else if-else structures.
Coverage
8 Bracing Style Decide on a bracing style and stick to it. Common styles include K&R style and Allman style.

9 Readability Write if statements in a way that is easy to read and understand. Add comments if the conditions or logic are complex.

10 Testing Test your if statements with different input values to ensure they behave as expected and handle various cases correctly.
Notes
 Conditional statement cannot start with an “else”. Incorrect usage includes using "else" without a
preceding condition.
 The "else" keyword acts as a fallback option when none of the previous conditions are met.
 "else" must always follow an "if" or "else if" statement.
 Always try handling multiple conditions using "else if" instead of standalone "else."
Conditions

You might also like