0% found this document useful (0 votes)
12 views5 pages

MUCLecture 2023 4426617

C

Uploaded by

narayanray1952
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)
12 views5 pages

MUCLecture 2023 4426617

C

Uploaded by

narayanray1952
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/ 5

MSc. Baraa H.

Kareem
Computer Skills & Programming I I MSc. Murtada A. Mahdi

Lecture 1: C++ LOOPS

In C++ programming language, there are three loop statements, these looping
statements and loop control statements help to execute a block of code iteratively in
a loop and control the loop execution respectively.

1. The while statement (loop).

2. The do…while statement (loop).

3. The for statement (loop).

The while STATEMENT.


At the start of the while loop execution, the condition is checked. If
the condition is true, statement(s) inside while the block is executed.
The condition is checked again. If it evaluates to true, the statement(s) inside the
while loop is executed. This cycle goes on. If at all, the condition evaluates to false,
execution comes out of the while loop, meaning the while loop is completed. And the
program continues with the execution of statements after the while loop if any.
The while statement or loop Syntax:

while (conditional)
{
Block_of_statements ;
}

Example1: In this example, we shall write a while loop that prints numbers from 1
to 5. The while loop contains a statement to print a number; the condition checks if
the number is within the limits.
#include <iostream>
using namespace std;

1
MSc. Baraa H. Kareem
Computer Skills & Programming I I MSc. Murtada A. Mahdi

int main() {
int n = 5;
int i = 1;
while (i<=n) {
cout << i << "\n";
i++;
}
}

Example 2: While Loop to Compute Factorial


In this example, we shall use a while loop to compute the factorial of a number.

#include <iostream>
using namespace std;

int main() {
int n = 5;
int factorial = 1;

int i = 1;
while (i<=n) {
factorial *= i;
i++;
}

cout << factorial;


}

C++ Do-While Loop


Do-While Loop can execute a block of statements in a loop based on a condition.
In this tutorial, we learn the syntax of Do-While loop in C++, its algorithm,
flowchart, then some examples illustrating the usage of it. Later we shall go through
Infinite Do-While Loop and Nested Do-While Loop.
Following is the syntax of while loop in C++.

do {
// statement(s)
} while (condition);

2
MSc. Baraa H. Kareem
Computer Skills & Programming I I MSc. Murtada A. Mahdi

Statement(s) inside the do block is executed and the


while condition is checked. If the condition is
true, statement(s) inside the do block is executed.
The condition is checked again. If it evaluates to true,
the statement(s) inside the while loop is executed. This
cycle goes on. If at all, the condition evaluates to false,
program control comes out of the loop, meaning do-while
loop execution is completed. And the program continues
with the execution of statements after the do-while loop if
any. Following is the flow chart of the do-while loop in
C++.

Example 1: Do-While Loop

In this example, we shall write a do-while loop that prints the string Hello five
times.
#include <iostream>
using namespace std;

int main() {
int i=0;
do {
cout << "Hello" << endl;
} while (++i < 5);
}

Example 2: Do While Loop to Compute Factorial


In this example, we shall use do-while loop to compute factorial of a number.

#include <iostream>
using namespace std;

int main() {
int n = 5;
int factorial = 1;

int i = 1;
do {
factorial *= i;

3
MSc. Baraa H. Kareem
Computer Skills & Programming I I MSc. Murtada A. Mahdi

} while (++i <= n);

cout << factorial << endl;


}

C++ for-Loop
For Loop can execute a block of statements in a loop based on a condition. It is
similar to a while loop in working, but the only difference is that for loop has
provision for initialization and update in its syntax.
The for-loop Syntax is:

for (initial value; condition; increment /decrement)


{
Block_of_statements ;
}

At the start of for loop execution, initialization statement is executed and then the
condition is checked. If the condition is true, statement(s) inside for block are
executed. And the update statement is executed. The condition is checked again. If it
evaluates to true, the statement(s) inside the for loop are executed. This cycle goes
on. If at all, the condition evaluates to false, for loop execution is deemed completed
and the program control comes out of the loop. And the program continues with the
execution of statements after for loop if any.

The for loop is controlled by three expressions: an initialization, a condition,


and a n update (increment/decrement).

4
MSc. Baraa H. Kareem
Computer Skills & Programming I I MSc. Murtada A. Mahdi

Example 1: For Loop


In this example, we shall write a for loop that prints numbers from 1 to 5. The
for loop contains a statement to print a number, and the condition checks if the
number is within the limits.

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 5; i++) {
cout << i << "\n";
}
}

Example 2: For Loop to Compute Factorial


In this example, we shall use for loop to compute the factorial of a number.

#include <iostream>
using namespace std;

int main() {
int n=5;
int factorial = 1;

for (int i = 1; i <= n; i++) {


factorial *= i;
}

cout << factorial << "\n";


}

You might also like