If Statement ๐ฟ#
An if statement is the most basic conditional. It appears in the format of
if (condition) {
// Conditional code here
}
// Code after
where
condition
is the a true/fales value. In C, true is represented by a non-zero number, often times 1, while false is represented by 0.
When the program reaches an if statement, it will check if the condition is true.
Tasks ๐ฏ#
Change the value of condition so that the output of the program below is โTrueโ.
#include <stdio.h>
int main() {
int condition = 0;
if (condition) {
printf("True");
}
if (!condition) {
printf("False");
}
return 0;
}
Solution โ
#include <stdio.h> int main() { int condition = 1; // Setting this to any non-zero value would mean true. if (condition) { printf("True"); } if (!condition) { printf("False"); } return 0; }