Transcript While

Introduction to Computers and
Programming
Class 9
Introduction to C
Professor Avi Rosenfeld
Loop Structures
• Event Controlled
– While Loops
– Do While Loops
• Counter Controlled
– Counter while loops
– For loops (next time)
Parts of a while Loop
•
Every while loop will always contain three
main elements:
– Priming: initialize your variables
– Testing: test against some known condition
– Updating: update the variable that is tested
3
Infinite Loop
• Infinite Loop: A loop that never ends
– Generally, you want to avoid these!
– There are special cases, however, when you do
want to create infinite loops on purpose
• Common Exam Questions:
– Given a piece of code, identify the bug in the
code
– You may need to identify infinite loops
4
Infinite Loop Example #1
#include <stdio.h>
Index:
1
Index:
1
Index:
1
main ()
Index: 1
{
Index: 1
int index =1;
… [forever]
while (index <= 10)
{
printf ("Index: %d\n", index);
}
Here, I have deleted part 3: the index = index + 1 statement.
}
5
#include <stdio.h>
void main()
{
int number,EvenNumbers = 0, OddNumbers = 0;
printf("\nEnter a number between 0 & 9 inclusive (999 to end program): ");
scanf("%d", &number); /* why is this scanf needed? */
while (number != 999)
{
switch (number)
{
case 0: case 2: case 4: case 6: case 8:
EvenNumbers++; /*what is the ++ ? */
printf("You entered a valid EVEN #");
break;
case 1: case 3: case 5: case 7: case 9:
OddNumbers++;
printf("You entered a valid ODD #");
break;
default:
printf("\n*** You did not enter a # between 0 & 9");
break;
} /* end of switch */
printf("\nEnter a number between 0 & 9 inclusive (999 to end program): ");
scanf("%d", &number);
} /* end of while loop */
printf("\n\nYou entered a total of %d EVEN #s and %d ODD #s\n", EvenNumbers, OddNumbers);
}
#include <stdio.h>
void main()
{
int number,EvenNumbers = 0, OddNumbers = 0;
do
{
printf("\nEnter a number between 0 & 9 inclusive (999 to end program): ");
scanf("%d", &number);
switch (number)
{
case 0: case 2: case 4: case 6: case 8:
EvenNumbers++;
printf("You entered a valid EVEN #");
break;
case 1: case 3: case 5: case 7: case 9:
OddNumbers++;
printf("You entered a valid ODD #");
break;
default:
printf("\n*** You did not enter a # between 0 & 9");
break;
} /* end of switch */
}while (number != 999); /* end of while loop */
printf("\n\nYou entered a total of %d EVEN #s and %d ODD #s\n", EvenNumbers, OddNumbers);
}
Shortcuts
• C provides abbreviations for some common
operations
• Assignment operators
• Increment/Decrement operators
Assignment Operators
• Abbreviations are provided for the basic
binary operations
–
–
–
–
–
Addition
Subtraction
Multiplication
Division
Modulus (%)
Shortcut Heaven (or Hell?)
#include <stdio.h>
void main()
{
int x = 2, y = 3;
x *= 3; /* same as x = x * 3; x is 6 */
printf("X is now %d\n", x);
y /= 2; /* same as y = y / 2; y is 1 */
printf("Y is now %d\n", y);
y += 2; /* same as y = y + 2; y is 3 */
printf("Y is now %d\n", y);
x -= 2; /* same as x = x - 2; x is 4 */
printf("X is now %d\n", x);
x %= y; /* same as x = x % y; x is 1 */
printf("X is now %d\n", x);
}
Samples
Assume variable int c = 10
Initial
Operator Value
Sample
Meaning
Assigns
+=
10
c += 7;
c = c + 7;
17 to c
-=
10
c -= 3;
c = c – 3;
7 to c
*=
10
c *= 6;
c = c * 6;
60 to c
/=
10
c /= 5;
c = c / 5;
2 to c
%=
10
c %= 6;
c = c % 7;
3 to c
Increment and Decrement
Operators
• C provides unary increment operators ++
and decrement operators –- (no spaces
between them)
• Increment operators add 1
• Decrement operators subtract 1
• Not for other assignment operators (*, /, %)
• Guess where C++ comes from???
Postincrement
• The ++ operator is after the variable
• Causes the initial value of the variable to be used
in the expression where it appears AND THEN
adds the 1 to the variable
• For example,
int iCount = 5;
printf( “%d\n”, iCount++ );
Would print 5… but iCount is worth 6 after the
statement
Preincrement
• The ++ operator is before the variable
• Adds 1 to the initial value of the variable
BEFORE it is used in the expression where it
appears
• For example,
int iCount = 5;
printf( “%d\n”, ++iCount);
Would print 6… and iCount is worth 6 after the
statement
More examples ++
postincrement vs. preincrement
int iTotal = 0;
int iCount = 5;
iTotal = iCount++ * 2;
printf( “%d\n”, iTotal);
printf( “%d\n”, iCount);
Would print 10 for iTotal and then 6 for iCount
iTotal = ++iCount * 2;
printf( “%d\n”, iTotal);
printf( “%d\n”, iCount);
Would print 12 for iTotal and then 6 for iCount
Decrement Operator -• Similar to increment in syntax/operation
• Instead of writing
iCount = iCount - 1; or
iCount -= 1;
• You can write
iCount-- ; or --iCount ;
• Subtle difference between the two new options
call postdecrement and predecrement
Postdecrement
• The -- operator is after the variable
• Causes the initial value of the variable to be used
in the expression where it appears AND THEN
subtracts the 1 from the variable
• For example,
int iCount = 5;
printf( “%d\n”, iCount-- );
Would print 5… but iCount is worth 4 after the
statement
Predecrement
• The -- operator is before the variable
• Subtracts 1 from the initial value of the variable
BEFORE it is used in the expression where it
appears
• For example,
int iCount = 5;
printf( “%d\n”, --iCount);
Would print 4… and iCount is worth 4 after the
statement
For our last trick…
#include <stdio.h>
void main()
{
int x = 2, y = 3;
printf("What is this number %d\n", x++ + ++y);
printf("But x is still %d and y is %d\n", x, y);
printf(“What # %d ???\n", y / x-- * ++x % y);
/* 0 */
printf("But x is still %d and y is %d\n", x, y);
}
Summary Table
Operator
Sample
Explanation
++
c++
++
++c
--
c--
--
--c
Use value of c in expression
THEN add 1 to c
Add 1 to c THEN use the
new value of c in expression
Use value of c in expression
THEN subtract 1 from c
Subtract 1 from c THEN use
new value of c in expression
Introducing for loops
#include <stdio.h>
void main()
{
int counter = 100;
while (counter--> 0)
printf("The counter is at %d ", counter);
}