0% found this document useful (0 votes)
6 views

Lecture4 m

This document is a lecture outline for a C++ programming course at Ninevah University, covering topics such as nested if-else statements, loops, and various programming examples. It includes code snippets for finding maximum numbers, converting Celsius to Fahrenheit, and calculating factorials. The document serves as a resource for students in the Medical Instrumentation program for the academic year 2024-2025.

Uploaded by

Stive Brack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture4 m

This document is a lecture outline for a C++ programming course at Ninevah University, covering topics such as nested if-else statements, loops, and various programming examples. It includes code snippets for finding maximum numbers, converting Celsius to Fahrenheit, and calculating factorials. The document serves as a resource for students in the Medical Instrumentation program for the academic year 2024-2025.

Uploaded by

Stive Brack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C++ Programming

Ninevah University
College of Electronics Engineering
Department of Electronic Engineering
MEDICAL INSTRUMENTATION

2nd Year
2024 – 2025

Lecturer
Prof Dr. Qais Thanon
Lecture #4

All the lectures of this course will upload at the


Google classroom

10/8/2024
Nested if-else Selection statements:
if (Condition1){

if(Condition2){
Statement1;
}
else {
Statement2;
}
}
else {
if(Condition3)
{
Statement3;
}
else
{
Statement4;
}
} 2
10/8/2024
Write a C++ program to find the maximum number between
three numbers
#include <iostream.h>
void main(){ T F
int num1, num2, num3; num1>num2
cout<< "Enter three numbers:”<<“\n ;"
cin >> num1>> num2>> num3;
if (num1 > num2){ T
if ( num1 > mun3 ) num1>num3
cout << “The largest no is:”<< num1;
}
else {
if (num2 > num3) T F
cout << “The largest no is:” << num2;
num2>num3
else
cout << “The largest no is:” << num3;
} num1
} num2 num3

3
10/8/2024
Write a C++ program to find the maximum number between
three numbers
#include <iostream.h>
void main(){ T F
int num1, num2, num3; num1>num2
cout<< "Enter three numbers:”<<“\n ;"
cin >> num1>> num2>> num3;
if (num1 > num2){ T
if ( num1 > mun3 ) F
num1>num3
cout << “The largest no is:”<< num1;
else
cout << “The largest no is:”<< num3;
} T F
else {
num2>num3
if (num2 > num3)
cout << “The largest no is:” << num2; num3
else num1
cout << “The largest no is:” << num3; num2 num3
}
}

4
10/8/2024
Write C++ program to enter a number represents a centigrade degree.
Find degree in Fahrenheit that generated from the first degree
according to the relation:
F= (9/5) * C +32.
Then display the below phrases according to their equivalent Fahrenheit
degree:
#include <iostream.h> 1. “Cold” when F ≤ 41.
void main () {
2. “Nice” when 41< F ≤ 77.
float C,F;
cin >> C;
3. “Hot” when F >77.
F = (9 / 5) * C + 32;
cout << "F="<<F<<'\n';
if (F <= 41)
{cout << "Cold"<<'\n';}
else if (F > 41 && F <= 77)
{cout << " Nice"<<'\n';}
else {cout << Hot"<<'\n';}
}

5
10/8/2024
Ex: Write a C++ program to find the value of Z where:

x+y if i=1
x-y if i=2
Z=
x*y if i=3
x/y if i=4

#include <iostream.h>
void main () {
int i;
float x, y, Z;
cin >> i >> x >> y;

if ( i >= 1 && i <=


4){
if (i == 1){Z = x
+ y;}
if (i == 2){Z = x
- y;}
if (i == 3){Z = x
* y;} True
if (i == 4){Z = x
/ y;}
}
else cout <<“wronh no.”; false
}

6
10/8/2024
Write a C++ program to input student name and marks of three subjects
,and calculate average, and print grade according to the following
conditions:

7
10/8/2024
#include <iostream.h>
#include <string>
void main() {
int d1, d2, d3, sum = 0;
float avg
string name;
cin >> name >> d1 >> d2 >> d3;
avg = (d1 + d2 + d3) / 3.0;
if ( average >= 90 ) cout << " Grade A ";
else if (average >= 80)
cout << " Grade B ";
else if (average >= 70)
cout << " Grade C ";
else if (average >= 60)
cout << " Grade D ";
else if (average >= 50)
cout << " Grade E ";

}}

8
10/8/2024
Iteration loops in C++
There may be a situation, when you need to execute a block of code
several number of times. For example if you want to print the
numbers from 1 to 10 then the program would be:

