CSA1001 Module2
CSA1001 Module2
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
Arithmetic Operators
- Subtraction Subtracts one value from another x-y
Relational Operators
!= Not equal x != y Returns 1 if the values are not equal
<= 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
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.
• 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
• 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”);
printf(“colour=“yellow”);
else if(code==3)
printf(“colour=“green”);
else
printf(“invalid input”);
Switch case
FLOWCHART OF SWITCH –CASE STATEMENT
EXAMPLE
Switch(character)
break;
break;
break;
}
Nested if-else
Example
LOOPING
• While
• Do-While
• For