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

7 For Loop

The document discusses for loops and increment/decrement operators in C++. It explains that assignment operators like += can be used to increment/decrement variables and provides examples. It also explains that increment/decrement operators (++/--) can be used before or after a variable, and that prefix operators change the variable before expression evaluation, while postfix change it after. Finally, it provides the general syntax of a for loop and examples of using for loops to iterate, calculate factorials, and sum even numbers.

Uploaded by

Erjus Hoxhaj
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)
37 views

7 For Loop

The document discusses for loops and increment/decrement operators in C++. It explains that assignment operators like += can be used to increment/decrement variables and provides examples. It also explains that increment/decrement operators (++/--) can be used before or after a variable, and that prefix operators change the variable before expression evaluation, while postfix change it after. Finally, it provides the general syntax of a for loop and examples of using for loops to iterate, calculate factorials, and sum even numbers.

Uploaded by

Erjus Hoxhaj
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/ 15

For Loop

Assignment Operators
• Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the addition
assignment operator

• Statements of the form


variable = variable operator expression;
can be rewritten as
variable operator= expression;

• Examples of other assignment operators include:


d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
Increment and Decrement
Operators
• Increment operator (c++) - can be used instead of
c += 1
• Decrement operator (c--) - can be used instead of
c -= 1
• Preincrement
• When the operator is used before the variable (++c or ––c)
• Variable is changed, then the expression is evaluated.

• Posincrement
• When the operator is used after the variable (c++ or c--)
• The variable is executed, then the variable is changed.
Increment and Decrement Operators

• The statements of increment and decrement are commonly used in the


for loop.
• The increment (i.e., ++) or decrement (i.e., --) operators are the
frequently used operators which take only one operand.
• The increment/decrement operators increase or decrease the value of
the single operand.
 e.g., for (int i = 0; i < 100; i++){ … }
 The variable i increase one after each iteration.

5-4
Comparison of Prefix and Postfix
Increments
The value of the expression (that uses the ++/-- operators) depends on the
position of the operator.

The value of
The value of
j is not
j is
increased
increased

5-5
• If c = 5, then
 cout << ++c; prints out 6 (c is changed before cout is executed)
 cout << c++; prints out 5 (cout is executed before the increment. c
now has the value of 6)
• When Variable is not in an expression
 Preincrementing and postincrementing have the same effect.
++c;
cout << c;
and
c++;
cout << c;
have the same effect.
Flowchart for for

Initialize variable

Condition true statement


Test the variable Increment variable

false
The for Repetition Structure

• The general format when using for loops is


for ( initialization; condition; increment or decrement
)
{
statement
}
• Example:
for( int counter = 1; counter <= 10; counter++)
{
cout << counter << endl;
}
 Prints the integers from one to ten
Printing the numbers from 1 to 10
increasing by 1

#include <iostream>
using namespace std;
int main()
{

for( int counter = 1; counter <= 10; counter++)


{
cout << counter << endl;
}
return 0;
}
Syntax of “for” statement

• For: keyword
• Initialization: this part is executed only once at the beginning
• Condition: Set the continuation-condition here to continue or end the loop.
The condition is tested each time before the statement is executed.

• Increment or Decrement: this part is executed each time after the


statements have been executed.

• Statements: The statements are executed each time in the loop.


The general format for a for loop
1 2 3
for (Initialization; Condition; Increment or Decrement)
{
statement;
}

Factorial of n is n(n-1)(n-2)...21

int n,f=1;

cin >> n;

for (i=2; i<=n; i++)


{
f *= i;
}
cout << “The factorial of ” << n << “ is ” << f << “.”;

12
#include <iostream>
using namespace std;
int main()
{
int n, i, f=1;
cout<<"Enter a number that you would like to find the factorial:";
cin >> n;

for (i=2; i<=n; i++)


{
f *= i;
}
cout << "The factorial of " << n << " is " << f << ".";
return 0;
}
Exercise

• Write a program to sum the even numbers from 2 to 100


Solution
#include <iostream>
using namespace std;
int main()
{
int number, sum=0;

for (number=0; number<=100; number+=2)


{
sum += number;
}
cout << "The sum of even numbers is " << sum <<"."<<endl;
return 0;
}

You might also like