constructs (programming methodology)

Post on 11-Apr-2017

162 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CONSTRUCTS (IMPERATIVE PROGRAMMING LANGUAGE)

Selection ConstructsConditional ConstructsLooping Constructs

By: Jyoti BhardwajRoll no: 04F.Y. M.C.A

SELECTION CONSTRUCTS.THE SELECTION CONSTRUCT:

The selection construct allows you to create a program that will select one of a number of different alternative courses of action. It is also called the if..then..else construct.

A selection statement provides for selection between alternatives. We can identify two types of selection constructs:

If statements Case statements

IF STATEMENT: An if statement, sometimes referred to as a conditional, can be used in two forms: If condition then action1

if (condition1) action1; if (condition2) action2;

.

.

.if (conditionN) actionN;

If condition then action1 else action2

if... else example 

main() { 

int i; printf("\nEnter a number : "); scanf("%d",&i);

if( i > 7 ) printf("I is greater than seven");

else printf("I is not greater than seven");

}

Output: Enter a number: 5I is not greater than seven

CASE STATEMENT: Switch case statements are a substitute for long if statements that compare a

variable to several values. Case statements allow selection from many alternatives.

The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.

Using break keyword: If a condition is met in switch case then execution continues on

into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.

What is default condition: If none of the listed conditions is met then default condition

executed.

The basic format for using switch case is outlined below:

switch(expression) {

    case 1:        code block        break; case 2:        code block        break; ..    case n:        code block        break;    default:        default code block

}

Selection structure vary from language to language, but they tend to agree on the following:

Case Selection can appear in any order. Case Selections do not have to be consecutive, meaning that it

is valid to have case-1, and case-4 without case-2 and case-3. Case Selection must have distinct actions for each case,

otherwise we need to combine the actions to avoid conflict.

To Sum up, the effect of Switch structure is as follows: Evaluate the switch expression. Go to the case label having a constant value that matches the

value of the expression found in step 1; If a match is found, go to the default label; if there is no default label; terminate the switch.

Terminate the switch when break statement in encountered.

Switch… case example:main() {

int Grade = 'B'; switch( Grade ) { case 'A' : printf( "Excellent\n" ); break; case 'B' : printf( “Very Good\n" ); break; case 'C' : printf( "Good \n" ); break; case 'D' : printf( “O.K. \n" ); break; case 'F' : printf( “Satisfactory \n" ); break; default : printf( “Fair \n" ); break; }

}

This will produce following result:Good

CONDITIONAL CONSTRUCTS. The conditional construct allows you to create a program

that will select one of a number of different alternative courses of action. It is also called the if..then..else construct.

The initial if statement checks a condition (in this case >= 50). If the condition is true, then the series of commands following it are executed. If the condition is false (i.e. the user enters a mark less than 50), then the series of commands following the else statement are executed.

Note: An if statement must check a condition and then be followed

by the word then. An if statement must have an end if to finish it off. An if statement does not have to have an else statement.

If…else Ladder:

if (expression) {

Block of statements; } else {

Block of statements; }

if (expression) {

Block of statements; } else if(expression) {

Block of statements; } else {

Block of statements; }

If…else Ladder:

if... else example int main(){ int year;

printf("Enter a year: "); scanf("%d",&year); if(year%4 == 0) {

if( year%100 == 0) {

if ( year%400 == 0) printf("%d is a leap year.", year);

else printf("%d is not a leap year.", year);

} else

printf("%d is a leap year.", year ); } else

printf("%d is not a leap year.", year); return 0; }

LOOPING CONSTRUCTS:The loop constructs

is used to execute a block of code until the condition becomes expired.

Loop constructs saves the programmer from writing code multiple times that has repetitive in nature

Types of loop to handle looping requirements

Loop Type Description while loop Repeats a statement or group of

statements while a given condition is true. It tests the condition before executing the loop body.

for loop Execute a sequence of statements multiple times.

do...while loop Like a while statement, except that it tests the condition at the end of the loop body

nested loops You can use one or more loop inside any another while, for or do..while loop.

The Infinite Loop:

A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.

int main () { for( ; ; ) { printf("This loop will run forever.\n"); } return 0; }

While loop example.. In while loop control statement, loop is executed until condition becomes false. Syntax:

while( condition )

body;

int main()

{

int i=3;

while(i<10)

{

printf("%d\n",i); i++;

}

}

Output:

3 4 5 6 7 8 9

do...while loop examples  In do..while loop control statement, while loop is executed irrespective of

the condition for first time. Then 2nd time onwards, loop is executed until condition becomes false.

int main() {

int i=1; do {

printf("Value of i is %d\n",i); i++;

}while(i<=4 && i>=2);

}Output:

Value of i is 1Value of i is 2Value of i is 3Value of i is 4

For loop example.   In for loop control statement, loop is executed until condition

becomes false.

int main() { int i; for(i=0;i<10;i++) { printf("%d ",i); } }

Output:0 1 2 3 4 5 6 7 8 9

When to Use Which Loop ?

If you know ( or can calculate ) how many iterations you need, then use a counter-controlled ( for ) loop.Otherwise, if it is important that the loop complete at least once before checking for the stopping condition, or if it is not possible or meaningful to check the stopping condition before the loop has executed at least once,then use a do-while loop.Otherwise use a while loop.

top related