Structured Program Development Dilshad M. Shahid New York University @1998
Today If/else, if/else, if/else While repetition structure Casting Assignment Operators (Shortcuts)
Sidetrack Conditional operator (?:) Only ternary operator (takes 3 operands) in C Example printf(“%s\n”, grade >= 60 ? “Passed” : “Failed”);
If/else, if/else, if/else Nested if/else structures test for multiple cases by placing if/else structures inside if/else structures Example
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”
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”);
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 63-64 for another good if/else, if/else example.
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”); }
Repetition structure Allows programmer to specify that an action is to be repeated while some condition remains true The While Structure (or Loop)
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
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
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”);
While loop Counter controlled repetition Can use this to control how many times the while loop is executed example in C
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”);
While loop Sentinel controlled loop Can use this to determine when to stop executing the while loop example in C in Borland
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
Assignment Operators Used to abbreviate assignment expressions Example: c = c + 3 can be written as c =+3 See pg 77
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
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”); }