Program Control Dilshad M Shahid New York University

  • Slides: 16
Download presentation
Program Control Dilshad M. Shahid New York University @1998

Program Control Dilshad M. Shahid New York University @1998

Today • For loop • Do/while loop • Comparison of both loops with while

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

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

For loop For repetition structure (or for loop) handles all details of counter-controlled repetition

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(“%dn”, counter); return 0; }

For loop General format of the for structure: for (expression 1; expression 2; expression

For loop General format of the for structure: for (expression 1; expression 2; expression 3) statement Equivalent while loop: expression 1; while (expression 2) { statement expression 3; }

For loop • The 3 expressions are optional. • Omitting expression 2 - C

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

For loop Other things a for loop will be skipped if the loopcontinuation condition

For loop Other things a for loop will be skipped if the loopcontinuation 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)

For loop • Generally, when should you use a for loop vs. a while

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

Do/while loop • Similar to the while loop • In while loop, the loop-continuation

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

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

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

Do/while loop Generally: do { statement } while (condition); In C: #include <stdio. h>

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

Loops • A for loop and a while loop may be executed zero or

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

Logical operators • Can be used to form more complex conditions by combining simple

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)

Logical operators • Examples /* logical AND */ if (withdrawal >= 100 && balance

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+”);

Logical operators • Simple conditions (e. g. >= , ==) are evaluated first because

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

Logical operators • Logical negation ! allows programmer to reverse meaning of a condition

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