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

Programming Fundamentals-Lec-05

The document discusses for loops in programming fundamentals. It explains that a for loop is similar to a while loop in that it continues to execute a block of code until a condition is false. The for loop syntax includes an initialization, condition, and update. Several for loop examples are provided, including printing numbers from 1 to 20, printing text multiple times, and summing integers.

Uploaded by

umar03029452584
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)
26 views

Programming Fundamentals-Lec-05

The document discusses for loops in programming fundamentals. It explains that a for loop is similar to a while loop in that it continues to execute a block of code until a condition is false. The for loop syntax includes an initialization, condition, and update. Several for loop examples are provided, including printing numbers from 1 to 20, printing text multiple times, and summing integers.

Uploaded by

umar03029452584
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/ 7

* Programming

Fundamentals

Lecture 05

Instructor: Rao Umer Farooq


*Programming Fundamentals
For Loop:

*For loop is very similar to a while loop in that it


continues to process a block of code until a
conditional statement becomes false

*The condition of the loop is tested before the body of


the loop is executed, hence it is called an entry-
controlled loop.
*Programming Fundamentals
For Loop:
*Syntax:

for (initialization; condition; update)


{ // body of-loop }

*initialization - initializes variables and is executed only once


*condition - if true, the body of for loop is executed
if false, the for loop is terminated
*update - updates the value of initialized variables and again checks
the condition
*For loop Example
Write a program using for loop that display numbers from 1 to 20.

Method 1 Method 2 Method 3 Method 4


main() main() main() main()
{ { { {
int i ; int i=1; int i=1; for ( int i= 1 ;
i<=20 ; i++)
for ( i=1 ; for ( i ; i<=20 ; i+ for ( ; i<=20 ; i+
i<=20 ; i++) +) +) {
{ { { cout << i <<
endl;
cout << i cout << i << cout << i <<
<< endl; endl; endl; }
} } } cout<< “program
end”
cout<< cout<< “program cout<< “program
“program end” end” end” }
} } }
*Programming Fundamentals
For Loop:
*Printing Numbers From 1 to 5
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{ cout << i << " ";
}
return 0;
}
*Programming Fundamentals
For Loop:
*Printing a text 5 times:
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << "Hello World! " << endl;
}
return 0;
}
*Programming Fundamentals
For Loop:
*// Summing integers with the for statement.
*int main()
*{
*int total = 0; // initialize total
*// total even integers from 2 through 20
*for (int number = 2; number <= 20; number += 2)
*total += number;
* cout << "Sum is " << total << endl; // display results
*return 0;
*}

You might also like