4.2.1. The for Loop, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

4.2.1. The for Loop, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

12th Class (HSSC-II) Computer SLO Based Key Point Notes

(National Book Foundation - As Federal Textbook Board, Islamabad 
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards) 

{Contact WhatsApp 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}

************************************

👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************

Unit 4: Control Structures


4.2.1. The for Loop

The for loop is used to execute a statement or set of statements repeatedly for a specified number of times. It is also known as Counter Loop.

Syntax:

for( Initialization; Condition; Increment/Decrement )
{
            statement 1; 
            statement 2;
            
            
            statement N;
}






Working of “for loop”

The number of iterations or repetitions depends on the initialization, condition, and increment/decrement parts of the “for loop”.

The for loop is executed as follows:
  1. First, the initialization part is executed. It is executed only once when the control enters the loop.
  2. Then the condition is evaluated, if it is TRUE, then the body of the loop is executed.
  3. After executing the body of the loop, the increment/decrement part is executed that changes the value of a counter variable.
  4. Then control again transfers the condition part. This process continues while the condition is TRUE. The loop is terminated when the given condition becomes FALSE. 

Flowchart of “for loop”



Example # 1:

Write a program that displays “Pakistan” for five times.

#include <iostream.h>
#include <conio.h>
void main()
{
        clrscr();
        for ( int n=1 ; n<=5 ; n++ )
        {
                cout<<“ \n PAKISTAN”;
        }
        getch();
}


Example # 2:

Write a program that reads an integer and prints its multiplicative table up to 20.

#include <iostream.h>
#include <conio.h>
void main()
{
        clrscr();
        int n;
        cout<<“Enter a number: “;
        cin>>n;
        for (int i=1; i<=10; i++)
        {
                cout<<n<<“\tx”<<i<<“\t=“<<n*i<<“\n”;
        }
        getch();
}



Example # 3:

Write a program that inputs a number and finds its factorial using a for loop.

#include <iostream.h>
#include <conio.h>
void main()
{
        clrscr();
        int n,f=1;
        cout<<“Enter an integer number: “;
        cin>>n;
        for (int c =1; c<=n; c++)
        {
                f = f * c;
        }
        cout<<“\n\tFactorial of a number is: “<<f;
        getch();
}


************************************
👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************


************************************
Shortcut Links For:

1.  5th Class All Subjects Notes

2.  8th Class All Subjects Notes

3.  Easy English Grammar Notes



1. Website for School and College Level Physics   
2. Website for School and College Level Mathematics  
3. Website for Single National Curriculum Pakistan - All Subjects Notes 

© 2023 & onwards Academic Skills and Knowledge (ASK  

Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.

Post a Comment

0 Comments

cwebp -q 80 image.png -o image.webp