1 / 16

Program Control

Program Control. Dilshad M. Shahid New York University @1998. Today. For loop Do/while loop Comparison of both loops with while loop Examples Logical operators. For loop. Remember counter controlled while loop? #include <stdio.h> main() { int counter = 1; /* initialization */

hughclark
Download Presentation

Program Control

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. Program Control Dilshad M. Shahid New York University @1998

  2. Today • For loop • Do/while loop • Comparison of both loops with while loop • Examples • Logical operators

  3. For loop Remember counter controlled while loop? #include <stdio.h> main() { int counter = 1; /* initialization */ while (counter <= 10) { /* repetition condition */ printf(“%d\n”, counter); counter++; /* increment */ } return 0; }

  4. For loop For repetition structure (or for loop) handles all details of counter-controlled repetition automatically #include <stdio.h> main() { int counter; /*initialization, repetition condition, and increment */ for(counter=1; counter <= 10; counter++) printf(“%d\n”, counter); return 0; }

  5. For loop General format of the for structure: for (expression1; expression2; expression3) statement Equivalent while loop: expression1; while (expression2) { statement expression3; }

  6. For loop • The 3 expressions are optional. • Omitting expression2 - C will assume condition is always true, thus creating an infinite loop • Can initialize expression1 elsewhere • expression3 (increment) can be placed in body of for loop • however, the 2 ; are required • for( ; ;)

  7. For loop Other things a for loop will be skipped if the loop-continuation condition is initially false for loop header can contain arithmetic expressions for(j=x; j <=4*x*y; j+=y/x) /* assume x=2, y=10 */ Increment can be negative (decrement) for(sum=100; sum >= 1; sum--) Increment can be by more than 1 for(j=7; j <=77; j+=3)

  8. For loop • Generally, when should you use a for loop vs. a while loop? • If number of repetitions is known, use a for loop • Otherwise use a while loop

  9. Do/while loop • Similar to the while loop • In while loop, the loop-continuation condition is tested at the beginning of the loop • In do/while, the loop-continuation condition is tested after loop body is executed

  10. Do/while loop For while loop: while(condition) For do/while loop: do statement while(condition)

  11. Do/while loop Generally: do { statement } while (condition); In C: #include <stdio.h> main() { int counter=1; do { printf(“%d\n”, counter); } while (++counter <= 10); return 0; }

  12. Loops • A for loop and a while loop may be executed zero or more times • a do/while loop is executed at least once • for loop is counter controlled • while and do/while loops are condition controlled

  13. Logical operators • Can be used to form more complex conditions by combining simple conditions • 3 kinds • && (logical AND) • || (logical OR) • ! (logical NOT or logical negation)

  14. Logical operators • Examples • /* logical AND */ • if (withdrawal >= 100 && balance < 5000) • printf(“Sorry, you cannot withdraw money”); • /* logical OR */ • if (average >= || final >= 90) • printf(“you get an A”); • /* logical negation */ • if (!(grade == 100)) • printf(“you don’t qualify for an A+”);

  15. Logical operators • Simple conditions (e.g. >= , ==) are evaluated first because their precedence is higher than && • && has a higher precedence than || and both associate left to right • && and || are binary operators, combining 2 conditions • expression containing && or || is evaluated only until truth or falsehood is known, e.g. • gender ==1 && age >=65 • will stop if gender is not equal to 1

  16. Logical operators • Logical negation ! allows programmer to reverse meaning of a condition • ! has a single condition as an operand and is a unary operator • ! is placed before a condition • if (!(grade == sentinelValue)) • printf(“The next grade is %f\n”, grade); • parentheses are need because logical negation has higher precedence than ==

More Related