[go: up one dir, main page]

0% found this document useful (0 votes)
29 views28 pages

Lecture 5 Loops

The document provides an overview of loops in computer programming, specifically focusing on for loops, while loops, and do-while loops. It includes examples of loop structures, their syntax, and practical applications such as printing messages, calculating factorials, and generating multiplication tables. Additionally, it discusses variations in loop initialization and increment expressions, as well as nested loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views28 pages

Lecture 5 Loops

The document provides an overview of loops in computer programming, specifically focusing on for loops, while loops, and do-while loops. It includes examples of loop structures, their syntax, and practical applications such as printing messages, calculating factorials, and generating multiplication tables. Additionally, it discusses variations in loop initialization and increment expressions, as well as nested loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Computer Programming

Loops ( Repetitive control Statements)

Week 04 Sp 25

Acknowledgement – Ms. Nabia Khalid worked on the slides


Outline
for Loop
while Loop
do-while Loop
Loops
• The following "non-computer" algorithms involve repetition:
• Compute the course grade for every student in a class.
• Microwave the food until the timer reaches 0, the cancel button is hit, or the
door is opened.
• While the ATM is running, process another customer.
Loops
• The loop logic structure is the repeating structure. It allows code to be
repeated until a condition in the data is met.
• Loops also allow the programmer to return to an earlier portion of
the code.
• A looping structure makes a decision every time the loop is repeated.
• As long as the decision is true, the statements in the loop repeat. A
loop stops when the loop test is false.
Naïve Example
• Print ‘Hello World’ 5 times
void main()
{
cout << "Hello World" << endl;
cout << "Hello World" << endl;
cout << "Hello World" << endl;
cout << "Hello World" << endl;
cout << "Hello World" << endl;
}

• What if you have to print this 50 or 500 times or even more….. ?


Better way
void main()
{

Repeat this part n-times


{
cout << "Hello World" <<
endl;
}

}
Loops
for (start; test; action) //the for loop
{
//Statements to be repeated
}

while (T / F) // The while loop


{
//Statements to be repeated
}

do//The do - while loop


{
//Statements to be repeated
} while (T / F); //repeats on True
for Loop
Example
int main()
{
for (int loop = 0; loop <= 5; loop++)
{
cout << "Hello World" << endl;
}
}

int main()
{
for (int loop = 1; loop <= 5; loop++)
{
cout << loop << endl;
}
}
The for loop
• Executed a fixed number of times
int main()
{
for (int loop = 0; loop < 10; loop++)
cout << loop << endl;
}
int main()
{
for (int loop = 9; loop >= 0; loop--)
cout << loop << endl;
}
Initialization expression Test expression Increment Expression

for (int loop = 0; loop < 10; loop++)


The for Loop

When a for loop is encountered, the initial-statement is


executed. The loop-test is executed. If loop-test is false,
the for loop is terminated. If loop-test is true, the
iterative-part is executed and the increment-statement is
executed. The process repeats by evaluating loop-test,
and so on ...
The for loop Initialization Exp

false
Test Exp

true

Body of Loop

Increment Exp
The for loop
for (initialization; Test; increment)
statement;

for (initialization; Test; increment)

{
statement 1;
statement 2;
statement 3;

}
The for loop
• Blocks
void main(void)
{
for (int numb = 0; numb < 10; numb++)
{
int square = numb * numb;
cout << "Number: " << numb << "Square : " << square <<
endl;
}
}
The for loop
• Variations
• Increment Expression – May perform any operation
for (int i = 10; i >= 0; i--)

for (int i = 0; i <= 10; i += 2)

for (int i = 1; i < 100; i *= 2)


Variables defined in for Statements

int i;
for (i = 10; i >= 0; i--)

for (int i = 10; i >= 0; i--)


C++ code + flowchart

for(i = 2; i <= 6; i = i + 2)
cout<<i+1<<'\t';

cout i+1
The for loop (C code)

for(i = 2; i != 11; i = i + 3)
printf("%d\t", i + 1);
Print the times table for 8 in one column. To solve this problem you will need to:

1. Write down the pseudocode.


2. Build a flow chart.
3. Create a C++ program.
Pseudo code + Flow chart

Declare a variable of type integer and


set the initial value to 0, int i = 0;
For repetition we need to use loop,
for loop.
Start the for loop.
Set the terminal condition, i <=12;
Increment i by 1, i = i + 1;
For every iteration, times 8, i * 8;
Print the result with ‘\n’ for every
iteration.
Stop the for loop. cout i*8
C++ Code
#include <istream>
using namespace std;
void main()
{
int i = 0, j = 8;
cout<<"Times 8 Table”<<endl;
for(i = 0; i <= 12; i = i + 1)
{
cout<<i<<'x'<<j<<'='<<j*i;
}
cout<<‘\n’;
}
The for Loop

Multiple Initialization and Increment Expressions
• Can have more than one initialization expression.
• Can also have more than one increment expression.
• Only one test expression.
• Different Expression separated by commas.

for (int j = 0, int k = 100; j < 50; j++,


k--)
{
// body of for loop.
}
The for Loop
Degrees to Radians

int main(void)
{
const float PI = 3.141593;
double radians;

cout << “Degrees to Radians “;


for (int degrees = 0; degrees <= 360; degrees += 10)
{
radians = degrees * PI / 180;
cout << radians << endl;
}
return 0;
}
The for loop
Sum of all even numbers from 2 to 100
1
2 // Summation with for.
3 #include <iostream.h>
4
5 // function main begins program execution
6 void main(void)
7 {
8 int sum = 0; // initialize sum
9
10 // sum even integers from 2 through 100
11 for ( int number = 2; number <= 100; number += 2 )
12 sum += number; // add number to sum
13
14 cout << "Sum is " << sum << endl; // output sum
15
16 } // end function main

Output: Sum is 2550


The for loop
//Example of a for loop that produces an average
of user-supplied values:
int n = 0;
int Value = 0;
int Sum;
float Average = 0;
cout << "Enter n: ";// get a value for the number of iterations
cin >> n;
Sum = 0; // Initialize sum to 0 to ensure the correct
average

for (int Counter = 0; Counter < n; Counter = Counter + 1)


{
cout << "Enter number: ";
cin >> Value;
Sum = Sum + Value;//accumulate
}

Average = (float)Sum / n; // Compute and display the average


Average = static_cast<float>(Sum) / n; // Compute and display the
average cout << "Average: " << Average;
Factorial
int value;
int factorial = 1;
cout << "Enter a positive value : ";
cin >> value;

for (int loop = value; loop>1; loop--)


{
factorial *= loop;
}
cout << "Factorial of value " <<value<<" is :
"<<factorial<<endl;
Nested for loops!

int value;
cout << "Enter a positive value : ";
cin >> value; // 5

for (int line = 1; line <= value; line++)


{//5
for (int star = 1; star <= value; star++)
{
cout << “ ”<< star;
}
cout << endl;
}
Tables Example

for (int tableOf = 1; tableOf <= 20; tableOf++)


{
for (int tableRange = 1; tableRange <= 10;
tableRange++)
{
cout << setw(5) << tableOf * tableRange;
}
cout << endl;
}
Square Example
#include<iomanip>

int squareValue;
cout << "Enter a positive value for creating a star square
: ";
cin >> squareValue;

for (int lines = 1; lines <= squareValue; lines++)


{
for (int stars = 1; stars <= squareValue; stars++)
{
cout << setw(2) << "*";
//cout << setw(2) << stars;
}
cout << endl;
}

You might also like