1 / 15

Structured Program Development Asim Iqbal

Structured Program Development Asim Iqbal. Outline Introduction Algorithms Pseudocode Control Structures The If Selection Statement The If…Else Selection Statement. Decision Making: Equality and Relational Operators. if control statement

koren
Download Presentation

Structured Program Development Asim Iqbal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Program DevelopmentAsimIqbal Outline Introduction Algorithms Pseudocode Control Structures The If Selection Statement The If…Else Selection Statement

  2. Decision Making: Equality and Relational Operators • if control statement • If a condition is true, then the body of the if statement executed • 0 is false, non-zero is true • Control always resumes after the if structure • Keywords • Special words reserved for C • Cannot be used as identifiers or variable names

  3. Equality and Relational Operators Example //90 - 100 int grade; printf("Please enter your grade => "); scanf("%d",&grade); if(grade>=90){ printf("You got A grade = %d \n ",grade); } if(grade<90){ printf("Better luck next time = %d\n ",grade); }

  4. Decision Making: Equality and Relational Operators

  5. Using if statements, relational operators, and equality operators /* Using if statements, relational operators, and equality operators */ #include <stdio.h> /* function main begins program execution */ main() { int num1; /* first number to be read from user */ int num2; /* second number to be read from user */ printf( "Enter two integers, and I will tell you\n" ); printf( "the relationships they satisfy: " ); scanf( "%d%d", &num1, &num2 ); /* read two integers */ if ( num1 == num2 ) { printf( "%d is equal to %d\n", num1, num2 ); } /* end if */ if ( num1 != num2 ) { printf( "%d is not equal to %d\n", num1, num2 ); } /* end if */ system("pause"); return 0; /* indicate that program ended successfully */ } /* end function main */ Output Enter two integers, and I will tell you the relationships they satisfy: 3 73 is not equal to 7

  6. Precedence and associativity of the operators

  7. C’s reserved keywords

  8. Introduction • Before writing a program: • Have a thorough understanding of the problem • Carefully plan an approach for solving it • While writing a program: • Know what “building blocks” are available • Use good programming principles

  9. Algorithms • Computing problems • All can be solved by executing a series of actions in a specific order • Algorithm: procedure in terms of • Actions to be executed • The order in which these actions are to be executed • Program control • Specify order in which statements are to be executed

  10. Pseudocode • Pseudocode • Artificial, informal language that helps us develop algorithms • Similar to everyday English • Not actually executed on computers • Helps us “think out” a program before writing it • Easy to convert into a corresponding C program • Consists only of executable statements

  11. Control Structures • Flowcharting C’s sequence structure.

  12. The if Selection Statement • Selection structure: • Used to choose among alternative courses of action • Pseudocode: If student’s grade is greater than or equal to 60Print “Passed” • If condition true • Print statement executed and program goes on to next statement • If false, print statement is ignored and the program goes onto the next statement • Indenting makes programs easier to read • C ignores whitespace characters

  13. The ifSelection Statement… • Pseudocode statement in C: if ( grade >= 60 ) printf( "Passed\n" ); • C code corresponds closely to the pseudocode

  14. The if…else Selection Statement • if • Only performs an action if the condition is true • if…else • Specifies an action to be performed both when the condition is true and when it is false • Psuedocode: If student’s grade is greater than or equal to 60Print “Passed” elsePrint “Failed” • Note spacing/indentation conventions

  15. The if…else Selection Statement • C code: if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n");

More Related