Skip to main content

C++ For Loop (फॉर लूप )


C++ For Loop(फॉर  लूप )
  • The C++ for loop is used to iterate a part of the program several times.
  •  If the number of iteration is fixed, it is recommended to use for loop than while or do-while loops.
  • The C++ for loop is same as C/C#. 
  • We can initialize variable, check condition and increment/decrement value.




for(initialization; condition; incr/decr)

{    

//code to be executed    
} 





PetaByte Center

C++ For Loop Example


#include <iostream.h>  


int main() 

{  
         for(int i=1;i<=10;i++)
{      
        
    cout<<i <<"\n"; 
     
  }

} 
  
Output:1 2 3 4 5 6 7 8 9 10

C++ Nested For Loop

In C++, we can use for loop inside another for loop, it is known as nested for loop. 

The inner loop is executed fully when outer loop is executed one time. 

So if outer loop and inner loop are executed 4 times, inner loop will be executed 4 times for each outer loop i.e. total 16 times.

C++ Nested For Loop Example

#include <iostream.h>  
   
int main () 

               {  

        for(int i=1;i<=3;i++)

              {  
    
             for(int j=1;j<=3;j++)

            {      

            cout<<i<<" "<<j<<"\n";  
    
          }     
        }  
    }  


C++ Infinite For Loop


If we use double semicolon in for loop, it will be executed infinite times. Let's see a simple example of infinite for loop in C++.


#include <iostream.h>  

   
int main () 
{  
        for (; ;)  
  
          {    
                  cout<<"Infinitive For Loop";    
          }    


    } 



Comments

Popular posts from this blog

Input Devices

Input Devices(इनपुट डिवाइस ) Input device enables the user to send data, information or control signals to computer. Central processing unit of computer receives the input and processes it to produce output. Some of the popular input devices are: Keyboard Mouse Scanner Joystick Light Pen Track ball Digitizer Microphone Magnetic Ink Character Recognition (MICR) Optical Character Reader (OCR) Keyboard (कीबोर्ड ) It is a basic input device that is used to enter data by pressing keys. It has different sets of keys for letters, numbers, characters and functions. QWERTY keyboard is the commonly used keyboard to enter data. Mouse (माउस) It is a hand held input device. It is used to move cursor or pointer across the screen. It generally has left and right button and a scroll wheel between them. Laptop computers come with a touch pad that works as a mouse. It lets you control the movement of cursor or pointer by moving your finger over the touchpad. Scanner (...

Types of computer

Types of Computer (कंप्यूटर के प्रकार ) We can categorize computer by two ways: data handling capabilities and size. On the basis of data handling capabilities , the computer is of  three  types: Analogue Computer (एनालॉग  कंप्यूटर ) Digital Computer (डिजिटल कंप्यूटर ) Hybrid Computer (हाइब्रिड कंप्यूटर ) 1) Analogue Computer(एनालॉग  कंप्यूटर ) Analog computers are designed to process analogue data. Analogue data is continuous data that changes continuously and cannot have discrete values such as speed, temperature, pressure and current. The analogue computers measure the continuous changes in physical quantity and generally render output as a reading on a dial or scale. Analogue computers directly accept the data from the measuring device without first converting it into numbers and codes. Speedometer and mercury thermometer are examples of analogue computers. 2) Digital Computer(डिजिटल कंप्यूटर) A digital computer is designed to perfor...

OUTPUT DEVICES

Output Devices(आउटपुट डिवाइस ) The output device displays the result of the processing of raw data that is entered in the computer through an input device. There are a number of output devices that display output in different ways such as text, images, hard copies, and audio or video. Some of the popular output devices are: Monitor CRT Monitor LCD Monitor LED Monitor Plasma Monitor Printer Impact Printers Character Printers Dot Matrix printers Daisy Wheel printers Line printers Drum printers Chain printers Non-impact printers Laser printers Inkjet printers Projector 1) Monitor(मॉनिटर) The monitor is the display unit or screen of the computer. It is the main output device that displays the processed data or information as text, images, audio or video. The types of monitors are given below. i ) CRT Monitor(सी आर  टी  मॉनिटर ) CRT monitors are based on the cathode ray tubes. They are like vacuum tubes which produce images in the ...