Hi there i am wondering if there is easy way of writing the following problem in C:-
For everytime constant A increases, constant B should decrease by 10 until A is equals to 120 !!

I dont really want to clutter my project with tons of IF statements

4
Contributors
6
Replies
9
Views
5 Years
Discussion Span
Last Post by deceptikon

You cannot increase or decrease a constant. I would call it a variable.

yh sorry my bad !! a variable
i tried the following but the code apparntaly stucks in this loop for infinite time

``````while (A++){
B += 10;
}
``````

Think what would be the exit condition of the loop according to task. How could you express it as continueing condition of while?

You didn't check to see when A equals 120. The implicit check is against 0, so your loop actually looks like this:

``````while (A++ != 0) {
B += 10;
}
``````

There are a number of things that can go wrong here, which you can limit a bit by changing the test to this:

``````while (A++ < 120) {
B += 10;
}
``````

However, it would also be wise to limit the lower end too, just to ensure that you're not starting with `A` at something like INT_MIN. If that happens then B will easily overflow:

``````if (A < 0) {
/* Assuming negatives are an error, for the sake of the example */
}
else {
while (A++ < 120) {
B += 10;
}
}
``````

Thanks for replying so quick guys but still having the same problem
jus to be clear A is an input So am checking against an input
so When A increase Then B should decrease

Post all of your code. If it's too long, shrink the code down into something smaller, but still complete and compilable.

Edited by deceptikon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.