The i++ (and ++i) is done as part of the while expression evaluation, which happens before the printing. So that means it will always print 1 initially.
The only difference between the i++ and ++i variants is when the increment happens inside the expression itself, and this affects the final value printed. The equivalent pseudo-code for each is:
while(i++ < 10) while i < 10:
i = i + 1
printf("%d\n", i); print i
i = i + 1
and:
i = i + 1
while(++i < 10) while i < 10:
printf("%d\n", i); print i
i = i + 1
Let's say i gets up to 9. With i++ < 10, it uses 9 < 10 for the while expression then increments i to 10 before printing. So the check uses 9 then prints 10.
With ++i < 10, it first increments i then uses 10 < 10 for the while expression. So the check uses 10 and doesn't print anything, because the loop has exited because of that check.
The i++ (and ++i) is done as part of the while expression evaluation, which happens before the printing. So that means it will always print 1 initially.
The only difference between the i++ and ++i variants is when the increment happens inside the expression itself, and this affects the final value printed. The equivalent pseudo-code for each is:
while(i++ < 10) while i < 10:
i = i + 1
printf("%d\n", i); print i
i = i + 1
and:
i = i + 1
while(++i < 10) while i < 10:
printf("%d\n", i); print i
i = i + 1
Let's say i gets up to 9. With i++ < 10, it uses 9 < 10 for the while expression then increments i to 10 before printing. So the check uses 9 then prints 10.
With ++i < 10, it first increments i then uses 10 < 10 for the while expression. So the check uses 10 and doesn't print anything, because the loop has exited because of that check.
i++ is post-increment and ++i is pre-increment. Post-increment means that the previous value is returned after incrementing the object. Pre-increment means that the object is incremented and then returned. Either way, the object is incremented when its expression is evaluated and that's why you don't get 0 as the first output.
python - Increment and while loop - Stack Overflow
Do-While loop. Increment - C++ Forum
while loops with increments - C++ Forum
c - How to increment a counter for a while loop within the loop? - Stack Overflow
Videos
Hi. As the title state my question is about incrementation when looping. I understand it may sound stupid and for that I apologize.
When you use a nested for loop you write :
for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { Task; } }
My question is when exactly does incrementing happen? Does i get incremented then go into the inner for loop? Does j complete the task then get incremented?
Thank you to anyone who help. I also apologize if I'm not making sense.
spam < 5 can be read as spam is less than 5, so it'll only increment from 0 to 4. In the 4th (last) iteration, spam = 4 so it prints 'Hello, world' and then spam + 1 = 5. At that point it will attempt another iteration, but spam < 5 is no longer true and therefore will exit the loop.
For reference: < means less than, <= means less than or equal to.
Any particular reason you think you'd get 6?
Your while loop is "while spam is less than 5", not "while spam is less than or equal to 5". The last iteration occurs when spam is 4, and then it gets incremented one last time to 5.
If spam equals 5, it is not less than 5, so the while loop stops iterating.
In the first case
while (ctr < 10)
printf("%d",ctr);
ctr=ctr+1;
the while loop body is considered only the printf() statement. ctr=ctr+1; is not part of the loop body. So you have an unchanged variable in loop condition check, which makes it infinite loop.
You need to enclose both the statements in a block scope, using {} so that both the statements become part of the loop body. Something like
while (ctr < 10) {
printf("%d",ctr);
ctr=ctr+1;
}
will do.
In the second case
int ctr=0;
while (ctr++ < 10)
printf("%d",ctr);
ctr is already incremented as the side effect of the postfix increment operator, in the while condition checking expression. Thus, while printing the value, the already incremented value is being printed.
It is quite simple, indeed.
int ctr = 0;
while (ctr < 10)
printf("%d",ctr);
ctr=ctr+1;
In this first piece of code, in spite of the indentation, your while is only involving printf("%d",ctr);, because there is no block making ctr=ctr+1; belong to the while.
It could be written:
int ctr = 0;
while (ctr < 10)
printf("%d",ctr);
ctr=ctr+1; // This is not in the loop, even with the previous indentation.
There is no increment to ctr in this loop and then it will run forever printing zeros.
In this second piece of code
int ctr=0;
while (ctr++ < 10)
printf("%d",ctr);
you do increment ctr every pass and it will work fine.
If you want to make the first loop work, write it like this:
int ctr = 0;
while (ctr < 10) {
printf("%d",ctr);
ctr=ctr+1;
}
Now ctr=ctr+1; is really inside the while loop.
Adding an item into an array can be done in several ways in Javascript
your code essentially does a push, so you could replace
namesArray[namesArray.length] = inputName
with
namesArray.push(inputName)
For an empty array, length property will be 0. Let's put it like this
1st iteration
while ( (inputName = prompt("Enter a name", "")) != "") {
namesArray[namesArray.length] = inputName;
// namesArray[0] = inputName;
}
2nd iteration
while ( (inputName = prompt("Enter a name", "")) != "") {
namesArray[namesArray.length] = inputName;
// namesArray[1] = inputName; // since now length is 1
}
And so on.. So what you're doing is assigning in following fashion
names[0] = inputName
names[1] = inputName
...
So, the length increases from 0 to n as you're adding elements to the Array.
I've been doing the MOOC Introduction to OOP course part 1 for a while, and I've been having a blast. I'm used to python, and no other language has got my attention like this, for some reason.
There is a thing, I don't quite understand. The increment operator in it self, raises the value of your variable by 1. However, in several excercises now, we have been doing something similar to this:
import java.util.Scanner;
public class ManyPrints {
// NOTE: do not change the method definition, e.g. add parameters to method
public static void printText() {
System.out.println("In the beginning there were the swamp, the hoe and Java");
// Write your code here
}
public static void main(String[] args) {
// ask the user how many times the text should be printed
// use the while structure to call the printText method several times
Scanner reader = new Scanner(System.in);
int num = 0;
System.out.println("How many? ");
int times = Integer.parseInt(reader.nextLine());
while(num <= times) {
printText();
num++;
}
}
}On the first try, I didn't even have the increment operator in my code, and it would just constantly print my code. Then I thought of this, because we have been doing it a lot, and it worked. However, I don't understand why. Why does the increment operator, make the program able to stop after the number in the user input? Why doesn't it just keep increasing the value by 1?
Sure while num is less or equal to times that part of the code runs, but if i remove the increment operator, it would be infinite.
Thank you.
You need an additional variable that stores the amount by which you increment. This variable itself has to be incremented by one in each run of the loop.
Try this :
int counter = 8;
int inc = 1;
while ( counter <= 18 )
{
System.out.print ( " " + counter );
if ( counter >= 14 && counter < 18 )
{
System.out.print ( " hello " );
}
counter = counter + inc;
inc += 1;
}