CONSTRUCTS (IMPERATIVE PROGRAMMING LANGUAGE) Selection Constructs Conditional Constructs Looping Constructs By: Jyoti Bhardwaj Roll no: 04 F.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: 5 I 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( "Excellentn" ); break; case 'B' : printf( “Very Goodn" ); 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
CONDITIONALCONSTRUCTS.  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; }
LOOPINGCONSTRUCTS: 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 LoopType 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("%dn",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 %dn",i); i++; } while(i<=4 && i>=2); } Output: Value of i is 1 Value of i is 2 Value of i is 3 Value 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
WhentoUseWhichLoop? Ifyouknow(orcancalculate) howmanyiterationsyouneed, thenuseacounter-controlled(for )loop. Otherwise,ifitisimportantthat theloopcompleteatleastonce beforecheckingforthestopping condition, orifitisnotpossibleor meaningfultocheckthestopping conditionbeforetheloophas executedatleastonce, thenuseado-whileloop. Otherwiseuseawhileloop.
Constructs (Programming Methodology)

Constructs (Programming Methodology)

  • 1.
    CONSTRUCTS (IMPERATIVE PROGRAMMING LANGUAGE) Selection Constructs ConditionalConstructs Looping Constructs By: Jyoti Bhardwaj Roll no: 04 F.Y. M.C.A
  • 2.
    SELECTION CONSTRUCTS. THE SELECTIONCONSTRUCT: 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
  • 3.
    if... else example main() { inti; 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: 5 I is not greater than seven
  • 4.
    CASE STATEMENT:  Switchcase 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.
  • 5.
    The basic formatfor 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 }
  • 6.
    Selection structure varyfrom 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.
  • 7.
    Switch… case example: main() { intGrade = 'B'; switch(Grade ) { case 'A' : printf( "Excellentn" ); break; case 'B' : printf( “Very Goodn" ); 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
  • 8.
    CONDITIONALCONSTRUCTS.  The conditionalconstruct 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.
  • 9.
    If…else Ladder: if (expression) { Blockof statements; } else { Block of statements; } if (expression) { Block of statements; } else if(expression) { Block of statements; } else { Block of statements; }
  • 10.
  • 11.
    if... else example intmain() { 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; }
  • 12.
    LOOPINGCONSTRUCTS: The loop constructsis 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
  • 13.
    Types of loopto handle looping requirements LoopType 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.
  • 14.
    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; }
  • 15.
    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("%dn",i); i++; } } Output: 3 4 5 6 7 8 9
  • 16.
    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 %dn",i); i++; } while(i<=4 && i>=2); } Output: Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4
  • 17.
    For loop example. Infor 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
  • 18.