Showing posts with label C language. Show all posts
Showing posts with label C language. Show all posts

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

Sunday, April 19, 2020

Pointer in C Language


Pointer in C Language:


pointer in c language
pointer in c language

A pointer in c language is defined as a variable that store memory address or that contains address of another variable where addresses are the location number always contains a number.
 So, pointer always contains the number.

 It is called pointer because it points to a specific location in memory by storing address of that location.


Syntax of pointer in c language:


syntax of array

Operators in pointer in c language:


 When a pointer is declared in a program, it contains garbage value i.e. it will point toward any value in the memory.

Two operators which are utilized in the pointer i.e. address operator (&) and indirection operator or deference operator (*).

  1.   Indirection operator gives the values stored at a specific address.  
  2.     Address operator can't be used in any constant or any expression. 


Example:


example of indirection and address operator




Pointer assignment:



 int i=10;

 int *p=&i;        //     value assigning to the pointer


Here declaration tells the compiler that "P"  will store the address of integer value or in other word "P" is a pointer to an integer and *p reads the value at the address that "P" contains.

P++;

We can give the value of 1 pointer variable to others when their base type and data type is identical or both the pointer points to the identical variable as in the: 


Array:

pointer assignment






We can give constant 0 to a pointer of any type for that specific constant 

‘NULL’ is used such as:

 *p=NULL;

 Which Operations are performed in pointer???

   


Addition of any number using pointer:



Example:

addition of a number through pointer
 

Subtraction of any number from a pointer:




Example:


subtraction of a number through pointer



Arithmetic operation never performs on pointer are:


1-    Addition, multiplication and division of two-pointer.
2-    Multiplication between the pointer by any number.
3-    Division of pointer by any number.
4-    Addition of float or double value to the pointer.


Increment and decrement operator:


The precedence level of difference operator increment or decrement operator is same and their associativity from right to left.



example of increament operator in c language



since the operator associated from right to left, so we are using increment operator to the pointer p.


increment structure in c language



Pointer Comparison:

  1.      Pointer variable can be compared when both of the variables, object of the identical data type and when both of the pointers of variable points to element of the same array. 
  2.     ==,!=,<=,,>=, can be used with a pointer.
  3.     Equal and not equal operators used to compare two-pointer should find whether they contain same address or not and they will equal only if are null or contains the address of same variable. 


Example:-

example of pointer comparison in c language

Pointer to pointer:

Addition of  a pointer variable stored in another variable is called

pointer to pointer variable.


Syntax of pointer to pointer:



syntax of pointer to pointer

Pointer vs array:


Let’s take a glance at a simple example to differentiate between pointer and 


pointer vs array in c


In the above example, at the first time printf ( ), print the identical/same value array and pointer.

Here array “arr”, as pointer to character and “p” acts as a pointer to array of character.

 After we attempt to extend the value of arr it'd give the error because its known to compiler about an array and its base address which is usually printed to base address is thought as a constant pointer and also the bottom address of array which isn't allowed by the compiler.



THANKS FOR READING THE ARTICLE I HOPE THAT IT HELPED U





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...