Pages

Hello there!

Powered by Blogger.

Monday, January 7, 2013

C CODES


CONTINUE STATEMENT IN C


CONTINUE STATEMENT IN C WITH EXAMPLE


Continue statement tell the compiler to skip the following statement and continue with the next iteration

Syntax:- 

continue;

Continue statement in for loop

Here continue causes the control to go to increment section(3rd section in for loop) of the loop than condition statement(2nd section in for loop).

Example:-

c program for display even number using continue statement


Continue statement in while and do while loop

Here continue causes the control to go to condition statement of loop.

Example:-

c program display odd number using continue statement


BREAK STATEMENT IN C


BREAK STATEMENT IN C WITH EXAMPLE


Break statement is used to exit from loop

Break statement in loops

Break statement immediately transfer control to the out of loop and statement which is immediately after loop is executed. In other word break statement terminate loop.

Note:-
# Break is terminated only one loop in case of nested loop

Example:-

c program to count N number using break statement


Example:-

Break statement in nested loop



Break statement in switch

The break statement transfer the control to out of the switch statement and statement which is immediately after switch is executed. 

Note:-
# break is optional, if 2 or more case labels belongs to same statement.

Program to accept day number of week and display the corresponding week day



SWITCH CASE IN C


SWITCH CASE IN C WITH EXAMPLES


The Switch statement tests the value of a given expression against a list of case labels, when they get matches, block of statement associated with that case in executed.

The general form of switch case

switch (expression)
{
case label1:
block of statement
break;

case label2:
block of statement
break;

case label3:
block of statement
break;
.
.
.

case labeln:
block of statement
break;

default:
block of statement
break;
}


Note :-


# The Expression and label are integer or characters.
# floats are not allowing in case labels.
# labels much be unique.
# block of statement may be zero or more than one statements.
# Case labels end with colon(:).
# The break is followed by semicolon(;).
# Default case is optional case.
# break is optional, if 2 or more case labels belongs to same statement.

Default case is optional case.

How Case Statement Execute

The expression is made to compared with each case labels, if they matches each others than block of statements related to particular case is executed and than control is transferred to statement that following the switch case.

The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement that following the switch case.

If value of expression is does not match with case label than default statement is execute and than control is transferred to statement that following the switch case.

Examples

Program to accept day number of week and display the corresponding week day




Program to display position of vowel in alphabet 




Note :- U is different from u.

NESTED LOOP IN C


NESTED LOOP IN C WITH EXAMPLE


c allows for, while and do while loops to be nested. that is one loop is inside another loop.

Example:-


Output:-

.....
....
...
..
.

one type of loop can be nested with other type of loop. for example while loop is inside for loop.

Example:-


DO WHILE LOOP IN C


DO WHILE LOOP IN C WITH EXAMPLE


Do While loop is another loop in c

Syntax:- 

do
{
Body of the loop
}
while(condition);
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not need.
# Body of the loop may have one or more statement.
# Semicolon is present after while.
# At least once body of loop is executed even condition is false.

Execution:-

In do while loop 1st body of the loop is executed than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

Hence body is 1st executed before the condition check so body of loop is always executed at least once.

Example:-

Program to count N numbers


Program to display even number up to 20



Example:-

To count N number to show condition is checked at bottom


Output:-
Enter a number 6

2
4
6
8
10
12
14
16
18
20
22

Here 22 also displayed even we put condition (i =<20)because condition is checked at bottom of loop.http://genuineptr.com/pages/index.php?refid=asangidevaraj

WHILE LOOP IN C


WHILE LOOP IN C WITH EXAMPLE


While loop is simplest loop in c

Syntax:- 

while(condition)
{
Body of the loop
}
Statement;

Note:-

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

Execution:-

In while loop 1st condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

Example:-

Program to count N numbers

Program to display even number up to 20



FOR LOOP IN C


FOR LOOP IN C WITH EXAMPLE


For loop is another loop in c.

Syntax:- 

for(initialization; condition; increment)
{
Body of the loop
}
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.
# Initialization and increment can be skip but semicolon should be there to separate 3 section.
# Condition section can`t be skip in for loop.

Execution:-

In for loop 1st variable is initialized than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, variable is incremented and the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

For loop can be written in another way by skipping initialization and increment condition.

Syntax:-

Initialization;
for( ; condition; )
{
Body of the loop
Increment;
}
statement

Variable can be declared in for loop

Example:-

for(int i=0; i< num; i++) Note:- for loop initial declaration are only allowed in c99 mode. Example:-

Program to count N numbers

Program to display even number up to 20


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