Problem Solving
Using C
CSA1001
Module-2
Control
Statements
in C
Introduction to C programming
• Operators
• Expressions
• Precedence of Operators
• Associativity
• Expression Evaluation
• Type Casting
• Conditional Statement
• Unconditional Statement
• Looping Statements
• Switch
Operators
• Symbols used to perform some operations on operands
• Arithmetic operators
• Relational operators
Operator Name Description Example
+ Addition Adds together two values x+y
Arithmetic Operators
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
• used to perform arithmetic
operations on operands. / Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by --x
1
Operator Name Example Description
== Equal to x == y Returns 1 if the values are equal
Relational Operators
!= Not equal x != y Returns 1 if the values are not equal
• used to compare two values (or variables).
> Greater than x>y Returns 1 if the first value is greater
than the second value
• Return value is either 0 or 1
< Less than x<y Returns 1 if the first value is less
than the second value
>= Greater than or x >= y Returns 1 if the first value is greater
equal to than, or equal to, the second value
<= Less than or equal to x <= y Returns 1 if the first value is less
than, or equal to, the second value
Operato Name Example Description
r
Logical Operators
&& AND x < 5 && Returns 1 if both
x < 10 statements are true
determine the logic between
variables or values, by combining
|| OR x < 5 || x < Returns 1 if one of the
multiple conditions 4 statements is true
! NOT !(x < 5 && Reverse the result,
x < 10) returns 0 if the result
is 1
Symbol Operator description syntax
Performs bit-by-bit
AND operation
& Bitwise AND a&b
Bitwise Operators
and returns the
result.
Performs bit-by-bit
| Bitwise OR OR operation and a|b
returns the result.
Performs bit-by-bit
to perform bit-level operations^ Bitwise XOR XOR operation and a^b
returns the result.
on the operands
Flips all the set
Bitwise First
~ and unset bits on ~a
Complement
the number.
Shifts the number
in binary form by
<< Bitwise Leftshift one place in the a << b
operation and
returns the result.
Shifts the number
in binary form by
Bitwise
>> one place in the a >> b
Rightshilft
operation and
returns the result.
Symbol Operator Description Syntax
Assign the value of the right operand to
= Simple Assignment a=b
the left operand.
Assignment += Plus and assign
Add the right operand and left operand
and assign this value to the left
operand.
a += b
Operators -= Minus and assign
Subtract the right operand and left
operand and assign this value to the left
operand.
a -= b
Multiply the right operand and left
*= Multiply and assign operand and assign this value to the left a *= b
operand.
Divide the left operand with the right
/= Divide and assign operand and assign this value to the left a /= b
operand.
Assign the remainder in the division of
%= Modulus and assign left operand with the right operand to a %= b
the left operand.
bitwise AND and assigns this value to
&= AND and assign a &= b
the left operand.
Performs bitwise OR and assigns this
|= OR and assign a |= b
value to the left operand.
bitwise XOR and assigns this value to
^= XOR and assign a ^= b
the left operand.
bitwise Rightshift and assign this value
>>= Rightshift and assign a >>= b
to the left operand.
bitwise Leftshift and assign this value
<<= Leftshift and assign a <<= b
to the left operand.
Sizeof Operator
• Determine the size in bytes of a data type or variable.
• unary operator
Operator Example Description
sizeof sizeof(int) Returns the size of an integer.
sizeof sizeof(arr) Returns the size of an array.
Comma Operator ( , )
• binary operator
• evaluates its first operand and discards the result, it then
evaluates the second operand and returns this value (and type).
• lowest precedence of any C operator.
• acts as both operator and separator.
Example: operand1, operand2
Ternary Operator
• Evaluate a condition and return one of two values based on the
result.
• shorthand for an if-else statement.
• condition ? expression1 : expression2
• Example: operand1, operand2
dot (.) and arrow (->) Operators
• used to reference individual members of classes, structures,
and unions.
• dot operator is applied to the actual object.
• arrow operator is used with a pointer to an object
• Example:
• structure_variable . member;
• Structure_variable -> member;
INCREMENT AND DECREMENT OPERATORS
EXPRESSION
• any valid combination of operators, constants and variables
Precedence and Associativity
i. * and / operation has high precedence over +, -. i.e. in an
expression which has mix of operators, the expression
with * and / will be executed first.
ii. If the expression is within the bracket, it will be executed
first.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Problems on operator precedence
& associativity
int x = 10, y = 20, z = 30;
int result = x + y * z / y - z;
printf("%d\n", result);
Problems on operator precedence
& associativity
int a = 5, b = 10, c = 15;
int result = a * b / c + b - a;
printf("%d\n", result);
Problems on operator precedence
& associativity
int x = 2, y = 3, z = 4;
int result = x + y * z / x - y;
Problems on operator precedence
& associativity
int a = 5, b = 10, c = 15;
int result = a > b && b < c || a < c;
printf("%d\n", result);
Problems on operator precedence
& associativity
int x = 5, y = 10, z = 0;
x += y -= z += 2;
printf("%d %d %d\n", x, y, z);
Problems on operator precedence
& associativity
int a = 10, b = 20, c = 30;
int result = a > b ? a : b > c ? b : c;
printf("%d\n", result);
Problems on operator precedence
& associativity
int a = 2, b = 4;
int result = a + b << a * b;
printf("%d\n", result);
TYPE CASTING
• process of converting one data
type to another data type
Implicit Type Casting
• Conversion of lower data type to higher data type will
occur automatically.
• Compiler will do
Explicit Type Casting
• Conversion of higher data type to lower data type
explicitly by the user
BRANCHING
• Decision Making and Branching:
• Simple -if
• if-else
• If-else-if-else(else-if ladder)
• Switch case and
• Nested-if
• Conditional Operator.
SIMPLE if Statement
• The simple if statement has the following syntax:
if (test-expression)
{
statement-block;
}
statement x;
Example
if( age>=18)
{
printf(“ Eligible to vote”);
}
if-else Statement
The if-else statement has the following syntax:
if (test-expression)
{
True-statement-block;
}
else
{
False-statement-block;
}
Statement x;
Example
if ( age>=18)
{
printf(“Eligible to vote”);
}
else
{
printf(“Not eligible to vote”);
}
If-else-if-else (ELSE – IF LADDER)
FLOWCHART OF ELSE-IF LADDER
EXAMPLE
if (code==1)
printf(“colour=“red”);
else if( code ==2)
printf(“colour=“yellow”);
else if(code==3)
printf(“colour=“green”);
else
printf(“invalid input”);
Switch case
FLOWCHART OF SWITCH –CASE STATEMENT
EXAMPLE
Switch(character)
case ‘A’ : printf(“Green”);
break;
case ‘B’: printf(“Yellow”);
break;
case ‘C’: printf(“Red”);
break;
default : printf(“ no choice”);
}
Nested if-else
Example
LOOPING
• While
• Do-While
• For
• Nested looping statements
WHILE STATEMENT
Example
FOR STATEMENT
Example
Do while loop
Example