slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Exam 2 – Nov 18th Room ACIV 008 PowerPoint Presentation
Download Presentation
Exam 2 – Nov 18th Room ACIV 008

Loading in 2 Seconds...

play fullscreen
1 / 22

Exam 2 – Nov 18th Room ACIV 008 - PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on

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 ' ) :

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Exam 2 – Nov 18th Room ACIV 008' - lavey


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
Exam 2 – Nov 18th

Room ACIV 008

project 2 update
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
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 case1
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
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
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 AND if ( x > 5 && y < 6 )

|| is OR if ( z == 0 || x > 10 )

! is NOT if ( ! (bob > 42) )

arithmetic expressions true or false
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
Practice with Arithmetic Expressions
  • int a = 1, b = 2, c = 3 ;
  • float x = 3.33, y = 6.66 ;
  • ExpressionNumeric ValueTrue/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
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 for1
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 for2
Truth Table for !

Expression ! Expression

0 1

nonzero 0

some practice expressions
Some Practice Expressions

int a = 1, b = 0, c = 7;

Expression True/False

a 1 T b 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
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.hat 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 loops
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

XO

XO

XO

nested for loops1

Outer Loop

Inner Loop

Nested for Loops

Output:

**********

**********

**********

**********

**********

int rows, columns;

for (rows=1; rows<=5; rows++)

{

for (columns=1; columns<=10; columns++)

{

printf("*");

}

printf ("\n");

}

nested for loops example 2
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 loops2
Nested for Loops

x

xx

xxx

xxxx

xxxxx

xxxxxx

xxxxxxx

xxxxxxxx

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");

the break statement
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
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
  • 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
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

slide22
Exam 2 – Nov 18th

Room ACIV 008