You are on page 1of 23

Lecture 3

OPERATORS &CONTROL STRUCTURE

Sarwar Morshed
American International University-Bangladesh (AIUB)

08/27/20 Sarwar Morshed 1


THE % FORMAT SPECIFIERS
The % Specifiers that you can use in ANSI C are:

08/27/20 Sarwar Morshed 2


Relational and Equality operators
In actuality, expressions like “5 > 3” are evaluated to
integer values: 1 for true, 0 for false. Thus the program
void main() {
printf( "Result of 1 > 2: %d\n", 1 > 2 );
printf( "Result of 6 < 8: %d\n", 6 < 8 );
}
Has Output?

08/27/20 Sarwar Morshed 3


Relational Opeators
Relational operators: >, >=, <, <=
Equality operators: ==, !=
IMPORTANT ‘==’ (two equals) versus ‘=’ (one equal) is
an extremely common source of programmer errors in
C.
One equal, =, is an assignment operator.

08/27/20 Sarwar Morshed 4


Relational or conditional operators:
== means 'is equal to'
!=  means 'is not equal to'
<   means 'is less than'
>   means 'is greater than'
<= means 'is less than or equal to'
>= means 'is greater than or equal to‘
Logical Operators:
&&  means 'AND'
||      means 'OR'
!      means 'NOT‘
Note: Result of the condition is always wither true(1) or
false(0)
08/27/20 Sarwar Morshed 5
Logical Operators
&&, || (logical AND, logical OR) are binary operators:
 Takes two arguments.
expression1 && expression2 evaluates to 1 (“true”) if both
expressions are non-zero, otherwise evaluates to 0 (“false”).

expression1 || expression2 evaluates to 1 (“true”) if either or


both expressions are non-zero, otherwise evaluates to 0
(“false”).

!expression evaluates to 1 (“true”) if the expression is zero,


otherwise evaluates to 0 (“false”).

08/27/20 Sarwar Morshed 6


Of course, instead of using only numeric constants, we
can use any valid expression, including variables.
Suppose that a=2, b=3 and c=6,

(a == 5) // evaluates to false since a is not equal to 5.


(a*b >= c) // evaluates to true since (2*3 >= 6) is true.
(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is
false.
((b=2) == a) // evaluates to true.

08/27/20 Sarwar Morshed 7


The If Statement
if (condition)
{
    statement;
}
Multiple statement within If
if (condition)
{
    statement 1;
    statement 2;
    -----------;
    statement n;
}
08/27/20 Sarwar Morshed 8
The if else statement
if (condition)
{
    statement 1;
    statement 2;
}
else
{
    statement 1;
    statement 2;
}

08/27/20 Sarwar Morshed 9


The if-else if-else statement
if (condition)
{
    statement . . . ;
}
else if (condition)
{
    statement . . .;
}
else
{
    statement . . .;
}
08/27/20 Sarwar Morshed 10
08/27/20 Sarwar Morshed 11
Example
Example.
if ( ( year % 4 == 0 && year % 100 != 0 )
||( year % 400 == 0) )
{
printf( "%d is a leap year\n", year );
}
else
{
printf( "%d is not a leap year\n", year );
}
08/27/20 Sarwar Morshed 12
Example
void main() {
int num;
printf( "Please enter a positive integer:\n" );
scanf( "%d", &num );
if (num % 3 == 0)
printf( "%d is divisible by 3.\n", num );
else if (num % 2 == 0)
printf( "%d is divisible by 2, but not 3.\n", num );
else
printf( "%d is not divisible by 3 nor 2.\n",num );
}

08/27/20 Sarwar Morshed 13


More on ‘if’
if executes the statement (or statement block) after it
when the specified condition is non-zero.
Thus, the following fragment prints ????
if( 18 )
printf( "Hi!\n" );
if( 0 )
printf( "Bye.\n" );

08/27/20 Sarwar Morshed 14


Exercise
What does the following fragment do?
int a;
printf( "Enter a number:" );
scanf( "%d", &a );
if( a = 3 )
printf( "You typed 3.\n" );
What does the following fragment do?
int a=4;
if( a == 3 );
printf( “a is 3.\n" );

08/27/20 Sarwar Morshed 15


Conditional Expressions
Consider the following code:
if (a < 13)
b = 3;
else
b = 18;
C has a construct that lets you encapsulate the
choice as part of the expression assigned to the
variable b. The following code is equivalent:
b = (a < 13) ? 3 : 18;
08/27/20 Sarwar Morshed 16
Conditional Expressions
Examples:-

08/27/20 Sarwar Morshed 17


switch statement
Similar to a chain of if/else statements, but more
restricted in terms of functionality.
Useful when one wants to branch based on the value
of an expression.

The objective of switch statement is to check several


possible constant values for an expression. Its
form/syntax is the following:

08/27/20 Sarwar Morshed 18


THE SELECTIVE STRUCTURE:
switch
switch( expression )
{
case constant1:
statement1;
[break;]
case constant2:
statement2;
[break;]
...
default:
statement;
[break;]
}
08/27/20 Sarwar Morshed 19
Flow Chart of Switch-Case
Statement

08/27/20 Sarwar Morshed 20


Switch Expression

08/27/20 Sarwar Morshed 21


The fall-through property
Missing break
switch( num )
{
case 1:
printf( "Behind Door 1 is nothing.\n" );
case 2:
printf( "Behind Door 2 is a goat.\n" );
break;
case 3:
printf( "Behind Door 3 is a pot of gold.\n" );
break;
}
08/27/20 Sarwar Morshed 22
switch example
switch( month ) {
case 1: case 3: case 5: case 7:case 8: case 10: case 12:
printf( "31 days.\n" );
break;
case 2:
printf( "28 or 29 days.\n" );
break;
default:
printf( "30 days.\n" );
}

08/27/20 Sarwar Morshed 23

You might also like