C++ Programming
Ninevah University
College of Electronics Engineering
Department of Electronic Engineering
MEDICAL INSTRUMENTATION
2nd Year
2024 – 2025
Lecturer
Prof Dr. Qais Thanon
Lecture #5
All the lectures of this course will upload at the
Google classroom
10/23/2024
Example: Write a program in C++ to find the sum of the series
#include <iostream.h>
#include <math.h>
void main() {
double sum = 0, a;
int n, i;
cout << " Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; ++i) {
a = 1 / pow(i, i);
sum += a; }
cout << " The sum of the above series is: " << sum;
}
2
10/23/2024
Example: Write a program in C++ to find the sum of first and last
digit of a number.
#include <iostream.h>
void main() {
int n, first, last;
cout << " Input any number: ";
cin >> n;
first = n;
last=n % 10;
for(first=n; first>=10; first=first/10);
cout<<" The first digit of "<<n<<" is: "<<first;
cout<<" The last digit of "<<n<<" is: "<<last;
cout<<" The sum is: "<<first+last;
}
3
10/23/2024
The while Loop
The general form of the while statement is:
initial statement;
while (testExpression){
//loop control statements;
update statement;
}
// C++ program to print numbers from 1 to 10
#include <iostream.h>
void main() {
int n=1;
while (n <= 10){
cout << n << " ";
n++;}
}
4
10/23/2024
Example: Write a program in C++ to count number of digits of an
integer using while loop.
5723
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
long n;
int count=0;
cout << " Enter an integer: ";
cin >> n; Initial statement
while (n !=0) { Condition tested
n = n / 10; Update statement
count++; }
cout << " The number of digits: " << count;
}
5
10/23/2024
The do…while Loop
The general form of a do...while statement is:
do{
statement;
} while (expression);
The statement executes first, and then the
expression is evaluated
If the expression evaluates to true, the
statement executes again
As long as the expression in a do...while
statement is true, the statement executes
6
10/23/2024
Example: Write a program to calculate the summation of
10, 10.5, 11, 11.5, 12, … ……. - .., 19, 19.5, 20
#include<iostream.h>
#include<iostream.h>
void main(){
void main(){
float n=10.0, sum=0;
float n, sum=0;
do {
for(n=10; n<=20; n+= 0.5)
sum += n;
sum += n;
n+= 0.5;
cout<<sum;
} while(n<=20);
}
cout<<sum;
Initial statement
} Condition tested
Update statement
7
10/23/2024
Notes on iteration loops
To avoid an infinite loop, the loop body must contain a
statement that makes the expression false
The statement can be simple or compound
If compound, it must be in braces
do…while loop has an exit condition and always iterates
at least once (unlike for and while)
8
10/23/2024
9
10/23/2024
The break statement break;
The break statement has the following two usages in C++:
When the break statement is encountered inside a loop, the loop is
immediately terminated
and program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement
#include <iostream.h>
#include <conio.h>
void main(){
clrscr();
int a;
for (a=10; a <= 20; a++) {
cout << “the value of a=“<< a;
if (a > 15} break;
}
getch();
}
10
10/23/2024
The break statement break;
The break statement has the following two usages in C++:
When the break statement is encountered inside a loop, the loop is
immediately terminated
and program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement
#include <iostream.h>
#include <conio.h>
void main(){
clrscr();
int a = 10;
do {
cout << “the value of a=“<< a;
a++;
if (a > 15} break;
}while (a <= 20);
getch();
} 11
10/23/2024
The continue statement
It is sometimes necessary to skip a certain test condition within a loop.
In such case, continue; statement is used in C++ programming.
In practice, continue; statement is almost always used inside a
conditional statement.
Syntax:
The syntax of a break statement in C++ is:
continue;
12
10/23/2024
Example: Write program to display integer from 1 to 10 except 6 and 9.
#include <iostream.h>
#include <iostream.h>
void main (){
void main (){
int a = 1;
for (int a = 1; a <= 10; a++){
do {
if( a ==6 || a==9 )continue;
if( a ==6 || a==9 )continue;
cout << "value of a: " << a ;
cout << "value of a: " << a;
}
a++;
}
}while( a <= 10 );
}
13
10/23/2024
Continue statement
The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.
#include <iostream.h>
void main(){
clrscr();
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
getch();
}
This example skips the value of 4:
14
10/23/2024
/ C++ program to print the sum of the odd numbers between 1 and
100
#include <iostream.h>
void main() {
int n, sum = 0;
for ( n = 1; n <= 100; n++)
if (n % 2 == 1) sum+=n;
cout << “\n " <<sum;
}
/ C++ program to print the average of the even numbers between 100 and -100
#include <iostream.h>
void main() {
int i, sum = 0, n=0;
for ( i = 100; i >= -100; i--)
if (i % 2 == 0) {sum+=i;
n++;}
cout << “\n " <<sum/n;
}
15
10/23/2024