using it for testing purposes only not user input, that is all it is going to do when it hits number = 2; it will spit out true whereas the rest of the time it will say false.
Code:
#include <stdio.h>
int main(void)
{
int number = 2;
int i;
for (i=1; i<5; i++)
{
if (number == i )
{
printf ("True\n" );
}
else
{
printf ("False\n");
}
}
return 0;
}
Code:
userx@~/bin <> gcc -Wall checknum.c
userx@~/bin <> ./a.out
False
True
False
False
you can also put that into a function to rid yourself of a loop.
Code:
#include <stdio.h>
// one way
int Is_match(int a, int b)
{
return (a == b ? 1 : 0);
}
// the other way
int is_it_match(int a, int b)
{
if ( a == b)
return 1;
else
return 0;
}
int main(void)
{
// one way
if ( Is_match(4,4) )
printf("match\n");
else
printf("not Match\n");
//the other way
if (is_it_match(4,5) )
printf("is a match\n");
else
printf("is not a match\n");
return 0;
}
results
Code:
userx@~/bin <> gcc -Wall checknum.c
userx@~/bin <> ./a.out
match
is not a match
so actually what you are saying here is wrong,
I want to check either the condition is true or false. I know I have to use for loop and if else condition but I don't know how to write it
because you did already write it, using a loop and if else statement to check for true or false.
Mod: Ok I think I see what you are saying, you want 4 test ran using 2 different conditions. 1 true 2 false
use variables so the value change, not hard coded values. just made it up code, not tested.
Code:
#include <stdio.h>
int main()
{
int a = 0, b = 0, c = 1;
for ( a = 0; a < 4 ; a++)
{
if ( b == c )
{
printf("match\n");
c++;
}
else
{
printf("not match\n");
b++;
}
}
return 0;
}
if you want/need to know the logic behind it post back and ask, so I can explain it to you so you will know.