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

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
If(condition)
{
If(condition)
{
If(condition)
{
If(condition)
{

}
}
}
}
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

Operator Name Description Example Operator Name Example

== Equal to x == y
Returns true if both x < 5 && x <
&& Logical and
statements are true 10 != Not equal x != y

> Greater than x>y

Returns true if one of < Less than x<y


|| Logical or x < 5 || x < 4
the statements is true

>= Greater than or equal to x >= y


Reverse the result,
!(x < 5 && x <
! Logical not returns false if the
10)
result is true
<= Less than or equal to x <= y
int x = 5;
if( x == 3) //this condition is false because x is 5, 5 is not equal to 3
{
//this block will not be executed
}
if( x >= 4 ) //this condition is true 5 is greater than 4
{
//this block will be executed
}
if( x == 5 && x < 3 ) //this condition is false because x is equal to 5 but not less than 3, Note the and function
{
//this block will not be executed both conditions need to be true
}
int x = 2;
x++;
if( x == 3) //this condition is TRUE because x is 5, 5 is not equal to 3
{
//this block will be executed
}
else
{
//this block NOT will be executed
}
if( x == 5 || x > 3 ) //this condition is false because x is not equal to 5 OR x is not greater than 3,
{
//this block will only execute if either condition is true NOTE OR
}
else
{
//this block will be executed THE PREVIOUS CONDITION WAS NOT EXECUTED.
}
Switch case
A switch-case statement is a control flow statement that allows you to evaluate the
value of an expression (or variable) and then execute different blocks of code
based on the possible values of that expression. It's a way to simplify the process
of handling multiple conditions or options.
Switch ( variable )
{
case option 1:
// write your code here that will execute when option 1 is true
break;
case option 2:
// write your code here that will execute when option 2 is true
break;
case option 3:
// write your code here that will execute when option 3 is true
break;
case option 4:
// write your code here that will execute when option 4 is true
break
default:
// write your code here that will execute when all option are not true
break;
}
int y = 4;
Switch ( y )
{
case 1:
// write your code here that will execute when option 1 is true
break;
case 2:
// write your code here that will execute when option 2 is true
break;
case 3:
// write your code here that will execute when option 3 is true
break;
case 4:
// write your code here that will execute when option 4 is true
break;
default:
// write your code here that will execute when all option are not true
break;
}
char letter = ‘R’;
Switch ( letter )
{
case ‘F’:
// write your code here that will execute when letter is F
break;
case ‘B’:
// write your code here that will execute when letter is B
break;
case ‘L’:
// write your code here that will execute when letter is L
break;
case ‘R’:
// write your code here that will execute when letter is R
break;
default:
// write your code here that will execute when all option are not true
break;
}
END

You might also like