7 For Loop
7 For Loop
Assignment Operators
• Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the addition
assignment operator
• 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
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
false
The for Repetition Structure
#include <iostream>
using namespace std;
int main()
{
• 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.
Factorial of n is n(n-1)(n-2)...21
int n,f=1;
cin >> n;
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;