advance c programming

28
ADVANCE C PROGRAMMING LANGUAGE TUTORIAL infobizzs.in

Upload: steve-fort

Post on 22-Jul-2016

242 views

Category:

Documents


0 download

DESCRIPTION

Advance information about C programming Language..

TRANSCRIPT

ADVANCEC PROGRAMMING LANGUAGE TUTORIAL

infobizzs.in

SWITCH CASE / SELECT CASE

• These statements are used with the

replacement of if-else and these statements

are used when we have multiple options or

choices.

• These choices can be limited and when we use

switch case we can use only integer or

character variable as expression.

infobizzs.in

• Syntax:

switch( expression)

{

case value-1:

block-1;

break;

case value-2:

block-2;

break;

----

default:

default-block;

break;

}

statement -X;

infobizzs.in

LOOPING

• These statements are used to control the flow of theprogram and repeat any process multiple timesaccording to user’s requirement.

• We can have three types of looping controlstatements.

• while

• do-while

• for

infobizzs.in

While

• It is an entry control loop statement, because it checks the condition first and then if the condition is true the statements given in while loop will be executed.

• SYNTAX:-

Initialization;

while (condition)

{

Statements;

Incremental / decrement;

}

infobizzs.in

Do-while

• Do-while loop is also called exit control loop.

• It is different from while loop because in this loop the portion of the loop is executed minimum once and then at the end of the loop the condition is being checked and if the value of any given condition is true or false the structure of the loop is executed and the condition is checked after the completion of true body part of the loop.

infobizzs.in

• SYNTAX:-

Initialization

do

{

Statement;

Increment / decrement;

}

while (condition)

infobizzs.in

For loop

• It is another looping statement or construct used torepeat any process according to user requirementbut it is different from while and do-while loopbecause in this loop all the three steps ofconstructions of a loop are contained in singlestatement.

• It is also an entry control loop.

• We can have three syntaxes to create for loop:-

infobizzs.in

for (initialization; Test condition; Increment /decrement)

{

Statement;

}

…………………………………….

for (; test condition; increment / decrement)

{

Statement;

}

………………………………………

infobizzs.in

for (; test condition;)

{

Statement;

Increment / decrement

}

infobizzs.in

JUMPS STATEMENTS• These are also called as branching statements.

• These statements transfer control to another part ofthe program. When we want to break any loopcondition or to continue any loop with skipping anyvalues then we use these statements. There arethree types of jumps statements.

• Continue

• Break

• Goto infobizzs.in

• Continue

This statement is used within the body of the loop.The continue statement moves control in thebeginning of the loop means to say that is used toskip any particular output.

• WAP to print series from 1 to 10 and skip only 5 and 7.

#include

void main ( )

{

int a;

infobizzs.in

clrscr ( );

for (a=1; a<=10; a++)

{

if (a= =5 | | a= = 7)

continue;

printf (“%d \n”,a);

}

getch ( );

}

infobizzs.in

Break

• This statement is used to terminate any sequence or sequence of statement in switch statement. This statement enforce indicate termination. In a loop when a break statement is in computer the loop is terminated and a cursor moves to the next statement or block;

• Example:

• WAP to print series from 1 to 10 and break on 5

#include

void main ( )

{

infobizzs.in

int a;

clrscr ( );

for (a=1; a<=10; a++)

{

if (a= =5)

break;

printf (“%d \n”,a);

}

getch ( );

}

infobizzs.in

Goto statement

• It is used to alter or modify the normal sequence ofprogram execution by transferring the control tosome other parts of the program. the control maybe move or transferred to any part of the programwhen we use goto statement we have to use alabel.

• Syntax:

• Forward Loop:

goto label;

….

….

label:

statement;

infobizzs.in

• Backward Loop:

label:

statement

….

….

goto label;

infobizzs.in

CALL BY VALUE IN C LANGUAGE:-

In call by value, value being passed to the

function is locally stored by the function parameter

in stack memory location. If you change the value of

function parameter, it is changed for the current

function only. It will not change the value of variable

inside the caller method such as main().

infobizzs.in

Example of call by value:-

#include <stdio.h>

#include <conio.h>

void change(int num) {

printf("Before adding value inside function num=%d \n",num);

num=num+100;

printf("After adding value inside function num=%d \n", num);

}

int main() {

int x=100;

clrscr();

printf("Before function call x=%d \n", x);

change(x);//passing value in function

printf("After function call x=%d \n", x);

getch();

return 0;

}infobizzs.in

Output:-

Before function call x=100

Before adding value inside function num=100

After adding value inside function num=200

After function call x=100

infobizzs.in

CALL BY REFERENCE IN C:-

In call by reference, original value is modified because we pass reference (address).

Example of call by Reference:-

#include <stdio.h>

#include <conio.h>

void change(int *num) {

printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n", *num);

}

infobizzs.in

int main() {

int x=100;

clrscr();

printf("Before function call x=%d \n", x);

change(&x);//passing reference in function

printf("After function call x=%d \n", x);

getch();

return 0;

}

infobizzs.in

Output:-

Before function call x=100

Before adding value inside function num=100

After adding value inside function num=200

After function call x=200

infobizzs.in

RECURSION IN C:-

A function that calls itself, and doen't perform any task after function call, is know as tail recursion. In tail recursion, we generally call the same function with return statement.

Syntax:-

recursionfunction(){

recursionfunction();//calling self function

}

infobizzs.in

ARRAY IN C:-

Array in C language is a collection or group of elements (data). All the elements of array are homogeneous(similar). It has contiguous memory location.

Declaration of array:-

data_type array_name[array_size];

Eg:-

int marks[7];

• Types of array:-

1) 1-D Array

2) 2-D Array

infobizzs.in

ADVANTAGE OF ARRAY:-

1) Code Optimization

2) Easy to traverse data

3) Easy to sort data

4) Random Access

infobizzs.in

2-D Array in C:-

2-d Array is represented in the form of rows and columns, also known as matrix. It is also known as array of arrays or list of arrays.

Declaration of 2-d array:-data_type array_name[size1][size2];

infobizzs.in

Initialization of 2-d array:-

int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};

infobizzs.in