0% found this document useful (0 votes)
15 views3 pages

Working of For LOOP in C++

for loop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Working of For LOOP in C++

for loop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Working of FOR LOOP in C++

Pattern:
for(int x=n;x<=n1;x++){ // n and n1 means any numbers

(initialization) (condition) (increment)

cout<<” //Write any thing to show on screen”;

}
Initialization Part: Declare variable and assign it any value
i.e 0,1,2 etc.

Condition Checking: Write the condition part in such a way that n1


gets the value according to your desired condition.

Increment or Decrement Part: This part can be written in many


ways i.e x=x+1, ++x, x++ etc for increment of 1 and for increment of 2
this part will be written as x=x+2.

Similarly, for decrement it will be written as x=x-1,x=x-2 etc.


Working:
Here’s a step by step procedure for working of for loop:

Step 1: First of all initialization part will be checked and assigned value
of declared variable will be stored.

Step 2: Then condition part will be checked and if condition is true


then the loop will start working and if the condition is not true then the
loop will be terminated and will not work further.

Step 3: After cout what’s written in inverted commas in scope of loop


it will move towards the increment part and after making increment or
decrement the assigned value of declared variable will be changed
accordingly with the incremented or decremented value and this value
will be stored for the variable.

[Note that increment will occur after working of loop for the
checked condition and not just after checking the condition]
Step 4: Now the same procedure will be repeated for the changed
value until the condition gets false and loop gets terminated.
Example:
Input Code: Output:
#include<iostream> *****

using namespace std; *****


int main(){ *****
for(int i=1;i<=5;i++){ *****
for(int j=1;j<=5;j++){ *****
cout<<"*";
}
cout<<endl;
}
return 0;
}

Remarks:
 Increment in first loop will occur when endl command will be
performed after termination of first loop.
 When second loop will be terminated then value of j stored in
memory will also be removed from memory.

You might also like