Top Banner
Lecture 10: Reviews
29

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

Dec 31, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.

Lecture 10: Reviews

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

Control Structures

All C programs written in term of 3 control structuresSequence structures

Programs executed sequentially by defaultSelection structures

Used to choose among alternative courses of actionThree types: if, if … else, and switch

Repetition structuresUsed to repeat an action when some condition remains true.Three types: while, do … while and for

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

Sequence StructuresNormally, statements are executed one after the other in the order written

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

Selection Structures - ifif statement

if (condition) { statement(s);}

Only performs an action if the condition is true.Example:

Flowchart

if ( grade >= 60 ) {

printf( “Passed\n” );}

if ( grade >= 60 ) {printf( “Passed\n” );

printf( “Congratulations!\n” );}

if ( grade >= 60 ) printf( “Passed\n” );

printf( “Congratulations!\n” );

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

Selection Structures - if…elseIf…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

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

Selection Structures - if…elseNested 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 skippedif ( 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: A80 ~ 89:B70 ~ 79:C60 ~ 69:D< 60: F

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

Selection Structures - if…elseif…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" ); }

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

Selection Structures - switchswitch 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:

actions }

The switch statement can be used only for testing a constant integral expression.switch (grade) {

case >= 90: … case >= 80: … ….. }

default case occurs if none of the cases are matched

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

Selection Structures - switchswitch 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.

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

Selection Structures - switchswitch statement with break statements

Exampleswitch ( 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;}

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

Selection Structures - switchswitch statement with break statements

Exampleswitch ( 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;}

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

Repetition StructuresTwo types of repetition structures

Counter-controlled repetitionDefinite repetition: know how many times loop will executeControl variable used to count repetitions

Sentinel-controlled repetitionIndefinite repetitionUsed when number of repetitions not knownSentinel value indicates "end of data entry”Loop ends when user inputs the sentinel value

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

Repetition StructuresCommon 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--;}

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

Repetition Structures - whilewhile loop repeated until condition becomes false

Counter-controlled repetitionSentinel-controlled repetition

Formatwhile ( condition ) { statement(s);}

Flowchart

condition while body

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

Repetition Structures - whileCounter-controlled while loop Loop repeated until counter reaches a certain valueCount 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 = 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;}

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

Repetition Structures - whileSentinel-controlled while loop Loop repeated until user inputs the sentinel valueSentinel value chosen so it cannot be confused with a regular inputExamples

Simple calculatorATM menu interfacePrime number

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

Repetition Structures - do…whileSimilar to the while statementCondition 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

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

Repetition Structures - do…whileExamples

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 ++;}

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

Repetition Structures - forCan loop a known number of timesFormat 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); }

for (j = 10; j > 0; j--) { printf(“%d\n”, j); }

for (j = 10; j > 0; j -= 2) { printf(“%d\n”, j); }

increment decrement Increasing/decreasing other than 1

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

Nested Control LoopsNested 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 loopsfor (i = 1; i < 5; i++) { for (j = 1; j < 5; j++) {

printf(“%d * %d = %d\n”, i, j, i*j); }

}

* 1 2 3 4

1 1 2 3 4

2 2 4 6 8

3 3 6 9 12

4 4 8 12 16

i

j

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

Nested Control LoopsNested 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 loopsfor (i = 1; i < 5; i++) { for (j = 1; j <= i; j++) {

printf(“%d * %d = %d\n”, i, j, i*j); }

}

* 1 2 3 4

1 1 2 3 4

2 2 4 6 8

3 3 6 9 12

4 4 8 12 16

i

j

j <= i

j <= i

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

Nested Control LoopsNested 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 loopsfor (i = 1; i < 5; i += 2) { for (j = 1; j < 5; j++) {

printf(“%d * %d = %d\n”, i, j, i*j); }

}

* 1 2 3 4

1 1 2 3 4

2 2 4 6 8

3 3 6 9 12

4 4 8 12 16

i

j

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

OperatorsArithmetic operators+, -, *, /, %Assignment operator=Arithmetic assignment operators+=, -=, *=, /=, %=Increment / decrement operators++, --Relational operators<, <=, >, >=Equality operators==, !=Logical operators&&, ||, !

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

== and =

Swapping both operators does not ordinarily cause syntax errors.

int payCode = 0;

payCode = 4;if (payCode == 4) printf(“You get a bonus!\n”);

int payCode = 0;

payCode == 4;if (payCode == 4) printf(“You get a bonus!\n”);

int payCode = 0;

payCode = 3;if (payCode == 4) printf(“You get a bonus!\n”);

int payCode = 0;

payCode = 3;if (payCode = 4) printf(“You get a bonus!\n”);

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

Pre-increment and Post-increment

Pre-increment/Pre-decrementOperator is used before the variable (++c or --c) Variable is changed before the expression it is in is evaluated

Post-increment/Post-decrementOperator 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);

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

Logical Operators

Logical AND - &&

Logical OR - ||

Logical NOT - !

&& 0 1

0 0 0

1 0 1

x

y

|| 0 1

0 0 1

1 1 1

x

y

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

Logical Operators

Examplesint F = 0, T = 1;int x, y;

x = !240;x = !0;x = (F && 1) || (T || F);y = (T || !F) && (!F && T);x = !(T && F || 1)

if (grade >= 80 && grade <90)printf(“B\n”);

while ((x > 0) || (y > 10)) {x--;

y--;}

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

scanf and printf

scanf()Used to obtain a value (values) from the user using standard input (usually keyboard).Examplesint num, j;float x;

scanf( “%d”, &num);scanf( “%d%d”, &num, &j);scanf( “%f”, &x);scanf( “%d%d%f”, &num, &j, &x);

scanf( “%d”, num);scanf( “%d%d”, num);

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

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 */