control structure by shuja ahmad

12

Upload: inocentshuja-ahmad

Post on 21-Mar-2017

377 views

Category:

Education


2 download

TRANSCRIPT

Page 1: control structure by shuja ahmad
Page 2: control structure by shuja ahmad

NAME SHUJA AHMADROLL NO 1422

Page 3: control structure by shuja ahmad

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.

Page 4: control structure by shuja ahmad

Types of control structure

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

Page 5: control structure by shuja ahmad

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.

Page 6: control structure by shuja ahmad

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.

Page 7: control structure by shuja ahmad

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 .

Page 8: control structure by shuja ahmad

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 .

Page 9: control structure by shuja ahmad

Flow charts

Page 10: control structure by shuja ahmad

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);

}

Page 11: control structure by shuja ahmad

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;

}

}

Page 12: control structure by shuja ahmad

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);}