Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture.

Similar presentations


Presentation on theme: "Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture."— Presentation transcript:

1 Exam 2 – Nov 18th Room ACIV 008

2 Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture L14 slide 18)  Use loop structure to compute the power.

3 A Sample Program to Illustrate Switch-Case switch (grade) { case ('a') : case ('A') : printf ("Good Job!\n") ; brake; case ('b') : case ('B') : printf ("Pretty good.\n") ; brake;

4 A Sample Program to Illustrate Switch-Case case ('c') : case ('C') : printf ("Better get to work.\n") ; brake; case ('d') : case ('D') : printf ("You are in trouble.\n") ; brake; default : printf ("You are failing!!\n") ; brake; } /* End of switch-case structure */ } /* End of main program */

5 Which Are Legal Identifiers? AREA area_under_the_curve 3D num45 Last-Chance #values x_yt3 pi num$ %done lucky*** Try them all in one of your the programs!!!

6 Logical Operators  So far we have seen only simple conditions. if ( count > 10 )...  Sometimes we need to test multiple conditions in order to make a decision.  Logical operators are used for combining simple conditions to make complex conditions. && is ANDif ( x > 5 && y < 6 ) || is ORif ( z == 0 || x > 10 ) ! is NOTif ( ! (bob > 42) )

7 Arithmetic Expressions: True or False  Arithmetic expressions evaluate to numeric values.  An arithmetic expression that has a value of zero is false.  An arithmetic expression that has a value other than zero is true.

8 Practice with Arithmetic Expressions int a = 1, b = 2, c = 3 ; float x = 3.33, y = 6.66 ; ExpressionNumeric Value True/False a + b3T b - 2 * a0F c - b – a0F c – a2T y – x3.33T y - 2 * x0F

9 Truth Table for && Exp 1 Exp 2 Exp 1 && Exp 2 0 0 0 0nonzero 0 nonzero0 0 nonzero nonzero 1 Exp1 && Exp2 && … && Expn will evaluate to 1 (true) only if ALL subconditions are true.

10 Truth Table for || Exp 1 Exp 2 Exp 1 || Exp 2 0 0 0 0nonzero 1 nonzero0 1 nonzero nonzero 1 Exp1 && Exp2 && … && Expn will evaluate to 1 (true) if only ONE subcondition is true.

11 Truth Table for ! Expression ! Expression 0 1 nonzero 0

12 Some Practice Expressions int a = 1, b = 0, c = 7; Expression True/False a1T b0F c7T a + b1F a && bT&&FF a || bT || FT !c7F !!c7T a && !bT && TT a < b && b < cF && TF a > b && b < cT && TT a >= b || b > cT || FT

13 Preprocessor Directives  Lines that begin with a # in column 1 are called preprocessor directives (commands).  Example: the #include directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.  If we have #include the preprocessor will place the contents of the file foo.h into the code.  This header file was included because it contains information about the printf ( ) function that is used in this program.

14 Nested for Loops for ( i = 1; i < 5; i = i + 1 ) { for ( j = 1; j < 3; j = j + 1 ) { if ( j % 2 == 0 ) { printf (“O”) ; } else { printf (“X”) ; } printf (“\n”) ; } How many times is the “if” statement executed? What is the output ? XO

15 15 Nested for Loops int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) { printf("*"); } printf ("\n"); } Output: ********** Inner Loop Outer Loop

16 16 Nested for Loops, Example #2 int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) { printf ("*"); } printf ("\n"); } Output: * ** *** **** ***** Outer LoopInner Loop

17 Nested for Loops int j, k; for(j = 0; j < 8; j++)// { for(k = 0; k < 8 - j; k++) //draw MAX - j blanks { printf(" "); } for(k = 0; k <= j; k++) //draw remaining j columns as x's { printf("x"); } printf("\n"); } printf("\n"); x xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx

18 The break Statement  The break statement can be used in while, do-while, and for loops to cause premature exit of the loop.  THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

19 Example break in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { break ; } printf (“%d “, i) ; } printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; } OUTPUT: 1 2 3 4 Broke out of loop at i = 5. If brake was not there the output would have been 1 2 3 4 5 6 7 8 9

20 The continue Statement  The continue statement can be used in while, do-while, and for loops.  It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.  THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

21 Example continue in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { continue ; } printf (“%d ”, i) ; } printf (“\nDone.\n”) ; return 0 ; } OUTPUT: 1 2 3 4 6 7 8 9 Done. 5 Note we used continue ; to skip printing 5

22 Exam 2 – Nov 18th Room ACIV 008


Download ppt "Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture."

Similar presentations


Ads by Google