13
Algebraic Operator C Operator = = = = != Relational Operator C Relational Operator > > < < > >= < <=

Algebraic Operator C Operator == = =!= Relational Operator C Relational Operator > > >=

  • Upload
    piera

  • View
    60

  • Download
    3

Embed Size (px)

DESCRIPTION

Algebraic Operator C Operator == = =!= Relational Operator C Relational Operator > > >=

Citation preview

Page 1: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Algebraic Operator C Operator= = =

= != Relational Operator C Relational Operator

> >< <> >=< <=

Page 2: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Datatypes

Datatype Keyword Bytes integer short 1

int 2long 4

float float 4double 8

Page 3: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Type Casting

• The operator called a cast operator is used to convert from one data type to another. Example: int a = 15; int b = 16; float c = 0.0;

c = ( float ) a / b ; The calculation a / b produces an integer result which is then converted to a float for an accurate solution.

Page 4: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

% f

• %f used to represent floats without specifying precision.

• Format:% a.b f

b specifies the number of places after the decimal point (after rounding the result)

a specifies the total field width (places before + after the decimal

point)

Page 5: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Selection structures

• A selection structure is used to choose among alternative courses of action.• The if selection structure executes an indicated action only when the condition is true.• The if selection structure expects only one statement

in its body. To include several statements, enclose the set of statements in { }.• Compound statement - The set of statements within

{ }

Page 6: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Format: if (expression)

statement;

A decision is made based on the expression. If the expression evaluates to zero, it is treated as false. If the expression evaluates to nonzero, it is treated as true.

Page 7: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Format:if (expression)

{ statement1; statement2;

statementn; }

Page 8: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Example: if ( a = = b ) printf(“The two numbers are equal.\n”);

else printf(“The two numbers are different.\n”);

Page 9: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

• Nested if/else structures test for different cases.Example:

if ( grade >= 90)printf(“ A \n”);

else if ( grade >= 80)printf(“ B \n”);

else if ( grade >= 70)printf(“ C \n”);

else if ( grade >= 60)printf(“ D \n”);

else printf(“F \n”);

Page 10: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

The while Repetition Structure• A Repetition Structure allows the programmer to

specify that an action is to be repeated while some condition remains true.

• Format :while ( expression )

statement;• The while structure body may be a single

statement or a compound statement.• The expression is tested each time through the

loop. The statement is repeated until the expression remains true.

Page 11: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

• When the condition becomes false, the while condition is exited and control passes to the next statement in the program.

• Example: Sum of the first 5 numbers:

j = 1; sum = 0;

while ( j <= 5) {

sum = sum + j; j = j + 1;

}printf(“sum is %d \n”, sum);

Page 12: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Increment and Decrement Operators

• Unary increment operator ++• Unary decrement operator --• These operators are used when a variable is incremented

or decremented by 1. Operator Example

++ ++a ++ a++ -- --b -- b--

Page 13: Algebraic  Operator C Operator == =           =!= Relational Operator C Relational Operator > > >=

Conditional Operator

• Conditional operator ?:• The conditional operator takes three operands

(ternary operator).• Format:

condition ? Value if true : Value if false;• Example: choice = number != 3 ? 0 : 3;