Monday, April 27, 2020

If else in c


IF Else in C:




if else in c




If-else in c:

If else is the most used condition in our life. Whenever we need to make any decision we use this condition in our daily life.  if-else in c is used when we are required to execute a specific block of code and that block is executed when it met the condition given to it. We call this process Decision making.
In this article, we will discuss how if-else statement are used in programming and also look at some examples of it.

Types of if-else statement:

If else statements are of following types:
1.   If statement
2.   If-else statement
3.   Else-if statement
4.   Nested if-else statement.

If statement:


First let’s take a look at the syntax of the if statement:

Syntax:

If (condition is given)
{
// Any statement to execute in its body
}
//   Any Statement outside its body.
Here we can see that if the condition given at the entrance is turned true then the body will execute otherwise it will execute the statement outside of the body.


If-else statement:


In the if-else statement, there are two conditions one at the entrance of if is given if it turns to false then the code inside its body is not executed then the code inside the body of else is executed.

Syntax:

If (condition is given)
{
//statement in the body of if
}
Else
{
//Any statement inside the body of else
}

Example of if-else in c:


Let’s look at a simple flow diagram example of if-else statement.


flow diagram of if else c



Example of Else if in c:


Let’s look at another example of if else statement.

A simple C program to display the greater number using the if else statement.

Source code:



#include<stdio.h>

Int main ()

