While Loop in C++ Programming Language

Image
  While Loop in C++ Programming Language We have discussed about the entry-controlled loop or pre-tested loop previously in our blog posts. These statements or loops are used to repeat a piece of code for a specific number of times as per our program requirement. We have studied about the for loop already. Today in this blog post we are going to study about the while loop in C++ programming language. And we will look at how this while loop actually works and how we can stop it by making some condition false so that the loop might not run infinitely. We use while loop in C++ program when the number of iterations is not known in advance and depends on the condition. When we know exactly that how many times a loop should run then we use the for loop instead of while loop. Basic Syntax Of While Loop in C++ Programming Language Basic Working Of While Loop In C++ Programming Language Now we are going to discuss about the basic working of while loop in C++ programming....

How to use Structures in C Programming Language

 How to use Structures in C Programming Language


In programming languages like C, C++, C#, etc., sometimes we encounter a situation where we need to store different variables with different datatypes, but these variables are related to one object or instance. In such situations, we use data structures.

Today, we are going to discuss a simple technique that will help us store related variables in one place, namely, structures. We will look at how structures are used in the C programming language to help us achieve our target.




What is Structure in C Programming Language

We can consider structure as a special type of data structure that we can use to store data related to an entity under one umbrella and we can call it structure. We can consider a structure as a user-defined data type and it allows us to store various data types in one place. In structures, each data member used inside the structure body is called a member of a structure.

How we can create a Structure

In C programming language we can simply create a structure by using the struct keyword and declaring the members of the structure (variables and attributes) inside the structure body. 


Syntax of Declaring a Structure 



As we can see in the above C program code we have declared a structure using the struct keyword followed by the structure name that is Student. In this Student structure, we have declared two variables or attributes Id of type int, and Name of type char. We can call these variables as members of a structure. Remember to put a semicolon (;) at the end of the closing curly bracket.


Simple Example Program for Using Structure in C Programming



In this void main function in the above coding example, we have simply created an object or instance of Student structure and named it as student1. Now we can use this student1 object to access the data members of the Student class with the help of the dot(.) operator like this student1.Id as shown in the above coding example.

In the same way, we can access Id and Name attributes and assign values to them as shown in the coding example. We can directly assign the value of 1 to Id, but to assign value to the Name variable we have used the strcpy() function which is present in the string.h header file in C programming like this strcpy(student1.Name, "Muhammad Fahad");

At last, we have simply printed the value of Id and Name to the console screen using printf() in C programming language.

At last, we have used the _getch() function to hold the console screen and not let it disappear after the successful execution of our program.


Complete coding Example




Congratulations we have successfully implemented the concept of structures in C programming language.


























Comments

Popular posts from this blog

How to run c project in visual studio community 2022

While Loop in C++ Programming Language

if and if else statement in C++ programming language