Transcript E2-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 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
3D
Last-Chance
x_yt3
num$
lucky***
area_under_the_curve
num45
#values
pi
%done
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 AND if ( 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
a+b
b-2*a
c-b–a
c–a
y–x
y-2*x
Numeric Value
3
0
0
2
3.33
0
True/False
T
F
F
T
T
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 Expressions
int a = 1, b = 0, c = 7;
Expression
a
b
c
a+b
a && b
a || b
!c
!!c
a && !b
a < b && b < c
a > b && b < c
a >= b || b > c
1
0
7
1
T&&F
T || F
7
7
T && T
F && T
T && T
T || F
True/False
T
F
T
F
F
T
F
T
T
F
T
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 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 Loops
Outer Loop
Output:
**********
int rows, columns;
**********
**********
for (rows=1; rows<=5; rows++)
{
for (columns=1; columns<=10; columns++)
{
printf("*");
}
printf ("\n");
}
**********
**********
Inner Loop
15
Nested for Loops, Example #2
int rows, columns;
for (rows=1; rows<=5; rows++)
{
for (columns=1; columns<=rows; columns++)
{
printf("*");
Output:
}
*
printf("\n");
**
}
***
****
Outer Loop
Inner Loop
*****
16
Nested for Loops
int j, k;
for(j = 0; j < 8; j++)//
{
for(k = 0; k < 8 - j; k++) //draw MAX - j blanks
{
printf(" ");
}
x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
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 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 ( )
OUTPUT:
{
int i ;
1234
for ( i = 1; i < 10; i = i + 1 )
{
Broke out of loop at i = 5.
if (i == 5)
If brake was not there the
{
output would have been
break ;
123456789
}
printf (“%d “, i) ;
}
printf (“\nBroke out of loop at i = %d.\n”, i) ;
return 0 ;
}
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:
12346789
Done.
5
Note we used continue; to skip
printing 5
Exam 2 – Nov 18th
Room ACIV 008