Nothing Special   »   [go: up one dir, main page]

Week 1 Self Learning Materials

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

UNIT-2

OPERATORS, CONDITIONAL STATEMENTS, ARRAYS


Unit-2: Operators, Conditional Statements and Arrays
Relational and logical Operators- Character and Numbers: Manipulation- Expressions with
pre / post increment operator- Expression with conditional and assignment operators- Ternary
operator- L value and R value in expression- Operator precedence-Type Conversion- Control
Statements: sequential, branching, looping and jump- If, if ..Else, else if ladder, nested if, switch case,
for loop, while loop, do while, goto, break, continue, exit: Jump statements, Understanding jump
statements with branch and iterative statements, Array Basic, Array Declaration, Initialization, Types,
Manipulating one dimensional arrays with indices, Methods: sort, append, reverse, traverse,
Manipulating two dimensional arrays with indices.
Week 1

Learning Objectives:

1. Understand the operators and types of operators.

2. Steps that are undertaken by a programmer to solve an expression.

3. Understand the operator precedence concepts and arrays.

4. Understand the use of looping, branching and selection statements

Key Topics

1.1 Operator

1.1.1 Relational Operator

1.1.2 Logical Operator

1.2 Character and Number Manipulation

1.3 Expressions

1.3.1 Expressions with pre / post increment operator

1.3.2 Expression with conditional and assignment operators

1.4 Ternary Operator

1.5 L value and R value in expression

1.6 Operator

1.6.1 Operator Precedence and Type Conversion

1.6 Lab Exercise -1


1.1 Operators

An operator is a symbol that operates on a value or a variable. The types of operators are:

 Arithmetic Operators
 Increment and Decrement Operator
 Relational Operator
 Logical Operator
 Assignment Operator
 Bitwise Operator
 Other Operator (Comma, sizeof operator)

1.1.1 Relational Operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1;
if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5!= 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

// Sample Program
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

1.1.2 Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.

Operator Meaning Example

Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5) &&


&& operands are true (d>5)) equals to 0.

Logical OR. True only if either If c = 5 and d = 2 then, expression ((c==5) ||


|| one operand is true (d>5)) equals to 1.

Logical NOT. True only if the


! operand is 0 If c = 5 then, expression!(c==5) equals to 0.
// Sample program

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

1.2 Manipulation: Character and Number Manipulation

Character arithmetic is used to implement arithmetic operations like addition and subtraction on
characters in C language. It is used to manipulate the strings. When the characters are used with the
arithmetic operations, it converts them into integer value automatically i.e. ASCII value of characters.

Example:

#include <stdio.h>

