0% found this document useful (0 votes)
5 views18 pages

Sec 3 2016 2

The document provides an overview of loop structures in C++, including while loops, do while loops, for loops, and nested loops, along with examples. It also discusses jump statements such as break, continue, and goto, explaining their usage with examples. Additionally, several programming tasks are presented to illustrate the application of these concepts.

Uploaded by

Ahmed mahlawy
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)
5 views18 pages

Sec 3 2016 2

The document provides an overview of loop structures in C++, including while loops, do while loops, for loops, and nested loops, along with examples. It also discusses jump statements such as break, continue, and goto, explaining their usage with examples. Additionally, several programming tasks are presented to illustrate the application of these concepts.

Uploaded by

Ahmed mahlawy
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/ 18

C++ sec3 – Loops

Loops

Jump
While loop Do while For loop Nested loop
statements.

1: General Loop Structures


2: while Loops

Its format is:


While (expression)
{statement}

Simple example:
3: do while Loops

Its format is:


do
{Statement} while (condition);

Simple example:
4: for Loops

Its format is:


for (initialization; condition;
increase)
{statement;}

Simple example:
5: Nested Loops

Simple example:

Output
6: Jump statements.

The break statement

The continue statement

The goto statement

A: The break statement


Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to
end an infinite loop, or to force it to end before its natural end. For example, we are going to stop
the count down before its natural end (maybe because of an engine check failure?):

Ex:
B: The continue statement
The continue statement causes the program to skip the rest of the loop in the current iteration
as if the end of the statement block had been reached, causing it to jump to the start of the
following iteration. For example, we are going to skip the number 5 in our countdown:
Ex:

C: The go to statement
go to allows to make an absolute jump to another point in the program. You should use this
feature with caution since its execution causes an unconditional jump ignoring any type of
nesting limitations. The destination point is identified by a label, which is then used as an
argument for the go to statement. A label is made of a valid identifier followed by a colon (:).
EX:
Programmers:
Pro 1
suppose you have the task of producing a Celsius-to- Fahrenheit temperature
conversion table. Fahrenheit temperatures corresponding to Celsius temperatures
from 5 to 50 degrees are to be displayed in increments of 5 degrees

Code:
Por 2
Calculate the Product of two numbers WITHOUT using the * operator
Code:

Output
Pro 3
Counting a’s (or any character), we want to read a text represented as a sequence
of characters, we want to calculate the number of occurrences of the
letter, we can assume that the text always has at least one
character
Example: the text
ayman and ahmed are working in commerce.
Now notice that we have 5 a's
Code:

Output
Pro 4
Data is requested and accepted continuously until a number larger than 100 is
entered. Entry of a number higher than 100 alerts the program to exit the while
loop and display the sum of the numbers entered.
Sol 1:

Output
Sol2 using break:

Sol3 whit continue


Pro 5
Write c++ programmer to calculate the square and cube of the number fro 1 : 10 .
And show the result as table .

Code

Output
Pro 6
Write c++ program to computes the positive and negative sums of a set of
MAXNUMS user-entered numbers.
Code:

Output
Pro 7
Write c++ program to compute the average grade for each student in a class of 20
students. Each student has taken four exams during the semester. The final grade
is calculated as the average of these exam grades.
Code:

Output:
Pro 8
Develop an C++ program to print the prime numbers that included between 1
and 100.
Code:

Output
Assignment

Code:
a,
Output: a

You might also like