{

Int x=15;

If(x>20)

{
Printf(“ x is greater than 20”);

Else

{

Printf(“ x is not greater than 20”);

}

return 0;

}

Output:

X is not greater than 20.

Else-if Statement:


Let’s look at the syntax of the else-if statement.

Syntax:


If (condition 1 given)
{

First Statement;

}
Else if(  condition 2 given)

{

Second statement;

}

Else if ( condition 3 given)

{

 Third Statement;

}

Else

default statement;


Example of else if in c:


Let’s look at a simple example of else if statement.
 A simple program to check whether the number is divisible by 4 and 5 or not.

 Source Code:

#include<stdio.h>
Int main()
{
Int x;
Printf(“enter the of x”);
Scanf(“%d”, &x);
If(x%4==0 && x%5==0)
{
Printf(“x is divisible by both 4 and 5”);
}
Else if(x%4==0)
{
Printf(“x is divisible by 4”)
}
Else if(x%5==0)
{
Printf(“x is divisible by 5”)
}
Else
{
Printf(“x is not divided by both  4 and 5”);
}
}

Nested If-else in c:


Nested if-else means when another if or else statement is used in the body of the if-else statement. Then that if-else is said to be nested if-else in c.

Let’s take a look at the syntax of nested if-else in c:

Syntax:


If (condition is given)

{

If (any expression)

{


 Any statement no 1;
}

Else

{

 Statement number 2;

}

}

Else

{

Statement number 3;

}

Example of Nested if-else in c:


Let’s take a look at a simple example of nested if-else statement in which we will  check which number s greater among the three numbers.


Source Code:

#include<stdio.h>
Int main()
{
Int  x,y,z;
Printf(“Enter any three values”);
Scanf(“%d%d%d, &x,&y,&z);
If(x>y)
{
If(x>z)
{
Printf(“ x is the greatest among all”)
Else
{
Printf(“ z is the greatest among all”);
}
Else
{
If (y>z)
{
Printf(“y is greater among all”);
Else
{
Printf(“z is greater among all”);
}
}

The basic concept in if-else statement is that in case if the condition given at the entrance of the body of IF turns to be wrong then its body will not executed and the statement that we have written in the body of else will be executed.

Thanks for reading the article

Saturday, April 25, 2020

Loops In C++


Loops In C++:




Loops In C++


What is Loop??


Loop in C++ is defined as any statement that runs the specific part of a code or any statement more than one time.


If we look for the types of loops in C++ which can be used we found three types of it.


Types of Loops In C++:


Following are the types of loops:

1.   for loop
2.   while loop
3.   do-while loop 


Each one of them has its own syntax, advantages, disadvantages, and Application.

When we want to run a specific part of a code for more than one times then Loops are used. It continues to run until some condition is fulfilled.

If we define a loop we will say that “It is a set of instructions in which we check the condition for the execution of loop”.

If In any cases when the condition given to the loop is not fulfilled it return back to the first instruction which is given in the sequence of instructions and it continues to repeat the execution process of the statements in that specific block.

And If the condition given to the loop has reached, so it means that the condition has been fulfilled, and control breaks out and comes out of the Loop and the remaining code is then executed.

 For Loop:


For Loop is used when we know the exact number of iteration or we can say that it is used when we know how many times a loop is going run.

The code given inside the body of For Loop executes only if the condition given at the entrance of the loop is fulfilled. It means that the condition given by us is fulfilled then the code inside the body of the loop is executed. The loop runs only for the number of times that we have specified.



Syntax:


The syntax of the for loop is following:

For (initialization expression; test expression; update expression)

{

// any statements in the body of the loop;

}


Initialization Expression:


In initialization expression:

We initialize a variable to loop and then we give a value to it.


Test Expression:


In test Expression:

we give the condition to the loop then If the condition turns true, then the code inside of the loop is executed and it updates the variable we initialized too variable. And if the condition turns false then For loop will exit.

Update Expression:


In the update expression:
When the condition of the loop turns true than here in update expression we apply the increment or decrement to the value of the variable. 



Example:


A  simple example of For loop is following:


Source code:


#include <iostream>

using namespace std;

int main()

{

for (int a =0 ; a <= 3; i++)

{

cout << " FOR LOOP:\n";

}

return 0;

}


Output:


FOR LOOP:
FOR LOOP:

FOR LOOP:


While Loop:


Another type of Loop used is While Loop. In while loop, we simply give a condition to the Loop the loop will continue to execute until the condition remains true.


If the condition given to loop turns wrong in the start then it will exit and again start the checking process until the condition is met.


Syntax:


The syntax of while loop is following:

While(  condition )

{
     
 The statement for the execution;


}

Example:


A simple example for while Loop.


Source code:


#include <iostream>

using namespace std;

int main()

{

 Int i=0;

while ( i <7)

{

cout << i<<endl;

i++;


}


Output:


0
1
2
3
4
5
6


Do-While Loop:


In do-while loop the condition given to the program is checked after the execution of the program it means that the program at least runs for one time. Then if the condition is true then further the program executes.

 This is the main difference between while and do-while loop. In a while loop, the condition is checked first and then the program is executed.


Syntax:


The syntax of While Loop is the following:

Initialization expression;

do

{

// statements to execute in the body of the loop

update_expression;

}

while (test_expression);

Another difference between the do-while loop and other loops is that:

In do-while loop, we use a semicolon at the end of the body of the loop, while  in other two loops do not use any semicolon at the end of the body.


Example:


A simple program of the do-while loop.

Source code:

#include <iostream>

using namespace std;

int main()

{

int b = 5;    //  Here we initialize the expression

do

{

cout << " do while loop\n";

b++;       //   Here we are incrementing/updating the expression

}

while (i < 1);      // here we are testing the expression

return 0;

}


Output:


Do while loop




Friday, April 24, 2020

Pointer in C++


Pointer in C++:


pointer in c++ programming
pointer in c++

What is pointer??


Pointers in c++ are variables that store integer values but those integers are the memory address of the other variables. A pointer that stores the address of any variable suppose we say that it stores the address of “x” then that pointer is said to pointing toward that “x”. 

When we declare a variable, the computer relates that variables name with a specific location in the memory and then stores that value there.

 Now whenever you refer to the variable by name in your code, the computer will always follow these two steps:

  1.               First of all, lookup the address that the variable name corresponds to
  2.        Then it will go to that location in memory and retrieve or set the value already stored in it.


Syntax:


The syntax of the pointer is the following:

data type * pointer_name;

Here we can use any type of data.


Initialization of pointer:


We can create a pointer exactly the same way;

int* a;

 Just Like every other object, we have to initialize a pointer before we can use it.

we can create a pointer to any type of object. The pointers take up 4 bytes and store a single numerical value.

we know that if we create an int without assigning it a value, it'll be just some garbage value. Whatever is value is there in the memory at the time.



Declaration of Pointer:


 If we want to declare a pointer variable named ptr, that will point to an integer variable of the name x:

 int * ptr = &x;

Here “int *ptr” creates a pointer to an integer value, which we initialize to the address of x.

Dereferencing of a Pointer:


There is something special that a pointer can do and that is we get back whatever object is in the memory location of that single numerical value. It means if my pointer stores the value of any location, when I dereference the pointer, I get back to that memory location and whatever is at that memory location will be retrieved.


How we know what numerical value to put into a pointer???

 So here is the answer, there exists an operator "&" which will return the address of any object (i.e. the value at that place in memory where that object is).
Int a; //any integer value
&a;// the address of that integar


Example:


A simple program to display the address of a pointer.

Source Code:

 #include<iostream>
 using namespace std;
 int main ()
{
int vari1;
 int vair2;
 cout << "address of vari1 is: ";
 cout << &var1 << endl;
 cout << "address of vari2 is: ";
 cout << &var2 << endl;
 return 0;
 }

Output:
address of vari1:
0xbfebd5c0
address of vari2:
0xbfebd5b6

Pointer Arithmetic:


Pointer arithmetic is the arithmetic of adding (and subtracting) values from a pointer. A pointer holds a memory address, so pointer arithmetic is the arithmetic of adding (and subtracting) values from a memory address.   
   
 If we are adding the value 1 to a pointer, it doesn’t mean that
 "We are adding 1 to that memory address we're holding".

We mean

 "To add to your memory to point at the next memory address along".
Now, let's add 1 to the pointer:


P=p+1; // now the pointer have the value of the next int.

Incrementing a Pointer:


The following program increments the variable pointer to access each succeeding element of the array :

Source code:


c++-program-for-increment-operator


Output:


program to display increment pointer in c++ programming


Decrementing a Pointer:

 A simple program to display the decrement of pointer.


Source code:

c++-program-for-decrement pointer


OUTPUT:



program to display decrement pointer in c++ programming


Pointer Comparisons:

A simple program to display a comparison of pointer.


Source code:


comparison-pointer-c++-program


OUTPUT:


program to display pointer comparison in c++ programming


Why we need pointers??


Pointers are crucial to the processing of strings. They are also necessary when we are making linked lists and using dynamic memory allocation. Using pointers for dynamic memory allocation means:



- We can use the same pointer for multiple memory locations.
- Assign this value to any other pointer with the same type or a void.




Thank You  for reading the article






If else in c

IF Else in C: If-else in c: If else is the most used condition in our life. Whenever we need to make any decision we use...