if
The if statement allows conditions to be tested and code can be skipped or executed based on the result of the test.
Relational operators are:
< Less than
> Greater than
== Equal to
!= Not equal to
<= Less than or equal to
>= Greater than or equal to
#include <stdio.h>
int main(int argc, char *argv[])
{
int Count;
Count = 1;
if (Count == 1)
{
printf("The count is: %d\n", Count);
return 0;
}
}
ALERT!
C uses two equal signs to specify 'equal to'. The statement if (Count = 1) is valid and will compile with no indication of error or warning. But this is not an 'equal to' condition test. The result of if (Count = 1) will always be true! Always check your if statements to ensure that you are using TWO equal signs when you intend to test for an 'equal to' condition.
for
The for statement has three parts, initialize, continue condition, and increment. This for statement starts Count at zero and while Count is less than or equal to 7, Count is incremented by one.
#include <stdio.h>
int main(int argc, char *argv[])
{
int Count;
for (Count = 0; Count <= 7; Count++)
{
printf("The count is: %d\n", Count);
}
return 0;
}
Count to 100 by 5s starting with 5.
#include <stdio.h>
int main(int argc, char *argv[])
{
int Count;
for (Count = 5; Count <= 100; Count = Count + 5)
{
printf("The count is: %d\n", Count);
}
return 0;
}