Friday 29 September 2017

Types Of For Loop

In the previous post, we understand how for loop works and three parts of for loop.In this post, I will discuss the types of for loop or in which way you will use for loop.
For loops are mainly two types:

  • Infinite for loop
  • Nested for loop
THE INFINITE FOR LOOP:-
A loop becomes infinite loop if the condition never becomes false. For loop required three parts to run initialization, test expression, and update. if you leave the conditions empty that will make the endless loop.

Syntax:-
#include <iostream>
using namespace std;
 
int main () {
   for( ; ; ) {
      cout<<"This loop will run forever.\n";
   }

   return 0;
}
when the conditional expression is absent, it is assumed to be true. you may use initialization and update expression but most of the
programmers prefer "for(; ;)" to construct an infinite for loop.

NOTE:- you will use CTRL+C to terminate the infinite for loop.
use printf("This loop will run forever.\n"); instead of cout<<"This loop will run forever.\n"; do it yourself and see what happened.

NESTED FOR LOOP:-
The placing of one loop inside the body of another loop is called nesting.In nested for loop, a for loop is present inside the another for loop. 

Syntax:-
for ( initialization; test condition; increment ) {

   for ( initialization; tset condition; increment ) //using another variable{
      statement(s);
   }
 
   statement(s);
}

program to print number in the right triangular pattern:-

#include <iostream>
using namespace std;
int main()
{
    int i,j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
            cout<<j;
            cout<<"\n"; 
    }
    return 0;
}
Output:-
1
12
123
1234
12345

now take a look how nested loop works? the outer for loop start working,i=1 then it will compare to test condition 1<=5 if the condition is true then it will go to inner for loop and give an increment. In inner for loop j=1, and test condition is 1, i is 1 and j<=i now it will run once and exist from inner for loop and go to outer for loop.Next time i become 2 outer for loop will run once but inner loop will run twice.The process will repeat again and again whenever test condition does not become false.
    
    happy coding....keep learning.

Sunday 24 September 2017

Iteration Statement- For Loop

Iteration is a process where a set of instruction or statement is executed repeatedly for a specified number of time or until a condition is met.Iteration statements are commonly known as loops.There are three types of looping statements in C++:

A looping is consist of three parts:- initialization, test expression, increment/decrement or update value. A loop will run whenever the test condition is not met if the test condition is met then the loop will break and show the result on the screen.

For loop is most commonly used looping technique because in for loop all three parts of the loop: initialization, test expression, increment/decrement or update value in the same line.

Syntax:
for(intialization;test expression;increment/decrement)

{
 
 //body of loop
 
}
In the above syntax, you will see three parts of the loop in the same line and the three parts are separated by the semicolon, not by the comma's.

Program to print 1 to 10 number by using for loop:
#include<iostream>
using namespace std;
int main(){
 int i;
 cout<<"numbers from 1 to 10"<<endl;
 for(i=1;i<=10;i++){
  cout<<i<<endl;
  
 }
 return 0;
}
Output:
numbers from 1 to 10
1
2
3
4
5
6
7 
8
9
10

Now, let understand how for loop works? Here we have the initial value of i as 1(initialization).The test condition is i<=10 and update expression is i++.
In the beginning, the control goes to the initial condition and assigns the value 1 to i. Now its check whether 1<=10, if the condition is true then the body of the loop will execute and give an increment to i and assign the value 2 to i. Again its check whether 2<=10, if the condition is true then the body of the loop will execute. The whole process will run whenever the test condition is not met. When i becomes equal to 11, the condition i<=10 becomes false and the loop terminates and the program ends.


Friday 22 September 2017

Selection Statement- the switch case statement

THE SWITCH CASE STATEMENT:-
A switch statement is used for multiple-way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type.

syntax:
switch (expression)
{
  case value1:
    code segment1;
    break;
  case value2:
    code segment2;
    break;
.
.
.
  case valueN:
    code segmentN;
    break;
  default:
    default code segment;
}
Above is the representation of the switch case. In switch-case, the program asks you about your choice and that choice will run. now understand its syntax. First, we provide the choice to the switch case and that particular choice will run after that program shows the result, it doesn't go to another part of the program because that particular case terminated by the break. The default choice is there if anyone tries to run the wrong choice it will show the default code segment.

program for basic calculation by using switch case:
#include<iostream>
using namespace std;
int main(){
 int ch,x,y,z;
 cout<<"1.ADDITION"<<endl;
 cout<<"2.SUBTRACTION"<<endl;
 cout<<"3.MULTIPLY"<<endl;
 cout<<"4.DIVISION"<<endl;
 cout<<"5.MODULUS"<<endl;
 cout<<"Enter your choice:";
 cin>>ch;
 switch(ch){
  case 1:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x+y;
   cout<<"Addition of two no. is "<<z<<endl;
   break;
  case 2:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x-y;
   cout<<"Subtraction of two no. is "<<z<<endl;
   break;
  case 3: 
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x*y;
   cout<<"Multiplication of two no. is"<<z<<endl;
   break;
  case 4:
      cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x/y;
   cout<<"division of two no. is "<<z<<endl;
   break;
  case 5:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x%y;
   cout<<"Modulus of two no. is "<<z<<endl;
   break;
  default:
  cout<<"Wrong choice";
 }
 return 0;
}

