Reputation: 1863
The formula simply isn't executing. I tried using printf
to debug and it printed out 0 for i
at the end of the code
#include <stdio.h>
int main()
{
int i, base, height;
printf("Lumber Cross-Sectional Moment of Section\n");
printf("Size Area Inertia Modulus\n");
for (i = 0; i > 35; i++)
{
if (i == 6 || i == 12 || i == 18|| i == 24 || i == 30)
{
base = base * 2;
height = 2;
}
if (i != 6 || i != 12 || i != 18 || i != 24 || i != 30)
{
height = height * 2;
}
printf("%d x %d %d %d %d \n", base, height, base * height, base * 2 + height);
}//for
return (0);
}//main
Upvotes: 0
Views: 9427
Reputation: 96211
You need to think what your conditions mean. The reason the loop doesn't execute is mentioned by others. Let's look at the other condition:
if (i != 6 || i != 12 || i != 18 || i != 24 || i != 30)
When do you think the above condition will be false? We need to look at two cases:
i
is equal to 6. So, i != 6
is false, but every other part of the condition (i != 12
, etc.) is true. This is because 6 is only equal to 6, and no other number. So the overall condition is true.i
is not equal to 6. Now, i != 6
is true, and therefore the whole condition is true.So, your if
condition above is always true.
Also, think about what would happen if you were looping till a large number, let's say 215. Will you want to have a condition like:
if (i == 6 || i == 12 || ... || i == 210)
Surely, there has to be a better to do what you want. I think what you want is to multiply base
by 2, and reset height
when i
is divisible by 6; otherwise multiply height
by 2. The code structure becomes:
/* not legal C */
if (i is divisible by 6) {
} else {
}
To test divisibility by 6, remember that for numbers n
and m
, n % m
gives you the remainder of n
divided by m
.
Upvotes: 3
Reputation: 315
The first loop will never execute.First time for checks for the (i>35), in your case i<35, so the loop will not execute.
Upvotes: 0
Reputation: 10393
I think if you reshape the code and the conditions, it will be lot better.
Can you change the for loop to for (i=1 ; i<36 ; i++)
? This way you can simplify the if condition inside the loop,as you can see you are checking if its a multiple of 6 in the code.
You can just think on those lines.
Some more pointers:
Upvotes: 0
Reputation: 2997
As said by the above two mates for loop will not execute until the condition is true. Before asking it here you should have debugged it yourself. :)
Upvotes: 0
Reputation: 3034
It should be
for (i = 0; i < 35; i++)
as said by Bill the Lizard.
for (i = 0; i > 35; i++)
is never true in its condition since 0 < 35 and hence the loop body is never executed.
Upvotes: 4
Reputation: 406015
Should be
for (i = 0; i < 35; i++)
The body of the loop will only execute when the condition is true.
Upvotes: 17