160 likes | 180 Views
Learn how to use for and do/while loops in C programming, compare them to while loops, see examples, and master logical operators. Explore the detailed structure and usage of each loop type.
E N D
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 */ while (counter <= 10) { /* repetition condition */ printf(“%d\n”, counter); counter++; /* increment */ } return 0; }
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; }
For loop General format of the for structure: for (expression1; expression2; expression3) statement Equivalent while loop: expression1; while (expression2) { statement expression3; }
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( ; ;)
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)
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 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 Generally: do { statement } while (condition); In C: #include <stdio.h> main() { int counter=1; do { printf(“%d\n”, counter); } while (++counter <= 10); return 0; }
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 conditions • 3 kinds • && (logical AND) • || (logical OR) • ! (logical NOT or logical negation)
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 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 • ! 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 ==