Faculty:Pedagogy,physics teacher 3/5 İntroduction to programming A program is a set of instructions that tell the computer to do various things; sometimes the instruction it has to perform depends on what happened when it performed a previous instruction. This section gives an overview of the two main ways in which you can give these instructions, or “commands” as they are usually called. One way uses an interpreter, the other a compiler. As human languages are too difficult for a computer to understand in an unambiguous way, commands are usually written in one or other languages specially designed for the purpose. Structure of a C++ program A C++ program is structured in a specific and particular manner. In C++, a program is divided into the following three sections: Standard Libraries Section Main Function Section Function Body Section For example, let’s look at the implementation of the “Hello Elshana!”program: 1. #include <iostream> 2. using namespace std; 3. int main() { 4. cout << "Hello Elshana!" << endl; 5. return 0; 6. } Standard libraries section: #include <iostream> using namespace std; #include is a specific preprocessor command that effectively copies and pastes the entire text of the file, specified between the angle brackets, into the source code. The file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command contains code for displaying and getting an input from the user. Namespace is a prefix that is applied to all the names in a certain set. iostream file defines two names used in this program - cout and endl. Main function section: int main() {} The starting point of all C++ programs is the main function. This function is called by the operating system when your program is executed by the computer. { signifies the start of a block of code, and } signifies the end. Function body section: cout << "Hello Elshana" << endl; return 0; The name cout is short for character output and displays whatever is between the << brackets. Symbols such as << can also behave like functions and are used with the keyword cout. The return keyword tells the program to return a value to the function int main After the return statement, execution control returns to the operating system component that launched this program. Execution of the code terminates here. While statements-is the simplest of the three loop types that C++ provides, and it has a definition very similar to that of an if statement. A while statement is declared using the while keyword. When a while statement is executed, the condition is evaluated. If the condition evaluates to true, the associated statement executes.However, unlike an if statement, once the statement has finished executing, control returns to the top of the while statement and the process is repeated.This means a while statement will keep looping for as long as the condition evaluates to true. while (condition) statement; If Statement-Use the if statement to specify a block of C++ code to be executed if a condition is true. In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print some text: Example if (20 > 18) { cout << "20 is greater than 18"; } Arrays- are used to store multiple values in a single variable, instead of declaring separate variables for each value. The array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.To create an array of three integers, you could write: int myNum[3] = {10, 20, 30}; For statements-The for statement is preferred when we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables. Let’s take a look at a sample for loop and discuss how it works: for (int count{ 1 }; count <= 10; ++count) std::cout << count << ' '; Do while statements-Consider the case where we want to show the user a menu and ask them to make a selection and if the user chooses an invalid selection, to ask them again. A do while statement is a looping construct that works just like a while loop, except the statement always executes at least once. After the statement has been executed, the do-while loop checks the condition. If the condition evaluates to true, the path of execution jumps back to the top of the do-while loop and executes it again. do statement while (condition); Rand(Random number generation)- is a program that takes a starting number (called a seed), and performs mathematical operations on it to transform it into some other number that appears to be unrelated to the seed.It then takes that generated number and performs the same mathematical operation on it to transform it into a new number that appears unrelated to the number it was generated from.By continually applying the algorithm to the last generated number, it can generate a series of new numbers that will appear to be random if the algorithm is complex enough. Char-The char data type is an integral type, meaning the underlying value is stored as an integer, and it’s guaranteed to be 1-byte in size. However, similar to how a boolean value is interpreted as true or false. You can initialize char variables using character literals, printing chars, printing chars as integers via type casting, inputting chars. The break statements-When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.It can be used to terminate a case in the switch statement. If we are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax: break;