Pages

Hello there!

Powered by Blogger.

Monday, January 7, 2013

DECREMENT OPERATOR IN C


DECREMENT OPERATOR IN C WITH EXAMPLE


The decrement operators used to subtract 1 to previous value. Decrement operator is unary operator because only one variable is used.

There are 2 type in decrement operator. They are
1. Postfix decrement operator
2. Prefix decrement operator

Syntax:-

For postfix

variable--

For prefix

--variable

Both prefix and postfix decrement operator are same thing when they used independently(i-- or --i) ie subtract 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i-- or num=--i)

Postfix decrement operator:-

Output:-

i=1
num=2

Execution:-

In postfix decrement operator, fist assign value to left side variable(num) than decrement by 1(i)

Here in our example first i is three(i=3) than i decrement by 1(i--). here i value is 2. 

Next statement is num = i--;

Here postfix is used so first i value is assigned to num ie 2 than i is decrement by 1 ie i value becomes 1.

Prefix decrement operator


Output:-

i=1
num=1

Execution:-

In prefix decrement operator, first decrement(i) by 1 than result value is assigned to left side variable(num)

Here in our example first i is three(i=3) than i decrement by 1(--i). here i value is 2. 

Next statement is num = --i;

Here prefix is used so first i is decrement by 1 ie i value becomes 1. Than value of i is assigned to num ie 1

Example:-

Program to fined sum of N odd number.


INCREMENT OPERATORS IN C


INCREMENT OPERATORS IN C WITH EXAMPLE


The increment operators used to adds 1 to previous value. Increment operator is unary operator because only one variable is used.

There are 2 type in increment operator. They are
1. Postfix increment operator
2. Prefix increment operator

Syntax:-

For postfix

variable++

For prefix

++variable

Both prefix and postfix increment operator are same thing when they used independently(i++ or ++i) ie adds 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i++ or num=++i)

Postfix increment operator:-

Output:-

i=2
num=1

Execution:-

In postfix increment operator, fist assign value to left side variable(num) than increment by 1(i)

Here in our example first i is zero(i=0) than i increment by 1(i++). here i value is 1. 

Next statement is num = i++;

Here postfix is used so first i value is assigned to num ie 1 than i is increment by 1 ie i value becomes 2.

Prefix increment operator



Output:-

i=2
num=2

Execution:-

In prefix increment operator, first increment(i) by 1 than result value is assigned to left side variable(num)

Here in our example first i is zero(i=0) than i increment by 1(++i). here i value is 1. 

Next statement is num = ++i;

Here prefix is used so first i is increment by 1 ie i value becomes 2. Than value of i is assigned to num ie 2

Example:-

Program to fined sum of N odd number.


CONDITIONAL OPERATOR IN C


CONDITIONAL OPERATOR IN C WITH EXAMPLE


Conditional operator is alternative to simple if-else statement

Syntax:-

expr1 ? expr2 : expr3

Execution:-

expr1 is always evaluated, if results is true than expr2 is executed otherwise expr3 is executed.

Example:-

Program to find given number as even or odd.

ELSE IF STATEMENT IN C


ELSE IF STATEMENT IN C WITH EXAMPLE


else if statement is used when multipath decision are involved.

Syntax:-

if(condition 1)
{
statement 1
}
else if (condition 2)
{
statement 2
}
else if (condition 3)
{
statement 3
}
.
.
.
else if (condition N)
{
statement N
}
statement

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Any one statement is executed not all.

Execution:-

The conditions are checked from top to bottom when true condition is found than statement related to that condition is executed and control is transfer to statement next to else is statement
when all condition are false than statement related to else is executed and control is transfer to statement next to else is statement

Examples:-

Program to solve following problem

Average marks Grade
100-75 1
74-50 2
49-30 3
29-30 4

NESTING OF IF – ELSE STATEMENT IN C


NESTING OF IF – ELSE STATEMENT IN C WITH EXAMPLE


When a series of decision are involved, we may have to use more than one if else statement in nested form as shown below

Syntax

if(condition 1)
{
if(condition 2)
{
True - Block of Statement 1
}
else
{
false - Block of Statement 2
}
}
else
{
false - Block of Statement 3
}

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Either true – block of statement or false – block of statement is executed, not both.

Execution:-

When both condition 1 and condition 2 are true than True - Block of Statement 1 is executed.
If condition 1 is true and condition 2 is false than false - Block of Statement 2 is executed.
If condition 1 is false than false – blocked statement 3 is executed.
In each case control is moved to statement that is next to if statement.

Examples:-

Program to solve following problem
The candidate is selected if his age and height is 18 and 5 and more respectively.

IF – ELSE STATEMENT IN C


IF – ELSE STATEMENT IN C WITH EXAMPLE

The if – else statement is an extension of the simple if statement.


Syntax:-

if(condition)
{
True - Block of Statement
}
else
{
False - Block of Statement
}
Statement;

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Either true – block of statement or false – block of statement is executed, not both.

Execution:-

If condition is true than true - block of statement is executed otherwise false - block statement is executed than control is moved to statement next to if statement.

Examples:-

Program to find even number


Program to find greatest number between 2.







http://genuineptr.com/pages/index.php?refid=asangidevaraj

IF DECISION MAKING STATEMENT IN C


IF DECISION MAKING STATEMENT IN C WITH EXAMPLE


The if statement is a powerful decision making statement and used to control the flow of execution of statements.

Syntax

if(condition)
{
Block of Statement
}
statement;

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.

Execution:-

If condition is true than block of statement is executed otherwise statement next to if statement is executed.

Example:-