Plan: 1 For repetition operator
Download 61.79 Kb.
|
Mustaqil ishi
- Bu sahifa navigatsiya:
- include int main() { int n,s = 0; cout > n; for (int i = 1; i s=s+i;
- include int main() { int n; cout cin >> n; for (int i = 1, j = 1; i
#include int main() { int i; for (int i = 10; i >= 1; i--) cout << i << " "; cout << endl; system ("pause"); return 0; } The break function can be applied to any iteration operator. This function ensures that duplication is completed. That is, it passes control from one iteration operator to the next. The continue function can be applied to any iteration operator. This function ensures that the iteration parameter takes the next value. In other words, the body of repetition ends. This does not end the repetition. Example 3: Given a natural number n. Create a program that calculates the sum of numbers from one to n. #include int main() { int n,s = 0; cout << "n="; cin >> n; for (int i = 1; i <= n; i++) s=s+i; cout << s << endl; system ("pause"); return 0; } Other options for the iteration operator The for iteration operator may not contain expressions in parentheses, but ";" must be The simplest continuous iteration operator is: for (;;) cout << "constant repetition"; If several variables need to be changed synchronously at the same time during the iteration process, they can be separated by commas where necessary in #include int main() { int n; cout<<"n="; cin >> n; for (int i = 1, j = 1; i <= n; i++, j += i) cout << i << " " << j << endl; system ("pause"); return 0; } If condition operator fulfillment (one-sided selection) if - else operators. Conditional operators To implement networking in branching algorithms conditional operator is used. if The operator does not confirm any condition performs networking in the program based on the results of the test [3, p. 34]: if ( The will work. If the condition is different from 0 or true, is executed, otherwise no action is taken if the condition is 0 or false failed and managed if passes from operator to operator (if it if any). Such a design is also called one-sided selection. This situation is shown in Figure 2.1 below. Download 61.79 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling