Dr. Sorin Draghici

Wayne State University

Implicit type conversions and casts

Arrays

Loops

for

while

do

Implicit (automatic) type conversions

float x = 1.5 ;

int y = 2 ;

double result ;

result = x * y ; /* result will be 3. as a double*/

float x ;

int a, b ;

a = 3 ;

b = 2 ;

x = a/b ; /* x = 1. */

x = (float)a/b ; /* x = 1.5 */

int table[10] ; /* table is an array of 10 integers */

table[3] = 2 ;

y = table[3] ;

while ( condition )

statement

__Examples:__

i = 0 ;

while( i < 10 )

scanf("%d", &a[i++] ) ;

while ( condition )

statement

__Examples:__

i = 0 ;

do

scanf("%d", &a[i++] ) ;

while ( i < 9 ) ;

for( initialize ; condition; increment )

statement

__Examples:__

__
__for( i = 0 ; i < 10 ; i++ )

scanf("%d", &a[i] ) ;

Example:

x = 1 ;

while ( x < 10 )

printf("%d\n", x ) ;

x++ ;

printf("I'm done\n") ;

Example:

for( i = 0 ; i < 10 ; i++ )

x[i] = y[i] ;

for( i = 0 ; i < 10 ; x[i] = y[i++] )

;

;

for( i = 0 ; i < 10 ; x[i] = y[i++] ) ;

int a[10], b[10] ;

for( i = 0 ; i < 10 ; i++ ) ;

x[i] = y[i] ;

i = 0 ;

while( x < 10 ) ;

printf("%d\n", x ) ;

Usually they are logical expressions like:

( x>0 && y>0 )

A logical expression is constructed with logical operators like:

&&, ||, !

…and relational operators like:

<, <=, >, >=, ==, !=

'

'

Example:

y = 1 ; x = 7 ;

if ( x = y )

printf("%d\n", x ) ;

else

printf("%d\n", y ) ;

'=' is the assignment operator

'==' is the equality test operator

Don't use one when you need the other.

Arrays

Loops

for

while

do

Don't forget the

chap. 6, pg. 200-202

chap. 7, pg. 245-247