Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Program Development

Similar presentations


Presentation on theme: "Structured Program Development"— Presentation transcript:

1 Structured Program Development
Dilshad M. Shahid New York University @1998

2 Today If/else, if/else, if/else While repetition structure Casting
Assignment Operators (Shortcuts)

3 Sidetrack Conditional operator (?:)
Only ternary operator (takes 3 operands) in C Example printf(“%s\n”, grade >= 60 ? “Passed” : “Failed”);

4 If/else, if/else, if/else
Nested if/else structures test for multiple cases by placing if/else structures inside if/else structures Example

5 If/else, if/else, if/else
Pseudocode If customer buys more than 20 oranges Print “the price is 40 cents each” else if customer buys more than 10 oranges Print “the price is 50 cents each” Print “the price is 75 cents each”

6 If/else, if/else, if/else
In C: int num_orange; printf(“How many oranges would you like?\n”); scanf(“%d”, &num_orange); if (num_orange > 20) printf(“The price is 40 cents each\n”); else if (num_orange > 10) printf(“The price is 50 cents each\n”); printf(“The price is 75 cents each\n”);

7 If/else, if/else, if/else
In C, indentations are not necessary. Can also write: if (num_orange > 20) printf(“The price is 40 cents each\n”); else if (num_orange > 10) printf(“The price is 50 cents each\n”); else printf(“The price is 75 cents each\n”); Look at pg for another good if/else, if/else example.

8 If/else, if/else, if/else
Compound statement - a set of several statements contained within a pair of curly brackets (braces) To include a compound statement in the body of an if, you would write, for example: if (num_orange > 20) printf(“The price is 40 cents each\n”); else { printf(“The price is 75 cents each\n”); printf(“Not a great deal if you ask me\n”); }

9 Repetition structure Allows programmer to specify that an action is to be repeated while some condition remains true The While Structure (or Loop)

10 While loop Tests for a given condition at the beginning of the loop before executing the other statements in the loop If condition is false, the statements in the loop are skipped Will continue execution until condition becomes false

11 Example In pseudocode: While sum is less than 20 Add 2 to sum
While loop Example In pseudocode: While sum is less than 20 Add 2 to sum

12 While loop Example In C: int sum; sum = 15; /*initialization step */
while(sum < 20){ /* your condition here */ sum = sum + 2; /* changing the value */ printf(“Not there yet\n”); } printf(“You made it\n”);

13 While loop Counter controlled repetition
Can use this to control how many times the while loop is executed example in C

14 While loop #include <stdio.h> main() {
int counter, grade, total, average; total = 0; /* initialization phase */ counter = 1; while (counter <= 5) { /* processing phase */ printf(“Enter grade: “); scanf(“%d”, &grade); total = total + grade; counter = counter + 1; } average = total/5; printf(“Class average is %d\n”, average”);

15 While loop Sentinel controlled loop
Can use this to determine when to stop executing the while loop example in C in Borland

16 Casting Dividing one integer by another integer can sometimes result in the loss of the fractional part C provides the unary cast operator to create temporary values that will hold the data Cast operators are available for any type

17 Assignment Operators Used to abbreviate assignment expressions
Example: c = c + 3 can be written as c =+3 See pg 77

18 Assignment Operators Increment and Decrement Operators
Example: c = c + 1 can be written as c ++ Table on pg 80 for all variations Table on pg 82 for precedence of operators

19 While loop Example In C: int sum; sum = 15; /*initialization step */
while(sum < 20){ /* your condition here */ sum = sum + 2; /* changing the value */ printf(“Not there yet\n”); }


Download ppt "Structured Program Development"

Similar presentations


Ads by Google