Lesson 11

The то loop, шикастан and давом

The то loop, шикастан and давом

In this lesson: you will write a loop when you do not know in advance how many steps it will make.

The whole lesson on one page

The то loop, шикастан and давом

In this lesson: you will write a loop when you do not know in advance how many steps it will make.

то

то (condition) { … } ("while") repeats the commands while the condition is дуруст:

1тағ шумора = 5;
2то (шумора > 0) {
3 чоп(шумора);
4 шумора = шумора - 1;
5}
6чоп("Оғоз!");

Something must change inside the loop, so that one day the condition becomes нодуруст. Otherwise the loop never ends.

The digits of a number

1тағ адад = хонданиРақам();
2тағ шумора = 0;
3то (адад > 0) {
4 адад = Риёзӣ.поён(адад / 10);
5 шумора++;
6}
7чоп("Рақамҳо:", шумора);
Input
2026

шикастан: leaving the loop

1// Reads numbers until 0 and prints their sum
2тағ ҷамъ = 0;
3то (дуруст) {
4 тағ рақам = хонданиРақам();
5 агар (рақам === 0) {
6 шикастан;
7 }
8 ҷамъ += рақам;
9}
10чоп("Ҷамъ:", ҷамъ);
Input
5
3
8
0
  • то (дуруст): this loop never ends by itself. You leave it with шикастан ("break").
  • давом ("continue") ends the current step and goes on to the next one.

Task: Number of digits

Read a positive whole number and print how many digits it has.

Input: One positive number.

Output: The number of digits.

Task: Backwards

Read a positive whole number and print its digits in reverse order: from 123 you get 321, from 1200 you get 21.

Input: One positive number.

Output: The reversed number.

Task: Sum until zero

Read numbers one at a time until a 0 comes. Print the sum of all the numbers.

Input: Numbers, each on a separate line; the last one is 0.

Output: The sum.