Tumgik
Text
BRANCHING MECHANISM
WHAT IS BRANCHING MECHANISM and What I know about it
Branching Statement is a way to  control the flow of a program using branching  and looping statements. Branching statements give us code which is optionally executable, depending on the outcome of certain tests which we can define.
Branching statements allow the flow of execution to jump to a different part of the program. The common branching statements used within other control structures include: break , continue , return , and go to .   
EXAMPLES OF BRANCHING
Break
A branching statement that terminates the existing structure.
Continue
A branching statement that causes a loop to stop its current iteration and begin the next one.
Return
A branching statement that causes a function to jump back to the function that called it.
Goto
An unstructured branching statement that causes the logic to jump to a different place in the program.
Exit
A predefined function used to prematurely stop a program and return to the operating system.
0 notes
Text
LOOPS in C++
NESwhile loop What is a loop
A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages − C++ programming language provides the following type of loops to handle looping requirements.
Tumblr media
Loops has different types that can be used in many ways
Loop Type & Description
WHILE LOOP
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
#include <iostream>
using namespace std;
int main()
{
   int number, i = 1, factorial = 1;
   cout << "Enter a positive integer: ";
   cin >> number;
       while ( i <= number) {
       factorial *= i;      //factorial = factorial * i;
       ++i;
   }
   cout<<"Factorial of "<< number <<" = "<< factorial;
   return 0;
}
FOR LOOP
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
#include <iostream>
using namespace std;
int main()
{
   int i, n, factorial = 1;
   cout << "Enter a positive integer: ";
   cin >> n;
   for (i = 1; i <= n; ++i) {
       factorial *= i;   // factorial = factorial * i;
   }
   cout<< "Factorial of "<<n<<" = "<<factorial;
   return 0;
}
DO WHILE LOOP
Like a ‘while’ statement, except that it tests the condition at the end of the loop body.
int main()
{
   float number, sum = 0.0;
       do {
       cout<<"Enter a number: ";
       cin>>number;
       sum += number;
   }
   while(number != 0.0);
   cout<<"Total sum = "<<sum;
       return 0;
}
NESTED LOOP
You can use one or more loop inside any another ‘while’, ‘for’ or ‘do..while’ loop.
#include <iostream> #include <conio.h> using namespace std; int main() {    int i=1,j;    while (i <= 5)    {        j=1;        while (j <= i )        {            cout <<j;            j++;        }        cout << endl;        i++;    }    getch();    return 0; }
0 notes