output:
1.ADDITION
2.SUBTRACTION
3.MULTIPLY
4.DIVISION
5.MODULUS
Enter your choice:1
Enter first no:12
Enter second no:12
Addition of two no. is=24
In the above program, there are five choices if someone wants to choose a particular function just input the choice number and that particular function will run. The break will separate the function if you are not using the break after the break all functions will run. The default will show the wrong choice here.
Note: if you don't know what is the modulus? Please click here

Difference between if-else and switch case:

  • An if-else decision is taken on basis of the input wherein the switch-case decision is taken by the user.
  • if-else is more complex for the lengthy condition than switch-case.
  • if-else can check multiple conditions at a time where switch-case check the only single condition at a time








Monday 18 September 2017

How to write a program without using semicolon in C++?

As we know every line in C++ ends with the semicolon but we can print a string without using any semicolon in the program. we can print string by using if-else, looping or switch case but here I'm print a string with if-else without using the semicolon.

program to print a string without using any semicolon:-
#include<iostream>
using namespace std;
 main()
{
 if(cout<<"Hello World"){
 }
}
 output:-
Hello World

In the above, you see that there is no semicolon used in the program and also it is most frequently asked interview question.


Thursday 14 September 2017

Decision Making Statement:- If-else Statement

Control Statements enable us to specify the flow of program control,i.e. the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.

There are four types of control statements in C++:
  •  Decision Making Statements
  •  Selection Statements 
  •  Iteration Statements 
  •  Jump Statements 
Decision-Making Statement: the if-else statement

The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test(i.e. whether the outcome is true or false).
                     flowchart of if-else statement


Here, we give a condition to the program if the condition is true then the body inside the if statement is executed and, code inside the if will run, otherwise else statement is executed and, the code inside the if is skipped.

syntax:
if (condition)

{

  statements

}

  else

{

  statements

}
program to check whether a person is adult or not:
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter the age:";
cin>>age;

if(age<18)
{
 cout<<"juvenile"<<endl; 
}

else 
{
 cout<<"Adult"<<endl;
}

return 0;
}
output:-
Enter the age:16
juvenile
Enter the age:21
Adult

Nested if and if-else statement

It is possible to embed or to nest if-else statements one within the other. Nesting is useful in situations where one or several different courses of action need to be selected.

syntax:
if(condition1)
{
// statement(s);
}
else if(condition2)
{
//statement(s);
}
.
.
.
.
else if (conditionN)
{
//statement(s);
}
else
{
//statement(s);
}

here, the program checks the condition of if  ,if-else and else, from these conditions which are true will be executed rest are skipped by the program.

program to find the greatest of three numbers:-
#include <iostream>
using namespace std;

int main()
{
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if((n1 >= n2) && (n1 >= n3))
        cout << "Largest number: " << n1;
    else if ((n2 >= n1) && (n2 >= n3))
        cout << "Largest number: " << n2;
    else
        cout << "Largest number: " << n3;
    
    return 0;
}
output:-
Enter three numbers:12
34
23
Largest number:34

In the above program, if n1>n2 and n1>n3 then if statement will be executed, if n2>n1 and n2>n3 then if-else statement will be executed, if n3>n1 and n3>n2 then else statement will be executed.


Saturday 2 September 2017

What is bits/stdc++.h in C++?(master file in C++)

<bits/stdc++.h> is a master file in c++. It is basically a header file which includes every standard library file. you need this file when you don't know which standard file is included in the program. It saves our time and solves the confusion. Let take an example if you want to find square root of a number, you have to include <math.h> file but if you use  <bits/stdc++.h> you don't need any other file.

#include<bits/stdc++.h>
using namespace std;
int main()
{
   float a;
   cout<<"Enter the number:";
   cin>>a;
   cout<<"Square root of the number is:"<<sqrt(a)<<endl;
   return 0;
}

output:-

Enter the number:3
Square root of the number is:1.73205

NOTE: <bits/stdc++.h> is use instead of <iostream> in C++.

ADVANTAGES:-


  1. In the competitive contest, using this file is a good idea, when you want to reduce the time wasted in doing chores, especially when your rank is time sensitive.
  2. It reduces typing in the program and solves the confusion.
  3. you don't have to remember all the  STL of GNU C++ for every function you use.
DISADVANTAGES:-


  1. It is not a standard header file of GNU C++ library. If you use other compilers it might fail. Like there is no such file in MICROSOFT VISUAL STUDIO.
  2. Using it would increase lots of unnecessary stuff and increases compile time.
  3. This header file is not a standard header file of C++ so, non-portable and should be avoided.