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