control structure by shuja ahmad

Post on 21-Mar-2017

377 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

NAME SHUJA AHMADROLL NO 1422

Control structure

• A statement used to control the flow of execution in a program is called control structure.• The control structure in are used to combine

individual instruction into a single logical unit.• The logic unit has one entry point and one exit

point.

Types of control structure

• Sequential structure • Selection structure • Repetition structure• Function call

Sequential structure

• In sequential structure , the statement are executed in the same order in which they are specified in program. • The control flows from one statement to other in a logical sequence.• All statements are executed exactly once.

Selection structure

• A selection structure selects a statement or set of statement to execute on the basic of a condition.• In this structure ,statement or set of statement is

execute when a particular condition is true and ignored when the condition is false • There are different types of selection

structure .These are if,if-else and switch.

Repetition structure

• A repetition structure execute a statement or set of statements is repeatedly• It is also known as iteration structure or loop.• The repetition structure include for loop,while

loop and do-while loop .

Function call

• Function call type of statement that moves the control to another block of code.• The control return back back after executing all

statement in the block.• The remaining statements are executed

immediately after the function call when the control is returned .

Flow charts

Write a program that inputs two numbers from user and display the result of first number raise to the

power of second number using do-while.#include<stdio.h>

void main()

{

int a,b,c,r;

printf("Enter first number:");

scanf("%d",&a);

printf("Enter second number:");

scanf("%d",&b);

c=1;

r=1;

do

{

r=r*a;

c=c+1;

}

while(c<=b);

printf("Result is %d",r);

}

Write a program that produce following output?

• 0 1• 1 2• 2 4 • 3 8• 4 16• 5 32• 6 64

#include<stdio.h>

void main()

{

int I,j;

i=0; j=1;

while(i<=6)

{

Printf(“%d %d\n”,I,j);

i=i+1; j=j+2;

}

}

Write a program that inputs a number from the user and display the factorial of that number using do-while

loop?

#include<stdio.h>void main(){Int n,c,f;c=1;f=1;printf(Enter a number :”);scanf(“%d”,&n);

do { f=f*c; c=c+1; }While(c<=n);Printf(“factorial of %d is %d”,n,f);}

top related