exam 2 – nov 18th room aciv 008
DESCRIPTION
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 L14 slide 18) Use loop structure to compute the power. A Sample Program to Illustrate Switch-Case. switch ( grade ) { case ( ' a ' ) : - PowerPoint PPT PresentationTRANSCRIPT
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 L14 slide 18)
Use loop structure to compute the power.
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;
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 */
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!!!
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 OR if ( z == 0 || x > 10 )
! is NOTif ( ! (bob > 42) )
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.
Practice with Arithmetic Expressions int a = 1, b = 2, c = 3 ; float x = 3.33, y = 6.66 ;
Expression Numeric Value True/False a + b 3 T b - 2 * a 0 F c - b – a 0 F c – a 2 T y – x 3.33 T y - 2 * x 0 F
Truth Table for &&Exp1 Exp2 Exp1 && Exp2
0 0 0
0 nonzero 0
nonzero 0 0
nonzero nonzero 1
Exp1 && Exp2 && … && Expn will evaluate to 1 (true) only if ALL subconditions are true.
Truth Table for ||Exp1 Exp2 Exp1 || Exp2
0 0 0
0 nonzero 1
nonzero 0 1
nonzero nonzero 1
Exp1 && Exp2 && … && Expn will evaluate to 1 (true) if only ONE subcondition is true.
Truth Table for !Expression ! Expression
0 1
nonzero 0
Some Practice Expressionsint a = 1, b = 0, c = 7;
Expression True/False
a 1 Tb 0 F
c 7 T
a + b 1 F
a && b T&&F F
a || b T || F T
!c 7 F
!!c 7 T
a && !b T && T T
a < b && b < c F && T F
a > b && b < c T && T T
a >= b || b > c T || F T
Preprocessor Directives Lines that begin with a # in column 1 are called
preprocessor directives (commands). Example: the #include <stdio.h> 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 <foo.h> 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.
Nested for Loopsfor ( 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 ?
XOXOXOXO
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
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 Loop Inner Loop
Nested for Loopsint 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
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.
Example break in a for Loop#include <stdio.h>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
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.
Example continue in a for Loop#include <stdio.h>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
Exam 2 – Nov 18th
Room ACIV 008