Skip to main content

Featured Post

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Looping Statements in Computer Programming, C Language

Introduction

We have learnt about branching statements that transfer the control from one point to another point in the computer programming. In this article, we concentrate on another important constructs like loop statements, which executes a set of statements repeatedly. Sometimes, it is required to execute a set of statements repeatedly in c programming. In such cases, the looping statements are used.

The statements that enables the programmer to execute a set of statements repeatedly till the required activity is completed, called looping statements or simply loop statements. These statements are also called repetitive or iterative statements. The statements within a loop may be executed for a fixed number of times or until a certain condition is reached. The various types of loop statements that are supported by the C language are as follows:

For Loop

A set of statements may have to be repeatedly executed for a specified number of times. In such situations, we use for loop statement. This type of loop is defined as an iterative statement that causes a set of statements to be executed repeatedly for a fixed number of times. If we know well in advance as how many times a set of statements have to be executed repeatedly, then for loop is the best choice.

The syntax of for loop as follows:
for (initialization; condition; increment/decrement)
{
    statement1;
    statement2;
    statement3;
}

Where

  • for: is the reserve word or keyword
  • Initialization: used to provide starting value to the variable. It should end with semicolon (;).
  • Condition: used to determine whether value of variable has reached the number of repetitions desired. It should end with semicolon (;).
  • Increment/Decrements: It is used to update the variable value by increment or decrements.

Note: Initialization, condition and updating statement can be multiple statements separated by comma (,).

In for loop some points have to keep in mind by c programmer, which are:

  • The initialization statement executes first and only once when the execution begins.
  • After this, condition is executed. If the condition is evaluated as the True then the body of for loop is executed. After executing the body of the loop the increment/decrements statement is executed. It is normally update the variable value by the specified step size. Then termination condition statement is executed and the process is repeated.
  • If the condition is evaluated to False, then the control comes out of the loop without executing the body of the loop. Later, the rest of the code is executed.

As long as condition is evaluated to True, the body of for loop and the updating statement are executed.
For example

for(i=1; i<=5;i++)
{
  printf(“%d”,i);
}
printf(“\nOut of the loop”);

In the above example,

  • For loop is executed for the first time and initialize the value of i =1.
  • Then, condition is checked and it is true since i = 1.
  • So, control enters into body of the loop and displays 1 on the screen using printf statement.
  • Then i is incremented by 1 and condition is checked again. The loop is repeatedly executed for i=1, 2, 3, 4 and 5, all these values are displayed one after other.
  • Finally, when i will be 6, the condition fails and control comes out of the loop and the message "Out of the loop" is displayed on the screen.

Here's an example to find sum of N natural numbers using For loop.

Algorithm:
Step1:    [Read the number of terms]
Read: N
Step2:    [Initialization]
    Sum = 0
Step3:    for i = 1 to N in steps of 1 do
    Sum = Sum + i
    [End of for]
Step4:    Write: Sum
Step5:    Exit

Looping Statements in Computer Programming, C Language


C program to find sum of natural number

main()
{
  int i,n,sum=0;
  printf("Enter the number of terms here:\n");
  scanf("%d",&n);
  for(i=1; i<=n; i++)
   {
     sum=sum+i;
   }
printf("The sun of n %d term = %d",n,sum);
getch();
}

Looping Statements in Computer Programming, C Language

Branching Statements in C

Comments

Popular Post

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows: Polynomial:      3x 3 -4x 2 +2x-9 Linked List: In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable. Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Memory representation of Linked List Data Structures in C Language

                                 Memory representation of Linked List              In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.                Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:       Suppose next node is allocated at an address 506, so the list becomes,   Suppose next node is allocated with an address with an address 10,s the list become, The other way to represent the linked list is as shown below:  In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of