Home > Software design >  hi ! I am new to programming ,I started learning dart to work on flutter ,my question is in while lo
hi ! I am new to programming ,I started learning dart to work on flutter ,my question is in while lo

Time:05-03

why when I write ex: var count = 1 ; while (count < 10) { print(count); count = 1 ; } it displays the count from 1-10 , but if i switched the print statement with the last line it gives me a count from 2-11 even my condition is to be under 10 ! ,thank you for your time

I would appreciate any tips for learning dart and flutter as a total beginner ,I am learning from a written course at eduactive.

CodePudding user response:

I am not sure about the code here's a the code for your problem and it generates the appropriate output maybe you are doing something different or forgot an addition line somewhere in your code

and my advice to you is at first try learning a older programming language like java or python because generally you won't find a good resources for learning new programming languages as much as the older once.

CodePudding user response:

Are you sure that you get values from 1-10 and 2-11? I'd expect values from 1-9 and 2-10. The reason is that in each loop

  1. The condition is checked
  2. The body of the loop is executed

So when entering the loop count always has a value between 1 and 9 (inclusive). When you increase that value by one before printing it, your algorithm prints the next number, not the one it already checked against the condition. You could add a million to count and it would be printed, even though your algorithm would leave the loop right after.

  • Related