int main(){

char s = 'm';

char t = 'z' - 'y';


printf("%d\n", s);

printf("%c\n", s);

printf("%d\n", (s+1));

printf("%c\n", (s+1));

printf("%d\n", (s-1));

printf("%c\n", (s-1));

printf("%d\n", t);

// printf("%c", t);

return 0;

Output:

109

110

108

1.3 Expression

Expression evaluation is mainly depending on priority and associativity. An expression is a


sequence of operands and operators that reduces to a single value.
An expression is a combination of variables constants and operators written according to the syntax of
C language. Every expression result in some value of a certain type that can be assigned to a variable.

Priority This represents the evaluation of expression starts from "what" operator.
Associativity It represents which operator should be evaluated first if an expression is containing
more than one operator with same priority.

Example: Expression Evaluation

1.3.1 Pre-increment and Post-increment Expression

C programming has two operators increment ++ and decrement -- to change the value of an operand
(constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the
value by 1. These two operators are unary operators, meaning they only operate on a single operand.

// Sample Program

#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0; }
Output:

++a = 11
--b = 99
++c = 11.500000
++d = 99.500000

1.3.2 Expression with conditional and assignment operator

Example

The following expression determines which variable has the greater value, y or z, and assigns the
greater value to the variable x:
x = (y > z)? y:z;

The following statement is equivalent to the previous expression.

if (y > z)

x = y;

else

x = z;

1.4 Ternary Operator

The conditional operator is also known as a ternary operator. The conditional statements are the
decision-making statements which depends upon the output of the expression. It is represented by two
symbols, i.e., '?' and ':'.

As conditional operator works on three operands, so it is also known as the ternary operator.

The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also
a decision-making statement.

Syntax of a conditional operator

Expression1? expression2: expression3;

The pictorial representation of the above syntax is shown below:

Meaning of the above syntax.

o In the above syntax, the expression1 is a Boolean condition that can be either true or false
value.
o If the expression1 results into a true value, then the expression2 will execute.
o The expression2 is said to be true only when it returns a non-zero value.
o If the expression1 returns false value then the expression3 will execute.
o The expression3 is said to be false only when it returns zero value.

Program 1

#include <stdio.h>

int main()

int age; // variable declaration

printf("Enter your age");

scanf("%d",&age); // taking user input for age variable

(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditional operator

return 0;

Explanation:

In the above code, we are taking input as the 'age' of the user. After taking input, we have applied the
condition by using a conditional operator. In this condition, we are checking the age of the user. If the
age of the user is greater than or equal to 18, then the statement1 will execute, i.e., (printf ("eligible
for voting")) otherwise, statement2 will execute, i.e., (printf("not eligible for voting")).

Program 2:

#include <stdio.h>

int main()

int a=5,b; // variable declaration

b=((a==5)?(3):(2)); // conditional operator

printf("The value of 'b' variable is : %d",b);

return 0;
}

Explanation:

In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the 'a'
variable. After the declaration, we are assigning value to the 'b' variable by using the conditional
operator. If the value of 'a' is equal to 5 then 'b' is assigned with a 3 value otherwise 2.

1.5 L value and R value in expressions

L-value: “l-value” refers to memory location which identifies an object. L-value may appear as either
left hand or right hand side of an assignment operator (=). L-value often represents as identifier.
Expressions referring to modifiable locations are called “modifiable l-values“. A modifiable l-value
cannot have an array type, an incomplete type, or a type with the const attribute. For structures and
unions to be modifiable L-values, they must not have any members with the const attribute. The name
of the identifier denotes a storage location, while the value of the variable is the value stored at that
location.

An identifier is a modifiable L-value if it refers to a memory location and if its type is arithmetic,
structure, union, or pointer. For example, if ptr is a pointer to a storage region, then *ptr is a
modifiable l-value that designates the storage region to which ptr points.

In C, the concept was renamed as “locator value”, and referred to expressions that locate (designate)
objects. The L-value is one of the following:

 The name of the variable of any type i.e, an identifier of integral, floating, pointer, structure,
or union type.

 A subscript ([ ]) expression that does not evaluate to an array.

 A unary-indirection (*) expression that does not refer to an array

 An l-value expression in parentheses.

 A const object (a nonmodifiable l-value).

 The result of indirection through a pointer, provided that it isn’t a function pointer.

 The result of member access through pointer(-> or .)// declare a an object of type 'int'

int a;
a = 1;

int b = a; // Ok, as l-value can appear on right

// Switch the operand around '=' operator

9 = a;

R-value: r-value” refers to data value that is stored at some address in memory. A r-value is an
expression that can’t have a value assigned to it which means r-value can appear on right but not
on left hand side of an assignment operator(=)

1.6 Operator Precedence Chart:

Precedence Operator Description Associativity

++ -- Suffix/postfix increment and decrement Left-to-right

() Function call

[] Array subscripting

. Structure and union member access

-> Structure and union member access through pointer

(type){list} Compound literal(C99)

2 ++ -- Prefix increment and decrement Right-to-left


+ - Unary plus and minus

! ~ Logical NOT and bitwise NOT

(type) Type cast

* Indirection (dereference)

& Address-of

sizeof Size-of

3 * / % Multiplication, division, and remainder Left-to-right

4 + - Addition and subtraction

5 << >> Bitwise left shift and right shift

< <= For relational operators < and ≤ respectively

> >= For relational operators > and ≥ respectively

7 == != For relational = and ≠ respectively

8 & Bitwise AND


9 ^ Bitwise XOR (exclusive or)

10 | Bitwise OR (inclusive or)

11 && Logical AND

12 || Logical OR

13 ?: Ternary conditional Right-to-Left

= Simple assignment

+= -= Assignment by sum and difference

14 *= /= %= Assignment by product, quotient, and remainder

<<= >>= Assignment by bitwise left shift and right shift

&= ^= |= Assignment by bitwise AND, XOR, and OR

15 , Comma Left-to-right

1.6.2 Type conversion


Converting one data type into another is known as type casting or, type-conversion. For
example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'.
You can convert the values from one type to another explicitly using the cast operator as follows:
Program:

#include <stdio.h>

main() {

int sum = 17, count = 5;

double mean;

mean = (double) sum / count;

printf("Value of mean : %f\n", mean );

Output:

Value of mean : 3.400000

References

1) https://www.javatpoint.com/c-array

2) https://beginnersbook.com/

3) Let Us C by Yashavant Kanetkar

4) C: The Complete Reference by Herbert Schildt

5) Computer Fundamentals and Programming in C by Reema Thareja

6) C in a Nutshell, 2e by Peter Prinz, Tony Crawford

You might also like