#include <iostream.h>
void main(){
int x=1; Initial value
cout << x++; // the out put would be 1
cout << x++; // the out put would be 2
cout << x++; // the out put would be 3
cout << x++; // the out put would be 4
cout << x++; // the out put would be 5 Update
cout << x++; // the out put would be 6
cout << x++; // the out put would be 7
cout << x++; // the out put would be 8
cout << x++; // the out put would be 9
cout << x++; // the out put would be 10
}
Condition to stop
9
10/8/2024
goto statement
The goto statement is a jump statement which is sometimes also
referred to as unconditional jump statement. The goto statement
can be used to jump from anywhere to anywhere within a function.

Syntax:
// C++ program to print numbers
goto label; from 1 to 10
Statement 1;
... #include <iostream.h>
Statement n;
label:
void main() {
Flag int n = 0;
label:
Label: cout << n++ << " ";
Statement 1;
….. if (n <= 10)
Statement n; goto label;
goto label; }

10
10/8/2024
Iteration loops in C++
C++ programming language provides the following type of loops to
handle looping requirements.

Sr. No. Loop Type & Description

for loop Execute a sequence of statements multiple


1 times and abbreviates the code that manages the loop
variable.
while loop Repeats a statement or group of statements
2 while a given condition is true. It tests the condition
before executing the loop body.

do...while loop Like a ‘while’ statement, except that it


3 tests the condition at the end of the loop body.

nested loops You can use one or more loop inside any
4 another ‘while’, ‘for’ or ‘do..while’ loop.

11
10/8/2024
A loop statement allows us to execute a statement or
group of statements multiple times and following is the
general from of a loop statement in most of the
programming languages -

The initial statement,


loop condition,
and update statement
are called: loop control
statements

12
10/8/2024
The for Loop
The general form of the for statement is:
for (initial statement; loop condition; update statement){
loop statements;
}

// C++ program to print numbers from


1 to 10
#include <iostream.h>
void main() {
int n;
for ( n = 1; n <= 10; n++)
cout << n << " ";
}

13
10/8/2024
The for Loop
The for loop executes as follows:
1. The initial statement executes.
2. The loop condition is evaluated. If the loop condition
evaluates to true
i. Execute the for loop statement.
ii. Execute the update statement (the third expression
in the parentheses).
3. Repeat Step 2 until the loop condition evaluates to false.

The initial statement usually initializes a variable (called the for


loop control, or for indexed, variable).

In C++, for is a reserved word

14
10/8/2024
The for Loop (comments)
The following are some comments on for loops:

 If the loop condition is initially false, the loop body


does not execute.

 The update expression, when executed, changes the value


of the loop control variable (initialized by the initial
expression), which eventually sets the value of the loop
condition to false. The for loop body executes
indefinitely if the loop condition is always true.

 C++ allows you to use fractional values for loop control


variables of the double type (or any real data type). Because
different computers can give these loop control variables
different results, you should avoid using such variables.
15
10/8/2024
The for Loop (comments)
 A semicolon at the end of the for statement (just before the
body of the loop) is a semantic error. In this case, the action of
the for loop is empty.
for (int x=0; x<100; x++);

 In the for statement, if the loop condition is omitted, it is


assumed to be true
for (int x=0; ; x++)

 In a for statement, you can omit all three statements—initial


statement, loop condition, and update statement. The
following is a legal for loop:
for ( ; ; )
cout << "Hello ";

16
10/8/2024
Example: Assume the following specification:
Input: read a number N > 0
Output: write the sequence 1 2 3 … N (one number per
line)
#include <iostream.h>
void main() { 1
int N; 2
3
cin >> N;
4
for ( int i = 1; i <= N; i++) 5
cout << i << “\n "; 6
}
Assume the following specification:
Input: read a number N < 0
Output: write the sequence -1 -2 -3 … -N (one number per line)
#include <iostream.h> -1
void main() { -2
int N; -3
cin >> N; -4
for ( int i = -1; i >= N; i--) -5
-6
cout << i << “\n ";}
17
10/8/2024
Example: Program to find the factorial of an integer
number
n! = 1 × 2 × 3 × 4 × ……. × n
1. #include <iostream.h>
2. #include <conio.h>
3. void main(){
4. clrscr();
5. int num, i, fac=1;
6. cout <<“Enter the number”;
7. cin >> num;
8. for (i=1; i<=num; i++)
9. fac*=i;
10. cout <<“\n”<<“The factorial is:”<< fac;
11.
12. getch();
13. }

18
10/8/2024
Example: Write a C++ program to count the number of digits
of any integer.
202211101.

1. #include <iostream.h>
2. #include <conio.h>
3. void main(){
4. clrscr();
5. long N;
6. int ndigits = 0;
7. cin >> N;
8. for ( ; N > 9; ) {
9. ndigits++;
10. N = N/10; // extracts one digit
11. }
12. cout << ndigits + 1;
13. getch();
14. }
19
10/8/2024

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy