How To Write Hello World Program In C++ Programming Language

Image
  How To Write a Hello World Program In C++ Programming Language In this post we will look at how a C++ program works. We will try make the example as simple as possible for the beginners so they can easily understand the basics of C++. We will write a simple C++ hello world program in C++ programming language which is considered as the very first program in almost every programming language. A Brief Introduction to the C++ Programming Language C++ is an object oriented programming language and it is an extension to C programming language. C++ is a general purpose programming language and it is also a case-sensitive language. C++ is very powerful programming language it has features of both low level and high level programming languages, and that is why it is called mid level programming language. Hello World program in C++ programming language Above is an example of a simple Hello World program in C++ programming language. Now let us look at the code, and I will take you...

How does a do-while loop work in the c programming language?

 How does a do-while loop work in the c programming language?

We use control statements in programming languages to execute code based on certain conditions. In C programming the concept of control statement is the same. These control statements iterate pieces of code multiple times according to user requirements. These iterations are controlled by some conditions that are given by the programmer or the developer. There are mainly three types of control statements or looping statements, which are,

1) do-while loop

2) while loop

3) for loop



Today our main focus is on the do-while loop. We are going to look at the do-while loop and the basic syntax of the do-while loop. After that, we are going to look at an example to get a better understanding of do-while loop and its working. These loop statements increase the performance of the code.


Below is the syntax of the do-while loop

do{

//Write the coding part here

}while(condition);

Note a very important point about the do-while loop, remember that the do-while will run at least once whether the condition is true or false it is going to run at least one time. The do-while loop starts with the do keyword. After the do keyword, the coding block comes, and at last, the end of the loop a while condition part is used in which some condition is given and this condition ends with a semicolon. If the condition is true then the do-while loop runs again. And if the condition is false then the loop terminates.

Now we are going to look at a simple example for a user to enter a password. The loop will keep iterating and repeat until the user enters the correct password. On entering the correct password the while condition will become false and the do-while loop will terminate. Let us look at the example given below.



 

At the start of the code, we used two header files <stdio.h> and <string.h>. The <stdio.h> file deals with standard input and output functions like print() and scanf(). And <string.h> deals with string functions like strcmp().

We declared a char array variable named it as password and assigned it a value "secret" After that we took another variable named as input[20] with an array size of 20.

In the do-while loop, we prompt the user to enter the password in the input[20] variable. In the do-while loop, we used the condition that will check whether the given value is correct or not.

We used strcmp() function and used it like this strcmp(input, password) the strcmp() returns 0(zero) when the two compared strings are equal which are input and password, and returns non-zero when strings are not equal.

When two strings are equal it will return 0. The do-while loop will terminate, and access will be granted to the user for a valid password.

Congratulations we have successfully implemented the do-while loop with an example.






Comments

Popular posts from this blog

How if else-if else ladder statement works in C program.

How to add two numbers in C programming language