You are on page 1of 5

The implementation of control structures

The unit no. 9


THE IMPLEMENTATION OF REPETITIVE (CYCLIC) STRUCTURES

9.1. The implementation of cyclic structures with known number of steps

The instruction for

In most high-level programming languages, the instruction for implements the cyclic structure with known number of steps. In C++ language the instruction for can be used in a much more flexible. Syntax: for (initialization; condition; increase) instructions; Most of the times, the instruction for scrolls through an interval upward or downward, using a variable named counter, which moves between the ends of the interval. For each value of the counter, is running the sequence instructions and then the counter is increased or it is decreased with a constant value. The instruction ends when the counter reaches out of range. Example: for (int i=1; i<=100; i++) cout<<i<< ; // this sequence displays the numbers between 1 and 100

The initialization and increase fields are optional. They can remain empty, but in all cases the semicolon signs between them must be written. For example we could write: for (;n<10;) if we wanted to specify no initialization and no increase; or for (;n<10;n++) if we wanted to include an increase field but no initialization (maybe because the variable was already initialized before). The instruction for can be replaced with repetitive instruction with initial test, as follows: 1
Computer programming and programming languages Theoretical considerations and applications

The implementation of control structures


initialization; while (condition) { instructions; increase; } 9.2. Aplications: 1. Reads an integer n, and then read n real numbers. Find their sum. #include <iostream.h> #include <conio.h> int main() { int n,i; float x; long int s=0; cout<<"Give n"; cin>>n; for (i=1; i<=n; i++) { cout<<"give x"; cin>>x; s=s+x; } cout<<" the sum is "<<s; getch(); } 2. Read an natural number, n. Calculate the sum s=1+2+3++n. #include <iostream.h> #include <conio.h> main() { int n, i; long int s=0; cout<<"Give n"; cin>>n; for (i=1; i<=n; i++) s=s+i; cout<<" the sum of numbers from 1 to "<<n<<" is "<<s; getch(); } 3. Read an natural number, n. Calculate the sum s = 1 + #include <iostream.h> #include <conio.h> main( ) { int n, i; float s=0; cout<<" Give n "; cin>>n; for (i=1;i<=n;i++) s=s+ (float) 1/(i*i); cout<<" the sum is "<<s; 2
Computer programming and programming languages Theoretical considerations and applications

1 1 1 + 2 + ... + 2 . 2 2 3 n

The implementation of control structures


getch(); }

Selfevaluation test no. 9 1. Read a natural number, n. Find the product p=n!=1x2x3xxn 2. Read a natural number, n. Show its divisors.

Verification work of learning unit no. 9

xy= =( (x x+ +y xy y )) 2

1. Read a natural number, n. Show if it is prime or no.. #include <conio.h> #include<iostream.h> main( ) { unsigned int n,i ; prim=1; for (k=2; k<=n/2; k++) if (n%k==0) prim=0; if (prim) cout<<n<< is a prime number ; else cout<<n<< isnt a prime number ; ghetch( ); } 2. Read an natural number, n. Calculate the sum
s =1+ 1 1 1 + + ... + 2 3 3 4 n ( n + 1)

#include <iostream.h> #include <conio.h> main( ) { int n,i; float s=0; cout<<"give n"; cin>>n; for (i=1;i<=n;i++) s=s+ (float) 1/(i*(i+1)); cout<<"the sum is "<<s; getch( ); 3
Computer programming and programming languages Theoretical considerations and applications

The implementation of control structures


}

3. Find all two-digits numbers these satisfied the relation : #include <iostream.h> #include <conio.h> main( ) { int n,i, x, y; for (x=1; x<=9; x++) for (y=0; y<=9; y++) if (10*x+y==(x+y)*(x+y)) cout<<10*x+y<<endl; getch( ); }

Answers and comments on the selfevaluation test no. 9 1. #include <iostream.h> #include <conio.h> main() { int n,i, p=1; cout<<"give n"; cin>>n; for (i=1;i<=n;i++) p=p*i; cout<<" factorial of "<<n<< is <<p; getch(); } 2. #include <iostream.h> #include <conio.h> main() { int n,i, p=1; cout<<"give n"; cin>>n; cout<<" the divisors of "<<n<< are: ; for (i=1; i<=n; i++) if (n%i==0) cout<<i<< ; getch(); }

4
Computer programming and programming languages Theoretical considerations and applications

The implementation of control structures

Summary The instruction for implements the repetitive structure with known number of steps Syntax: for (initialization; condition; increase) instructions; initialization is the initialization expression condition is the expression of continuation increase it is the expression of resetting

Bibliography

5
Computer programming and programming languages Theoretical considerations and applications

You might also like