if-else statement helps make decisions in your program. It checks a condition (a true or false statement) and runs code based on whether the condition is true or false.
Here’s how it works:
- If statement: It checks if a condition is true. If the condition is true, the code inside the
ifblock runs. - Else statement: If the condition is false, the code inside the
elseblock runs.
Example:
#include <stdio.h>
int main() {
int number = 10;
// Check if number is greater than 5
if (number > 5) {
printf("The number is greater than 5\n");
} else {
printf("The number is 5 or less\n");
}
return 0;
}
How it works:
- In this example, the condition is
number > 5. - If the
numberis greater than 5, the program will print "The number is greater than 5". - If the
numberis not greater than 5 (false), the program will print "The number is 5 or less".
Key points:
- The condition in the
ifstatement must be something that can be either true or false. - The
elsepart is optional, and you only use it when you want to do something different if the condition is false.

❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️
ReplyDeletePerfect
ReplyDeleteWell done , Bilal keep going
ReplyDelete