Computers in Engineering - 308-208 Lecture 14
Computers in Engineering - 308-208

# Lesson 14 - Learning Goals

## 14.3 Learn some of the difference between C and FORTRAN

```   mark = mark/7 ;
price = price + tax ;
price = price * tax + price ;
bonus = bonus - 7 ;
total = total * 5 ;
month = month % 12 + 1 ;

```

EQUIVALENT STATEMENTS USING ARITHMETIC OPERATORS

```   mark /= 7 ;
price += tax ;
price *= tax + price ;
bonus -= 7 ;
total *= 5 ;
month %= 12 + 1 ;

```

EXAMPLES USING PRINTF( ) LIBRARY FUNCTION

```   printf("Hello, World \n");
printf("%d %f %lf",integer,real,long_integer );
printf(" %c %s ", character, string );
printf(" %p ", pointer);
printf(" %o ", octa_notation);
printf(" %x ",hexa_notation);
printf(" %u",unsigned_variable);
printf(" %s is %d years old ",name,age);
printf(" \t abcdefg \n\t hijklmn \n\t ... ");
printf(" He got %f \% in his final exam !!!",grade);

```

VALID STATEMENTS IN C

• Assignment statement, procedure call
```   a = b+c;
count = scanf ("%d", &n);
scanf ("%d", &n);```
( These always end with semicolons)
• Empty statement
```   ;

```
• Compound statement
```{
statement
{
statement temp = x ;
x = y ;
y = temp ;
}
statement
}

```

LOGICAL EXPRESSIONS IN C

• No special equivalent of LOGICAL
• Use char for logical values
• Use 0 for false, non-zero for true
• if ( exp ) then... executes " then " part if ( exp ) is non-zero
• Logical ops return 1 if comparison or combination is true

 C FORTRAN 90 FORTRAN 77 Function 0 .FALSE. .FALSE. false non-zero .TRUE. .TRUE. true ! ! .NOT. logical negate < < .LT. less than <= <= .LE. less than or equal > > .GT. greater than >= >= .GE. greater than or equal == == .EQ. equal != /= .NE. not equal && && .AND. logical AND || || .OR. logical OR

• Can't use comparisons on strings -- use strcmp :

Example :

```strcmp("b", "a") returns {1} (true because b > a)

strcmp("b", "b") returns {0} (false because b cannot be
big & small at the same time)
```

GENERAL FORM OF IF ... ELSE STATEMENT

```   if ( expression )
statement_1 ;
else
statement_2;
```

GENERAL FORM OF IF STATEMENT ( WITHOUT ELSE STATEMENT )

```   if ( expression ) statement ;

```

OR ( WITH MORE THAN ONE STATEMENT )

```if ( expression )
{
statement_1;
statement_2;
.
.
.
statement_n;
}

```

EXAMPLES OF NESTED IF STATEMENTS

Example #1 :

```   if ( grade >= 85 )
if ( final > 90 )
printf( "Outstanding student !");
else
printf( "Excellent work !" );

```

Example #2 :

```   if( cost > sales )
if( cost/2 > 100*sales )
printf( "You are in big trouble !!!");
else
printf( "You seem to be in trouble. ");

```

Example #3 :

```   if( house_value > 500000 )
if( number_cars > 3 )
printf( "You are running into debt !!!");
else
printf( "You are rich." );

```

Example #4 :

```   if( house_value > 500000 ) {
if( number_cars > 3 )
printf( "You are running into debt !!!"); }
else
printf( "Buy new cars !!!" );

```

GENERAL FORM OF IF...ELSE IF...ELSE STATEMENTS

```if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
.
.
.
else if ( expression_n )
statement_n;
else
statement_k;

```

EXAMPLE USE OF IF...ELSE IF...ELSE STATEMENT

```/* Function to print the letter grade */

{
if ( grade < 50 )
printf(" F ");
else if ( grade < 55 )
printf(" C- ");
else if ( grade < 60 )
printf(" C ");
else if ( grade < 65 )
printf(" C+ ");
else if ( grade < 70 )
printf(" B- ");
else if ( grade < 75 )
printf(" B ");
else if ( grade < 80 )
printf(" B+ ");
else if ( grade < 85 )
printf(" A- ");
else
printf(" A ");
}

```

BIT OPERATORS

 Operator Explanation & bitwise logical AND | bitwise logical OR ^ bitwise exclusive OR << left shift >> right shift ~ one's complement

FULL SET OF C OPERATORS IN ORDER OF DECREASING PRECEDENCE

 Operator Associativity ( ) [ ] -> left to right ! + - ++ -- * & (cast type) size of right to left * / % left to right + - left to right << >> left to right < <= > >= left to right = = != left to right & left to right ^ left to right | left to right && left to right || left to right ?: right to left = += -= *= /= %= &= ^= |= <<= >>= right to left , left to right

On to the next lecture