Lecture 10: Reviews Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default Selection structures Used.

Download Report

Transcript Lecture 10: Reviews Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default Selection structures Used.

Lecture 10: Reviews
Control Structures
All C programs written in term of 3 control structures
Sequence structures
Programs executed sequentially by default
Selection structures
Used to choose among alternative courses of
action
Three types: if, if … else, and switch
Repetition structures
Used to repeat an action when some condition
remains true.
Three types: while, do … while and for
Sequence Structures
Normally, statements are executed one after the other
in the order written
Selection Structures - if
if statement
if (condition) {
statement(s);
}
Only performs an action if the condition is true.
Example:
if ( grade >= 60 ) {
printf( “Passed\n” );
}
Flowchart
if ( grade >= 60 ) {
printf( “Passed\n” );
printf( “Congratulations!\n” );
}
Selection Structures - if…else
If…else statement
if (condition) {
statement(s);
}
else {
statement(s);
}
Specifies an action to be performed both when the
condition is true and when it is false.
Flowchart
Selection Structures - if…else
Nested if…else statements
Test for multiple cases by placing if…else selection
statements inside if…else selection statement
Once condition is met, rest of statements skipped
if ( score >= 90 ) {
printf( "The grade is 'A'.\n" );
}
else if ( score >= 80 ) {
printf( "The grade is 'B'.\n" );
}
else if ( score >= 70 ) {
printf( "The grade is 'C'.\n" );
}
else if ( score >= 60 ) {
printf( "The grade is 'D'.\n" );
}
else {
printf( "The grade is 'F'.\n" );
}
>= 90:
80 ~ 89:
70 ~ 79:
60 ~ 69:
< 60:
A
B
C
D
F
Selection Structures - if…else
if…else statements in sequence
if ( score >= 90 ) {
printf( "The grade is 'A'.\n" );
}
else if ( score >= 80 ) {
printf( "The grade is 'B'.\n" );
}
if ( score >= 70 ) {
printf( "The grade is 'C'.\n" );
}
else if ( score >= 60 ) {
printf( "The grade is 'D'.\n" );
}
else {
printf( "The grade is 'F'.\n" );
}
Selection Structures - switch
switch statement
Useful when a variable or expression is tested for all the values
it can assume and different actions are taken.
Format
Series of case labels and an optional default case
switch ( controlling expression ) {
case value1:
actions
case value2:
actions
default:
default case occurs if none of the cases are matched
actions
}
The switch statement can be used only for testing a constant
integral expression.
switch (grade) {
case >= 90: …
case >= 80: …
…..
}
Selection Structures - switch
switch statement with break statements
switch ( controlling expression ) {
case value1:
actions;
break;
case value2:
actions;
break;
default:
actions;
break;
}
It causes program control to continue with the first statement
after the switch statement.
Selection Structures - switch
switch statement with break statements
Example
switch ( n ) {
case 1:
printf( “The number is 1\n”);
break;
case 2:
printf( “The number is 2\n”);
break;
case 3:
printf( “The number is 3\n”);
break;
default:
printf( “The number is not 1, 2, or 3\n”);
break;
}
Selection Structures - switch
switch statement with break statements
Example
switch ( n ) {
case 1:
printf( “The number is 1\n”);
case 2:
printf( “The number is 2\n”);
break;
case 3:
printf( “The number is 3\n”);
break;
default:
printf( “The number is not 1, 2, or 3\n”);
break;
}
Repetition Structures
Two types of repetition structures
Counter-controlled repetition
Definite repetition: know how many times loop
will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
Used when number of repetitions not known
Sentinel value indicates "end of data entry”
Loop ends when user inputs the sentinel value
Repetition Structures
Common error:
Infinite loops are caused when the loop-continuation
condition in a while, for or do...while statement never
becomes false. To prevent:
a) Make sure there is not a semicolon immediately
after the header of a while statement.
b) In a counter-controlled loop, make sure the control
variable is incremented (or decremented) in the loop.
c) In a sentinel-controlled loop, make sure the sentinel
value is eventually input.
j = 1;
while (j <= 10); {
printf(“%d\n”, j);
j++;
}
j = 1;
while (j <= 10) {
printf(“%d\n”, j);
}
j = 1;
while (j <= 10) {
printf(“%d\n”, j);
j--;
}
j = 1;
while (j <= 10) {
printf(“%d\n”, --j);
j++;
}
Repetition Structures - while
while loop repeated until condition becomes false
Counter-controlled repetition
Sentinel-controlled repetition
Format
while ( condition ) {
statement(s);
}
Flowchart
condition
while body
Repetition Structures - while
Counter-controlled while loop
Loop repeated until counter reaches a certain value
Count the number of iterations
j = 1;
while (j <= 10) {
printf(“Good Luck\n”);
j++;
}
j = 0;
while (j < 10) {
printf(“Good Luck\n”);
j++;
}
j = 1;
while (j <= 10) {
printf(“Good Luck\n”);
j += 2;
}
j = -1;
while (j <= 10) {
printf(“Good Luck\n”);
j += 3;
}
j = 0;
while (j <= 10) {
printf(“Good Luck\n”);
j++;
}
Repetition Structures - while
Sentinel-controlled while loop
Loop repeated until user inputs the sentinel value
Sentinel value chosen so it cannot be confused with a
regular input
Examples
Simple calculator
ATM menu interface
Prime number
Repetition Structures - do…while
Similar to the while statement
Condition for repetition only tested after the body of
the loop is performed.
All actions are performed at least once.
Format
do {
statement(s);
} while ( condition );
Flowcharting the do…while repetition statement
Repetition Structures - do…while
Examples
counter = 1;
do {
printf( “%d\n”, counter);
counter ++;
} while (counter <= 10);
counter = 1;
while (counter <= 10) {
printf( “%d\n”, counter);
counter ++;
}
counter = 12;
do {
printf( “%d\n”, counter);
counter ++;
} while (counter <= 10);
counter = 12;
while (counter <= 10) {
printf( “%d\n”, counter);
counter ++;
}
Repetition Structures - for
Can loop a known number of times
Format when using for loops
for ( initialization; loopContinuationTest; increment ) {
statement(s);
}
initialization;
while ( loopContinuationTest ) {
statement(s);
increment;
}
for (j = 1; j <= 10; j++) {
printf(“%d\n”, j);
}
increment
for (j = 10; j > 0; j--) {
printf(“%d\n”, j);
}
decrement
for (j = 10; j > 0; j -= 2) {
printf(“%d\n”, j);
}
Increasing/decreasing
other than 1
Nested Control Loops
j
Nested while loops
i = 1;
while (i < 5) {
j = 1;
while (j < 5) {
printf(“%d * %d = %d\n”, i, j, i*j);
j++;
}
i++;
}
Nested for loops
for (i = 1; i < 5; i++) {
for (j = 1; j < 5; j++) {
printf(“%d * %d = %d\n”, i, j, i*j);
}
}
i
*
1
2
3
4
1
1
2
3
4
2
2
4
6
8
3
3
6
9 12
4
4
8 12 16
Nested Control Loops
j
Nested while loops
i = 1;
while (i < 5) {
j = 1;
while ( j <= i ) {
printf(“%d * %d = %d\n”, i, j, i*j);
j++;
}
i++;
}
Nested for loops
for (i = 1; i < 5; i++) {
for (j = 1; j <= i;i j++) {
printf(“%d * %d = %d\n”, i, j, i*j);
}
}
i
*
1
2
3
4
1
1
2
3
4
2
2
4
6
8
3
3
6
9 12
4
4
8 12 16
Nested Control Loops
j
Nested while loops
i = 1;
while (i < 5) {
j = 1;
while ( j < 5 ) {
printf(“%d * %d = %d\n”, i, j, i*j);
j++;
}
i += 2;
}
Nested for loops
for (i = 1; i < 5; i += 2) {
for (j = 1; j < 5; j++) {
printf(“%d * %d = %d\n”, i, j, i*j);
}
}
i
*
1
2
3
4
1
1
2
3
4
2
2
4
6
8
3
3
6
9 12
4
4
8 12 16
Operators
Arithmetic operators
+, -, *, /, %
Assignment operator
=
Arithmetic assignment operators
+=, -=, *=, /=, %=
Increment / decrement operators
++, -Relational operators
<, <=, >, >=
Equality operators
==, !=
Logical operators
&&, ||, !
== and =
Swapping both operators does not ordinarily cause
syntax errors.
int payCode = 0;
int payCode = 0;
payCode = 4;
if (payCode == 4)
printf(“You get a bonus!\n”);
payCode == 4;
if (payCode == 4)
printf(“You get a bonus!\n”);
int payCode = 0;
int payCode = 0;
payCode = 3;
if (payCode == 4)
printf(“You get a bonus!\n”);
payCode = 3;
if (payCode = 4)
printf(“You get a bonus!\n”);
Pre-increment and Post-increment
Pre-increment/Pre-decrement
Operator is used before the variable (++c or --c)
Variable is changed before the expression it is in is
evaluated
Post-increment/Post-decrement
Operator is used after the variable (c++ or c--)
Expression executes before the variable is
changed.
c = 5;
printf(“%d\n”, ++c);
printf(“%d\n”, c);
c = 5;
printf(“%d\n”, c++);
printf(“%d\n”, c);
c = 5;
++c;
printf(“%d\n”, c);
c = 5;
c++;
printf(“%d\n”, c);
Logical Operators
x
Logical AND - &&
y
&&
0
1
0
0
0
1
0
1
Logical OR - ||
x
y
Logical NOT - !
||
0
1
0
0
1
1
1
1
Logical Operators
Examples
int F = 0, T = 1;
int x, y;
x
x
x
y
x
=
=
=
=
=
!240;
!0;
(F && 1) || (T || F);
(T || !F) && (!F && T);
!(T && F || 1)
if (grade >= 80 && grade <90)
printf(“B\n”);
while ((x > 0) || (y > 10)) {
x--;
y--;
}
scanf and printf
scanf()
Used to obtain a value (values) from the user using
standard input (usually keyboard).
Examples
int num, j;
float x;
scanf(
scanf(
scanf(
scanf(
“%d”, &num);
“%d%d”, &num, &j);
“%f”, &x);
“%d%d%f”, &num, &j, &x);
scanf( “%d”, num);
scanf( “%d%d”, num);
scanf and printf
printf()
int num = 3, j = 2;
float x = 3.0;
printf( “Good Luck!\n”); /* prints a string */
printf( “num = %d\n”, num); /* prints the value of a variable */
printf( “%d + %d = %d\n”, num, j, num+j);
/* prints the value of an expression */
printf( “%c\n”, ‘b’); /* prints the character ‘b’ */
j = ‘a’;
printf( “%c\n”, j); /* prints the character ‘a’ */
printf( “x = %f\n”, x); /* prints a floating-point number */
printf( “x = %.2f\n”, x );
/* prints result with 2 digits after decimal point */