I am reading K&R book, on page 63 there is a line
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
where lim is int equal to 1000
My question, why is lim is not decreasing after consequential runs of while loop ?
As I see it, --lim is equivalent to "lim = lim - 1"
===================================================================
Thanks for all the answers !
--lim means "take one from the value of lim and use the result".
The alternative lim-- would be "use the value of lim and then take one away".
So if lim starts at 1000 the first time the loop executes it will have the value 999 before it is checked to see if it's greater than 0. If it were lim-- then the value that would be checked would be 1000, but it would still have the value of 999 at the end of the iteration through the loop. This is important at the start and end of the loop.
The MSDN as a page on this Prefix Increment and Decrement Operators
When the operator appears before its operand, the operand is incremented or decremented and its new value is the result of the expression.
Are you sure?
#include <stdio.h>
int main()
{
int lim = 10;
while (--lim > 0 && printf("%d\n",lim));
}
ouput:
9
8
7
6
5
4
3
2
1
Lim is decreasing, you probably made a mistake elsewhere. However, --lim is not quite equal to lim = lim - 1.
There are two operators to perform increment/decrement of a variable: pre-increment(or decrement), and post-increment(or decrement).
++x (pre-inc) and x++ (post-inc) both modify the value of x by +1. So what's the difference?
When you use x++ in an expression, the expression will consider x to have it's current value and after evaluating everything accordingly, increment that value by one. So...
int x = 5;
printf("%d", x++);
... will print out 5. HOWEVER, after the printf() line, the value of x will be 6.
Pre-increment works the other way round: the value of x it's first incremented, and then considered to evaluate the expression surrounding it. So...
int x = 5;
printf("%d", ++x);
... will print out 6 and, of course, the value of x will be 6 after that.
Of course, the same applies to the decrement operators.
Now, the assignment operation (x = x + 1) evaluates to the value assigned, after the assignment happened, so its behavior is actually similar to ++x, not x++.
Try to compile and run this code. It should be somewhat enlightening.
#include <stdio.h>
int main()
{
int lim = 10;
while (--lim > 0 && 1 > 32)
printf("I should never get here\n");
printf("%d\n",lim); // lim is now 9
}
Oh look, lim is now 9 even though I never actually entered the loop because 1 isn't greater than 32.
--lim or lim-- is a short hand of lim = lim - 1, So maybe author want to use this syntax to better clarify!
Excluding David's example (where the initial test of 'c' causes the loop body to never be executed, and thus lim is not decremented), and the possibility that lim is initially less than or equal to 1 (in which case the getchar() would not be executed), the code is equivalent to the following:
c=getchar();
while (lim > 1 && c != EOF && c != '\n')
{
lim = lim - 1;
/* Original body of loop */
c=getchar();
}
Both exceptions are rather unlikely and probably inconsequential, so you could use the above code if it's easier to understand.
Related
The semicolon has been added after the first while loop, but why is the value of the i variable 3 here, where j is 2?
#include<stdio.h>
int main()
{
int i=1;
while(i++<=1);
printf("%d",i);
int j=1;
while(j++<=1)
printf("%d",j);
return 0;
}
Both while loops run once (and once only); the difference is that, in the second case (the j loop) you are printing the 'control variable' inside the loop but, for the first case, you are printing it after the loop condition has evaluated to false. Also note that, in the first case, the semicolon immediately following the while statement defines the body of that loop as empty1.
Let's break down the first loop into steps:
On the first test of the condition, i++ evaluates to 1 and then i is incremented – so the loop runs.
On the second test, i++ evaluates to 2 (so the loop doesn't run) but i is still (post-)incremented, leaving it with the value of 3 (as shown in the output).
The same thing happens with j in the second loop but, in that case, as previously mentioned, you are displaying the value in the body of the loop (on its only run), so you see the value after the first (post-)increment.
As noted in the comments, if you add another printf("%d", j); after the body of the loop (which, in that case, consists of a single statement), you will see that j, too, has the value 3 when that loop has finished.
1 More precisely, the semicolon (on its own) defines a null statement, which forms the body of the while loop.
It is often helpful to clarify such 'null loops' by putting the semicolon on a line by itself (some compilers, with full warnings or static analysis enabled, may even suggest you do this):
#include<stdio.h>
int main()
{
int i = 1;
while (i++ <= 1)
; // Body of loop - Null Statement
printf("%d", i); // Loop has finished
int j = 1;
while (j++ <= 1)
printf("%d", j); // Body of loop
// Loop has finished
return 0;
}
For starters let;s consider how the postfix increment operator works. From the C Standard (6.5.2.4 Postfix increment and decrement operators)
2 The result of the postfix ++ operator is the value of the
operand. As a side effect, the value of the operand object is
incremented (that is, the value 1 of the appropriate type is added to
it).
Now consider this while loop
int i=1;
while(i++<=1);
In the first iteration of the loop the value of the expression i++ as the value of its operand that is 1. So the loop will iterate a second time, Due to applying the side effect to the variable i it will be equal already to 2 when the expression i++<=1 will evaluate the second time.
Now the value of i is equal to 2 and is greater than 1. so the loop will be interrupted. Again due to applying the side effect of the postfix increment operator to the variable i it will be equal to 3. This value is outputted in the following call of printf.
In simplest terms,
consider three things for each iteration of first while loop:
first iteration:
int i = 1;//initialization
while(i++<=1); //1. i evaluated as 1,
//2. loop condition is true causing loop to iterate again.
//3. ...but not before ++ causes i==2.
second iteration:
while(i++<=1); //1. i evaluated as 2,
//2. loop condition is false causing loop to eventually exit
//3. ...but not before ++ causes i==3.
printf is not included in the while loop (because of the semicolon) but
when program flow finally reaches printf("%d",i); the value of i is output as 3
In the second loop, because printf is included in the while construct, it will output the value of j for each iteration. For the same reasons as in loop one, it will also iterate only twice, and its values at time of output will be 2 & 3.
Using a debugger to set break points, and a watch on i, you can step through code such as this to see the sequence of these effects as they happen.
For explanation porpouses look at this example:
int main()
{
int i=1;
while(i++<=1)
printf("%d",i);
printf("%d",i);
}
The Output is:
2
3
Ok let's dive into the programm procedure:
Declaring i (i = 1)
while checking Condition and it's true because i++ returns 1(old Value). That's because the ++ are after the i. If you would code it like this ++i than it returns 2(new Value) (i = 2)
execute printf (i = 2)
while checking Condition and it's false because i++ returns 2 and 2 is not <= 1 (i = 3)
execute printf (i = 3)
Do you understand?
If it solved your thoughts jumble ;) mark it as answer.
You've got many answers already so I won't try to explain it in words but with an illustration in code. I've added two functions:
One that acts like a prefix increment operator (++i)
One that acts like a postfix increment operator (i++)
I'm only using the postfix version in the program though. I've added logging to the function so you can see how it works.
#include <stdio.h>
// this acts as if you put ++ before the variable
int preinc(int* p) {
++*p;
return *p;
}
// this acts as if you put ++ after the variable
int postinc(int* p) {
int rv = *p;
++*p;
printf("returning %d but incremented to %d\n", rv, *p);
return rv;
}
int main() {
int i=1;
while(postinc(&i) <= 1);
printf("end result: %d\n---\n", i);
int j=1;
while(postinc(&j) <= 1)
printf("in loop: %d\n", j);
printf("end result: %d\n", j);
}
Output:
returning 1 but incremented to 2
returning 2 but incremented to 3
end result: 3
---
returning 1 but incremented to 2
in loop: 2
returning 2 but incremented to 3
end result: 3
If you add the semicolon in the end of the while you will take: i = 3 and j = 3(IN THE OUTPUT).
If you add the semicolon only in the end of the printf and not to the while you will take i = 2 and j = 2(IN THE OUTPUT), but in the end you will, also, have the values
i = 3 and j = 3.
This happens because the "i" variable is incremented only after the first iteration of the loop, in which the empty statement (terminated by the semicolon) is evaluated.
that is why it is highly recommended to do "++i" rather than "i++", because then the incrementation is performed prior to the evaluation.
if you flip it here, you will see that i will be equal to 2 as well, regardless of the presence of the semicolon
So I saw this in my text book and I cannot understand how it works.
x=0;
int i = 0;
int n;
while(!x && i < n){
if(array[i]==target)
x=1;
else
++i;
}
what I don't understand is how "!x" works in loop's condition.
what I understand is this loop keeps running until it runs n times or when array[i]==target, which will change the value of x = 0 to x = 1 and stop the loop.
what I tried:
I tried replacing !x to x==0 and it did the same job.
The short answer here is that it does not matter what !x is because it is surrounded with undefined behavior completely nullifying its effect.
In the statement
While(condition) {...}
condition must resolve to either true or false, and must therefore be logical expression.
Given:
int x=0;//This answer assumes 'int' here as not specified in OP
int i = 0;
int n;
while(!x && i <n)
Because x is initialized as 0, equivalent to false the expression !x resolves to true, satisfying part of the condition.
Because n is not initialized however, the condition (!x && i <n) invokes undefined behavior, making the results of the overall condition unknown at the time of first entry, i.e. it can be either true or false. Further more, n does is never modified within {...}, so if the initial path happens to enter the brackets, the condition will change only due to changes in i. But again, because the value of n is not known, only undefined behavior will occur.
I don't know what language this is but !someVar is typically shorthand syntax for "variable is false", or another way of writing someVar == false. And in binary 0 is false and 1 is true. In the C language, 0 is false and anything not 0 is true. Therefore, in your loop, while !x is shorthand syntax for while x == false or while x == 0. Again, generally speaking since we don't know the language.
While(!x) means if x is a condition then we go into the while loop, only if the condition does not satisfy.Here in your code you have assigned x as zero,so !x means 1 because '!' Basically means opposite if x =0 then !x =1, if x=1 then !x=0 (all numbers greater than 1 are also considered as 1 only) so while(1 && i<n) is what happening here in the next step.If i<n is true then you will enter into the loop. If i<n is false the you won't enter the loop.
while(!x)
{
intructions;
}
means that your instructions will only keep running over and over again if x is false or equals 0 and will stop executing instructions once x becomes true or different from 0
#include <stdio.h>
int main()
{
int i=0;
while(i++,i<=8);
printf("%d\n",i);
return 0;
}
Why is the increment of i done after the comparison in each test case?
i <= 8 succeeds for the last time when i = 8.
In the last iteration, i++ is executed, then i <= 8 fails because i = 9.
Note that the , is a sequence point, so i++ is absolutely guaranteed to be executed before i <= 8. Not so for similar constructs.
It's not. Your loop condition is i <= 8, it is first non-true when i reaches 9 (you're incrementing i by 1 each time, so it will be exactly 9). That is why it prints 9.
To reach to the print() statement, while loop must end. The terminating condition, controlling expression should evaluate to false (or, in other words, until the controlling expression compares equal to 0), i.e., it will be false only when i <= 8; evaluates to false. For a value of i as 9, that happens.
Next line, the value of i, gets printed. So, you see 9.
Increment of i is not done after the comparison in each test case. i++ is executed first and after that the comparison is done.
This is because when expressions are separated using commas in C, the expressions are evaluated from left to right and the value of the last expression becomes the value of the total comma separated expression.
So the value of
i++,i<=8
is actually the value of i<=8 and that comparison is done only after i++ is executed.
So the while loop here
int i=0;
while(i++,i<=8);
is equivalent to
for(i=1; i<=8; i++);
Hence the control exits the loop only when i is greater than 8. Since i is incremented by 1 on each iteration, this means that the loop is over when i becomes 9.
You started i = 0 and using while ( i++, i<=8 ) loop you incremented it's value until 8 and when it's increased one more time i = 9 , then loop condition became false and breaks the loop with i = 9. And that's why now, when you print i's value, it gave you 9.
I am revising C again and was making some test programs. At one program I was checking a condition which was translating ino this condition.
#include <stdio.h>
int main()
{
if(0 <= 3000.000000 <= 2000.00){ //this is the condition
printf("3000 is less than 2000, whoa.. \n");
}
return 0;
}
The output is always this print string. I can't understand why.
P.S
I am testing the middle value, i.e 3000.000000 here, but it can be some variable.
The condition is parsed like this:
if((0 <= 3000.000000) <= 2000.00){
The first part, (0 <= 3000.000000), is true, and evaluates to 1 in the comparison with 2000.00. And 1 <= 2000.00 is true.
If you're trying to test whether a value a lies between two values b and c or is equal to either, then you need an expression along the lines of
(a >= b) && (a <= c)
You're getting caught by the fact that in C, booleans are integers: either 0 or 1.
So that line is interpreted left-to-right: First 0 <= 3000, which is true so it ends up as 1. Then that value is fed into the next half, (1) <= 2000, which is obviously true.
It will prints the string in printf.
Because the condition is static.
The 0 is always less than 30000.000000. For the next condition the output of the first condition returns 1. it checks using the 1.
The second condition checking is 1 <= 2000.00. This condition is also true.
So, only this prints the string.
the first condition evaluates to 1 as output and further 1<2000 is checked which is also true.So,the string is printed.
#include <stdio.h>
int main()
{
int k=5;
if(++k < 5 && k++ / 5 || ++k <= 8)
{
printf("%d",k);
}
return 0;
}
Why is the output 7 and not 8?(I am a beginner in programming so please bear with me.)
Operator precedence and logical expression short circuit evaluation.
The && in your logical condition binds more tightly than ||, so your conditional is equivalent to:
((++k<5 && k++/5) || ++k<=8)
It's easier to read code when it is presented in a structured way, like this:
int main() {
int k=5;
if ((++k<5 && k++/5) || ++k<=8) {
printf("%d",k);
}
return 0;
}
And now a blow-by-blow of the execution.
k starts at 5.
++k<5 advances k to 6, which is not <5.
The second half of the && expression is never evaluated, because 0 && ANYTHING == 0.
Because the left hand side of the || is 0, the right hand side is not short circuited. It must be evaluated.
++k<=8 advances k to 7, which is <=8.
Total conditional evaluates to 1 because right hand side of || is 1.
The "then" clause of the if statement is executed.
Current value of k, which is 7, is printed.
The program returns 0, and terminates.
It's also worth noting that the second half of your && clause is perhaps not doing what you intended. k++/5 is integer division, and since k>5 at all times, k++/5 will always be >=1 and thus always true.