ВУЗ:
Составители:
Рубрика:
switch (count)
{
case 1: x=1; break;
case 2: x=2; break;
case 3: x=3; break;
default: x=4;
}
Ɍɟɩɟɪɶ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɡɧɚɱɟɧɢɹ count, ɛɭɞɟɬ ɜɵɩɨɥɧɹɬɶɫɹ ɬɨɥɶɤɨ
ɨɞɧɚ ɜɟɬɜɶ ɩɟɪɟɤɥɸɱɚɬɟɥɹ ɢ
x ɛɭɞɟɬ ɩɪɢɧɢɦɚɬɶ ɨɞɧɨ ɢɡ ɱɟɬɵɪɟɯ ɩɪɟɞɭ-
ɫɦɨɬɪɟɧɧɵɯ ɡɧɚɱɟɧɢɣ.
ȼɋɢ/ɋɢ++ ɢɦɟɟɬɫɹ ɬɪɢ ɜɚɪɢɚɧɬɚ ɨɩɟɪɚɬɨɪɚ ɰɢɤɥɚ: ɰɢɤɥ ɫ ɩɪɟɞɭɫɥɨɜɢ-
ɟɦ, ɰɢɤɥ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɢ ɰɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ.
ɐɢɤɥ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ:
while (ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ) ɨɩɟɪɚɬɨɪ
ɉɪɢ ɤɚɠɞɨɦ ɩɨɜɬɨɪɟɧɢɢ ɰɢɤɥɚ ɜɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ, ɢ, ɟɫɥɢ
ɡɧɚɱɟɧɢɟ ɷɬɨɝɨ ɜɵɪɚɠɟɧɢɹ ɧɟ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ
ɨɩɟɪɚɬɨɪ - ɬɟɥɨ
ɰɢɤɥɚ.
ɐɢɤɥ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ:
do ɨɩɟɪɚɬɨɪ while (ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ);
ȼɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ ɜɵɱɢɫɥɹɟɬɫɹ ɢ ɩɪɨɜɟɪɹɟɬɫɹ ɩɨɫɥɟ ɤɚɠɞɨɝɨ ɩɨɜɬɨɪɟɧɢɹ
ɨɩɟɪɚɬɨɪɚ – ɬɟɥɚ ɰɢɤɥɚ, ɰɢɤɥ ɩɨɜɬɨɪɹɟɬɫɹ, ɩɨɤɚ ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ ɜɵ-
ɩɨɥɧɹɟɬɫɹ. Ɍɟɥɨ ɰɢɤɥɚ ɜ ɰɢɤɥɟ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɜɵɩɨɥɧɹɟɬɫɹ ɯɨɬɹ ɛɵ ɨɞɢɧ
ɪɚɡ.
ɐɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ:
for (E1; E2; E3) ɨɩɟɪɚɬɨɪ;
ɝɞɟ E1, E2 ɢ E3 – ɜɵɪɚɠɟɧɢɹ ɫɤɚɥɹɪɧɨɝɨ ɬɢɩɚ. ɐɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ ɪɟɚɥɢ-
ɡɭɟɬɫɹ ɩɨ ɫɥɟɞɭɸɳɟɦɭ ɚɥɝɨɪɢɬɦɭ:
1. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ
E1. Ɉɛɵɱɧɨ ɷɬɨ ɜɵɪɚɠɟɧɢɟ ɜɵɩɨɥɧɹɟɬ ɩɨɞ-
ɝɨɬɨɜɤɭ ɤ ɧɚɱɚɥɭ ɰɢɤɥɚ.
2. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ
E2, ɢ, ɟɫɥɢ ɨɧɨ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ
ɩɟɪɟɯɨɞ ɤ ɫɥɟɞɭɸɳɟɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɝɪɚɦɦɵ (ɜɵɯɨɞ ɢɡ ɰɢɤɥɚ). ȿɫɥɢ
E2 ɧɟ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɲɚɝ 3.
3. ȼɵɩɨɥɧɹɟɬɫɹ
ɨɩɟɪɚɬɨɪ – ɬɟɥɨ ɰɢɤɥɚ.
4. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ
E3 (ɜɵɩɨɥɧɹɟɬɫɹ ɩɨɞɝɨɬɨɜɤɚ ɤ ɩɨɜɬɨɪɟɧɢɸ
ɰɢɤɥɚ), ɩɨɫɥɟ ɱɟɝɨ ɫɧɨɜɚ ɜɵɩɨɥɧɹɟɬɫɹ ɲɚɝ 2.
ɉɭɫɬɶ ɬɪɟɛɭɟɬɫɹ ɩɨɞɫɱɢɬɚɬɶ ɫɭɦɦɭ ɷɥɟɦɟɧɬɨɜ ɧɟɤɨɬɨɪɨɝɨ ɦɚɫɫɢɜɚ ɢɡ
n
ɷɥɟɦɟɧɬɨɜ.
ɋ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɰɢɤɥɚ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ ɷɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɬɚɤ:
int s=0;
int i=0;
while (i<n) s+=a[i++];
ɗɬɚ ɠɟ ɡɚɞɚɱɚ ɫ ɩɪɢɦɟɧɟɧɢɟɦ ɰɢɤɥɚ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɪɟɲɚɟɬɫɹ ɫɥɟ-
ɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
int s=0;
int i=0;
28
switch (count) { case 1: x=1; break; case 2: x=2; break; case 3: x=3; break; default: x=4; } Ɍɟɩɟɪɶ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɡɧɚɱɟɧɢɹ count, ɛɭɞɟɬ ɜɵɩɨɥɧɹɬɶɫɹ ɬɨɥɶɤɨ ɨɞɧɚ ɜɟɬɜɶ ɩɟɪɟɤɥɸɱɚɬɟɥɹ ɢ x ɛɭɞɟɬ ɩɪɢɧɢɦɚɬɶ ɨɞɧɨ ɢɡ ɱɟɬɵɪɟɯ ɩɪɟɞɭ- ɫɦɨɬɪɟɧɧɵɯ ɡɧɚɱɟɧɢɣ. ȼ ɋɢ/ɋɢ++ ɢɦɟɟɬɫɹ ɬɪɢ ɜɚɪɢɚɧɬɚ ɨɩɟɪɚɬɨɪɚ ɰɢɤɥɚ: ɰɢɤɥ ɫ ɩɪɟɞɭɫɥɨɜɢ- ɟɦ, ɰɢɤɥ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɢ ɰɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ. ɐɢɤɥ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: while (ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ) ɨɩɟɪɚɬɨɪ ɉɪɢ ɤɚɠɞɨɦ ɩɨɜɬɨɪɟɧɢɢ ɰɢɤɥɚ ɜɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ, ɢ, ɟɫɥɢ ɡɧɚɱɟɧɢɟ ɷɬɨɝɨ ɜɵɪɚɠɟɧɢɹ ɧɟ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ - ɬɟɥɨ ɰɢɤɥɚ. ɐɢɤɥ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: do ɨɩɟɪɚɬɨɪ while (ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ); ȼɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ ɜɵɱɢɫɥɹɟɬɫɹ ɢ ɩɪɨɜɟɪɹɟɬɫɹ ɩɨɫɥɟ ɤɚɠɞɨɝɨ ɩɨɜɬɨɪɟɧɢɹ ɨɩɟɪɚɬɨɪɚ – ɬɟɥɚ ɰɢɤɥɚ, ɰɢɤɥ ɩɨɜɬɨɪɹɟɬɫɹ, ɩɨɤɚ ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ ɜɵ- ɩɨɥɧɹɟɬɫɹ. Ɍɟɥɨ ɰɢɤɥɚ ɜ ɰɢɤɥɟ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɜɵɩɨɥɧɹɟɬɫɹ ɯɨɬɹ ɛɵ ɨɞɢɧ ɪɚɡ. ɐɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: for (E1; E2; E3) ɨɩɟɪɚɬɨɪ; ɝɞɟ E1, E2 ɢ E3 – ɜɵɪɚɠɟɧɢɹ ɫɤɚɥɹɪɧɨɝɨ ɬɢɩɚ. ɐɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ ɪɟɚɥɢ- ɡɭɟɬɫɹ ɩɨ ɫɥɟɞɭɸɳɟɦɭ ɚɥɝɨɪɢɬɦɭ: 1. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ E1. Ɉɛɵɱɧɨ ɷɬɨ ɜɵɪɚɠɟɧɢɟ ɜɵɩɨɥɧɹɟɬ ɩɨɞ- ɝɨɬɨɜɤɭ ɤ ɧɚɱɚɥɭ ɰɢɤɥɚ. 2. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ E2, ɢ, ɟɫɥɢ ɨɧɨ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɩɟɪɟɯɨɞ ɤ ɫɥɟɞɭɸɳɟɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɝɪɚɦɦɵ (ɜɵɯɨɞ ɢɡ ɰɢɤɥɚ). ȿɫɥɢ E2 ɧɟ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɲɚɝ 3. 3. ȼɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ – ɬɟɥɨ ɰɢɤɥɚ. 4. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ E3 (ɜɵɩɨɥɧɹɟɬɫɹ ɩɨɞɝɨɬɨɜɤɚ ɤ ɩɨɜɬɨɪɟɧɢɸ ɰɢɤɥɚ), ɩɨɫɥɟ ɱɟɝɨ ɫɧɨɜɚ ɜɵɩɨɥɧɹɟɬɫɹ ɲɚɝ 2. ɉɭɫɬɶ ɬɪɟɛɭɟɬɫɹ ɩɨɞɫɱɢɬɚɬɶ ɫɭɦɦɭ ɷɥɟɦɟɧɬɨɜ ɧɟɤɨɬɨɪɨɝɨ ɦɚɫɫɢɜɚ ɢɡ n ɷɥɟɦɟɧɬɨɜ. ɋ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɰɢɤɥɚ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ ɷɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɬɚɤ: int s=0; int i=0; while (i
Страницы
- « первая
- ‹ предыдущая
- …
- 26
- 27
- 28
- 29
- 30
- …
- следующая ›
- последняя »