Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

PROGRAMMING FUNDAMENTALS PRESENTATION

Group members:

• FA23-BCS-392 Hira Inayat


• FA23 –BCS-413 Usman Sethi
• FA23-BCS-486 Aliha Tahir
• FA23-BCS 368 Nadia Seher
Looping in C++
Looping is a fundamental concept in C++ that allows you to repeat a block of
code multiple times. This is essential for automating tasks and processing data
efficiently.
For Loop in C++
1. The for loop in C++ is used to execute a block of code repeatedly for a specific number of times.
2. The syntax of the for loop consists of three parts: initialization, condition, and increment/decrement.

3. The initialization part sets the starting value for the loop counter, the condition part checks if the loop should
continue, and the increment/decrement part updates the loop counter after each iteration.
Flowchart of the For Loop:
1 Initialization
The loop starts by initializing a counter variable, which is used to track the number
of iterations.

2 Condition
The loop checks the condition, which determines whether the loop should continue
or terminate.

3 Execution
If the condition is true, the code inside the loop is executed.
Syntax of the For Loop:
for( initialization; condition; increment/decrement)

Initialization Condition Increment/Decrement


The initialization expression The condition expression
sets the initial value of the loop determines whether the loop The increment/decrement
counter. should continue or terminate. expression updates the loop
counter after each iteration.
Understanding the For Loop:
Initialize Execute Code
The loop counter is set to its initial If the condition is true, the code inside
value. the loop is executed.

1 2 3 4

Check Condition Update Counter


The loop checks if the condition is true After the code is executed, the loop
or false. counter is updated.
Example 1: Printing Numbers from 1 to 10

1 Initialize: int i=1 2 Condition: i<=10


The loop counter is initialized to 1. The loop continues as long as the counter is less
than or equal to 10.

3 Print 4 Increment: i++


The current value of the counter is printed to the The loop counter is incremented by 1 after each
console. iteration.
Example 1:

Output:
Code:
1 2 3 4 5 6 7 8 9 10
for( int i=1; i<=10; i++) { cout<< i<< " "; }
Example 2: Printing Table Using a For Loop

1. The for loop can be used to print the multiplication table of any given number.
2. The loop initializes a counter variable, usually i, to the starting value (e.g., 1).

3. The condition checks if the counter is less than or equal to the desired number of iterations (e.g., 10 for a 10x
table).
4. Inside the loop, the code prints the product of the given number and the current value of the counter.

5. The increment expression updates the counter variable after each iteration, usually by adding 1 (i++).
Example 2:

Code:

for(int i=1;i<=10; i++){ Output:


cout<< number <<" x
"<<i<<"="<<(number*i)<<endl;
Table of 9 will be displayed on screen.
Example 3: Printing Squares Using a For
Loop
1. The for loop can be used to print the squares of numbers from 1 to 10.
2. The loop initializes a counter variable `i` to 1, checks if `i` is less than or equal to 10, and then increments `i` by
1 after each iteration.

3. Inside the loop, the code calculates the square of the current value of `i` and prints it to the console.
Example 3:

Code: Output:
for (int i = 1; i <= number; i++) { cout << number; Square of 5= 25

if (i != number) { cout << " + "; } } cout << " = " <<
(number * number) << endl;
Example 4: Printing Star Patterns Using For
Loops
1. The for loop can be used to print various star patterns on the console.
2. The loop initializes a counter variable, usually i, and checks if it meets the desired condition.

3. Inside the loop, the code prints the appropriate star symbol, space, or newline character to create the desired
pattern.
Example 4:

Output:
The star pattern will be displayed.
Code:
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++)
{ cout<< "*"; } cout <<endl; }

You might also like