View
216
Download
0
Embed Size (px)
Citation preview
1
Flow of Control
Chapter 4 in ABC
2
Operators and Associativity
Operator Associativity
+(unary) -(unary) ++ -- ! right to left* / % left to right
+ - left to right< <= > >= left to right
== != left to right
&& left to right
|| left to right= += -= *= /= etc. right to left
A boolean expression evaluates
to 0 or 1 (int)
There is no type “boolean” in C. Numeric values are used instead: 0 false, 0 true
3
int i = 1, j = 2, k = 3;
Declarations and Initializations
Expression Equivalent Expression Value
i == j j == i 0
i != j j != i 1
i + j + k == -2 * -k (( i + j ) + k ) == (( -2 ) * ( -k )) 1
4
char c = `A';
int i = 7, j = 7;
double x = 0.0, y = 2.3;
Declarations and Initializations
Expression Equivalent Expression Value
! c ?
! ( i – j ) ?
! i – j ?
! ! ( x + y ) ?
! x * ! ! y ?
5
char c = `A';
int i = 7, j = 7;
double x = 0.0, y = 2.3;
Declarations and Initializations
Expression Equivalent Expression Value
! c ! c 0
! ( i – j ) ! ( i – j ) 1
! i – j ( ! i ) – j -7
! ! ( x + y ) ! ( ! ( x + y ) ) 1
! x * ! ! y ( ! x ) * ( ! ( ! y ) ) 1
6
char c = `B';
inti = 3, j = 3, k = 3;
double x = 0.0, y = 2.3;
Declarations and Initializations
Expression Equivalent Expression Value
i && j && k ( i && j ) && k 1
x || i && j – 3 x || ( i && ( j – 3 ) ) 0
i < j && x < y ( i < j ) && ( x < y ) 0
i < j || x < y ( i < j ) || ( x < y ) 1
‘A’ <= c && c <= ‘Z’ ( ‘A’ <= c ) && ( c <= ‘Z’ ) 1
c – 1 == ‘A’ || c + 1 == ‘Z’ ( ( c – 1 ) == ‘A’ ) || ( ( c + 1 ) == ‘Z’ ) 1
7
Declarations and Initializations
int main(void){
char c = `w‘, c2;
int i = 1, j = 2, k = -7;
double x = 7e + 33, y = 0.001;
...
Expression Equivalent Expression Value
‘a’ + 1 < c ?
-i – 5 * j >= k + 1 ?
3 < j < 5 ?
x-3.333 <= x + y ?
x < x + y ?
what is the value of c2?c2 is not initialized (i.e. contains garbage).It is NOT a compilation
error to access an uninitialized variable
8
Declarations and Initializations
int main(void){
char c = `w‘, c2;
int i = 1, j = 2, k = -7;
double x = 7e + 33, y = 0.001;
...
Expression Equivalent Expression Value
‘a’ + 1 < c (‘a’ + 1 ) < c 1
-i – 5 * j >= k + 1 (( -i ) – ( 5 * j )) >= ( k + 1 ) 0
3 < j < 5 ( 3 < j ) < 5 1
x-3.333 <= x + y ( x – 3.333 ) <= ( x + y ) 1
x < x + y x < ( x + y ) 0
c2 is not initialized (i.e. contains garbage).It is NOT a compilation
error to access an uninitialized variable
9
Short-circuit Evaluation (&&,||)
Short circuit evaluation: The evaluation process stops as the outcome “true” / “false” is known.
if ( (i != 0) && ( 1/i < ....)){...}
into cnt = 0;while ( ++cnt <= 3 && (c = getchar()) != EOF ) {
.
.}
Keep looping as long as the expression evaluates to a value
different than zero
Keep looping as long as the expression evaluates to a value
different than zero
Do SomethingDo SomethingWhat is the maximum number
that getchar() is called?
not evaluated if i==0
10
if ( a == 1 )
printf( “***\n” );
else
printf( “###\n” );
if ( a == 1 )
printf( “***\n” );
printf( “another command\n” );
The if Statement
An if / else statementAn if / else statement
An if statement, not followed by an else
statement
An if statement, not followed by an else
statement
11
if ( a == 1 )
a single command;
else
a single command;
The if Statement
Open a block if you likeOpen a block if you like
if ( a == 1 ) { some commands… }else {some commands…}
12
Software 1, TAU - 4.7
The if Statement
if (a == 1)
if (b == 2)
printf("***\n");
else
printf("###\n");
is equivalent to
if (a == 1)
if (b == 2)
printf("***\n");
else
printf("###\n");
Indentation does not influence the else statementIndentation does not influence the else statement
13
Software 1, TAU - 4.7
The if Statement
if (a == 1)
if (b == 2)
printf("***\n");
else
printf("###\n");
NOT equivalent to
if (a == 1) {
if (b == 2)
printf("***\n");
}
else
printf("###\n");
“Dangling else“: What about the else???“Dangling else“: What about the else???
14
Switch Statementswitch ( c )
{
case `a':
++a_cnt;
break;
case `b':
++b_cnt;
break;
case `c':
case `C':
++cC_cnt;
break;
default:
++other_cnt;
}
value to be evaluated (integral expression)value to be evaluated (integral expression)
The cases are constant integral expressions with unique values, indicating labels to jump to
(according to the value of c)
The cases are constant integral expressions with unique values, indicating labels to jump to
(according to the value of c)
15
The conditional operator exp1?exp2:exp3
x = exp1?exp2:exp3; is Equivalent to: if (exp1) x =exp2;
else x = exp3;
Example:
char a = `a', b = `b';
int i = 1, j = 2;
double x = 7.07;
Expression Equivalent Expression Value Type
i == j ? a – 1 : b + 1 ( i == j ) ? ( a – 1 ) : ( b + 1 ) 99 int
j % 3 == 0 ? i + 4 : x (( j % 3 ) == 0 ) ? ( i + 4 ) : x 7.07 double
j % 3 ? i + 4 : x ( j % 3 ) ? ( i + 4 ) : x 5.0 double
The type is determined by both exp2 and exp3 – irrespective of which is evaluated
‘a‘ has a decimal value of 97‘a‘ has a decimal value of 97
16
int i=1, factorial=1;While ( i++ < n )
factorial *= i;
While ( ( c = getchar() ) != EOF ){
if ( c >= ‘a’ && c <= ‘z’ )++lowercase_letter_cnt;
++total_cnt;}
The while Statement
Loop until i is equal to, or bigger than n.
Loop until i is equal to, or bigger than n.
Loop until the EOF character is encountered.
Loop until the EOF character is encountered.
17
for ( expr1; expr2; expr3)
statement
next statement
expr1;
while ( expr2 )
{
statement
expr3
}
next statement
The while Statement
These are equivalent!!! These are equivalent!!!
18
The for Statement
i = 1; i = 1;
sum = 0; sum = 0;
for ( ; i <= 10; ++i) for ( ; i <= 10; )
sum += i; sum += i++;
i = 1;
sum = 0;
for ( ; ; )
{
sum += i++;
printf("%d\n", sum);
}
Here we sum the numbers from 1 to 10
Here we sum the numbers from 1 to 10
Here we have an infinite loop!!!
Here we have an infinite loop!!!
sum = 0for ( i = 1; i <= 10; ++i ) sum += i;
The loop stops when the condition (middle statement)
evaluates to 0
The loop stops when the condition (middle statement)
evaluates to 0
19
The for Statement
for ( sum = 0, i = 1; i <=n; ++i )
sum += i;
is equivalent to:
for ( sum = 0, i = 1; i <= n; sum += i, ++i );
but is not equivalent to:
for ( sum = 0, i = 1; i <= n; ++i, sum += i );
20
The comma Operator
• Usually used in “for loops” - allows for multiple initializations and multiple processing of indices.
• Has the lowest precedence of all the operators in C
• left-to-right Associativity
• The expression exp1,exp2 as a whole has the value and type of the right operand (i.e. exp2)
Example:
int i, j, k = 3;
double x = 3.3;
Expression Equivalent Expression Value
i = 1, j = 2, ++k + 1 (( i = 1 ), ( j = 2 )), (( ++k ) + 1 ) 5
k != 1, ++x * 2.0 + 1 ( k != 1 ), ((( ++x ) * 2.0 ) + 1 ) 9.6
21
The do-while Statement
int error = 1;
do {
int, n;
printf( “Input a positive integer: ” );
scanf( “%d”, &n );
if (error = (n <= 0) )
printf( “\nERROR; Do it again!\n\n” );
} while (error);
22
The do-while Statement
int error = 1;
do {
int res, n;
printf( “Input a positive integer: ” );
res = scanf( “%d”, &n );
if (res != 1){
printf(“\nERROR: failed to read integer number!\n\n”);
break;
}
if (error = (n <= 0) )
printf( “\nERROR; Do it again!\n\n” );
} while (error);
23
The break Command
while(1){
while ( 1 )
{
scanf( “%1f”, &x );
if ( x < 0.0 )
break;
printf( “%f\n”, sqrt( x ) );
}
}
Exit the loop if x is negativeExit the loop if x is negative
break jumps to here!!break jumps to here!!
24
The continue Command
for ( i = 0; i < TOTAL; ++i )
{
c = getchar();
if ( c >= `0' && c <= `9' )
continue;
.
.
.
}
Jump to the end of the current iterationJump to the end of the current iteration
25
to hereto here
goto and labelinggoto error;
.....
error:
{
printf( “An error has occurred -- bye!\n” );
exit(1);
}
while (scanf( “%1f”, &x ) == 1 )
{
if (x < 0.0)
goto negative_alert;
printf( “%f %f\n”, sqrt(x), sqrt(2 *x));
}
negative_alert: printf( “Negative value encountered!\n” );
Jump from this line of code to the line with the appropriate label
Jump from this line of code to the line with the appropriate label
Jump from hereJump from here
26
Count blanks, digits, letters, newlines and others
#include <stdio.h>
int main( void )
{
int blank_cnt = 0, c = 0, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0;
while ( (c = getchar()) != EOF )
if ( c == ' ' )
++blank_cnt;
else if ( c >= '0' && c <= '9' )
++digit_cnt;
else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
++letter_cnt;
else if (c == '\n')
++nl_cnt;
else
++other_cnt;
Brackets are not neccessary (but it is advised to use them for
readability!!)
Brackets are not neccessary (but it is advised to use them for
readability!!)
27
Example: Count blanks, digits, letters, newlines and others
printf( “%10s%10s%10s%10s%10s%10s\n\n”,
“blanks”, “digits”, “letters”, “lines”, “others”, “total”);
printf( “%10d%10d%10d%10d%10d%10d\n\n”,
blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt,
blank_cnt + digit_cnt + letter_cnt + nl_cnt + other_cnt );
return 0;
}
blanks digits letters lines others total
197 31 348 27 180 783
28
Example: Print a table of values for some Boolean functions
#include <stdio.h>
int main( void )
{
int b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0;
int cnt = 0;
printf( “\n%5s%5s%5s%5s%5s%5s%7s%7s%11s\n\n”,
“Cnt”, “b1”, “b2”, “b3”, “b4”, “b5”, “fct1”, “fct2”, “majority” );
.
.
.
Boolean variablesBoolean variables
HeadingsHeadings
29
Print a table of values for some Boolean functions
for ( b1 = 0; b1 <= 1; ++b1 )
for ( b2 = 0; b2 <= 1; ++b2 )
for (b3 = 0; b3 <= 1; ++b3)
for (b4 = 0; b4 <= 1; ++b4)
for (b5 = 0; b5 <= 1; ++b5)
printf( “%5d%5d%5d%5d%5d%5d%6d%7d%9d\n”,
++cnt, b1, b2, b3, b4, b5, b1 || b3 || b5, b1 && b2 || b4 && b5,
b1 + b2 + b3 + b4 + b5 >= 3 );
putchar('\n');
return 0;
}
Cnt b1 b2 b3 b4 b5 fct1 fct2 majority
1 0 0 0 0 0 0 0 0
2 0 0 0 0 1 1 0 0
3 0 0 0 1 0 0 0 0
…
The resultThe result
30
A Test that Fails#include <stdio.h>
int main( void )
{
int cnt = 0;
double sum = 0.0, x = 0.0;
for ( x = 0.0; x != 9.9; x += 0.1 )
{
sum += x;
printf( “cnt = %5d\n”, ++cnt );
}
printf( “sum = %f\n”, sum );
return 0;
}
The test x<9.9 is more robust in this case
==> It is a good programming style to use a relational expression, when appropriate, rather than equality
expression.
The test x<9.9 is more robust in this case
==> It is a good programming style to use a relational expression, when appropriate, rather than equality
expression.
Trouble!!! (on most machines goes into an infinite loop)
Trouble!!! (on most machines goes into an infinite loop)
31
#include <stdio.h>
#define LIMIT 46
int main(void)
{
long f0 = 0, f1 = 1, n = 0, temp = 0;
printf( “%7s%19s%29s\n%7s%19s%29s\n%7s%19s%29s\n”,
“ ”, “Fibonacci”, “Fibonacci”,
“ n”, “ number”, “ quotient”,
“--”, “---------”, “---------” );
printf( “%7d%19d\n%7d%19d\n”, 0, 0, 1, 1 );
for ( n = 2; n <= LIMIT; ++n ) {
temp = f1;
f1 += f0;
f0 = temp;
printf( “%7ld%19ld%29.16f\n”, n, f1, (double) f1 / f0 );
}
return 0;
}
Print Fibonacci Numbers and Quotients
HeadingsHeadings
The first two casesThe first two cases
32
nFibonacci
numberFibonacci quotient
0 0
1 1
2 1 1.0000000000000000
3 2 2.0000000000000000
4 3 1.5000000000000000
5 5 1.6666666666666667
6 8 1.6000000000000000
7 13 1.6250000000000000
………. ………. ……….
23 28657 1.6180339901755971
………. ………. ……….
44 701408733 1.6180339887498949
46 1836311903 1.6180339887498949
Print Fibonacci Numbers and Quotients