0% found this document useful (0 votes)
8 views82 pages

ICP Module 2 PDF

The document outlines Module 2 of an Introduction to C Programming course, focusing on operators, expressions, and control statements. It covers various types of operators including arithmetic, relational, logical, and assignment operators, as well as decision-making and looping statements like if, switch, while, and for loops. Additionally, it explains concepts such as type conversion, typecasting, and unconditional branching using break, continue, and goto statements.

Uploaded by

hema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views82 pages

ICP Module 2 PDF

The document outlines Module 2 of an Introduction to C Programming course, focusing on operators, expressions, and control statements. It covers various types of operators including arithmetic, relational, logical, and assignment operators, as well as decision-making and looping statements like if, switch, while, and for loops. Additionally, it explains concepts such as type conversion, typecasting, and unconditional branching using break, continue, and goto statements.

Uploaded by

hema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

RajaRajeswari College of Engineering

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Autonomous institution, Under Visvesvaraya Technological University“Jnana Sangama”, Belgaum
– 560 018
FACULTY: Ms. Thilagavalli S
Subject Name: “Introduction to C programming ”
Subject Code: “B24ESCK245”
“Module-2”
Semester: II
Section: ECE A, B
MODULE 2
Operators and Expressions: Expressions and Arithmetic Operators, Relational and Logical
Operators, Conditional operator, size of operator, Assignment operators and Bitwise Operators, Operators
Precedence, Type conversion and typecasting.
Decision control and looping statements: Statements‐Selection statements (Decision Making)-
if and switch statements with examples, iterative statements (loops)- for, while, do‐while statements with
examples, Unconditional statements- break, continue, goto statements with examples.
2. Module (Operators in C)
• OPERATORS AND EXPRESSIONS

• An operator is a symbol which helps the user to command the computer to do a certain
mathematical or logical manipulations.

1.Arithmetic operators
2.Relational Operators
3.Logical Operators
4.Assignment Operators
5.Increments and Decrement Operators
6.Conditional Operators
7.Bitwise Operators
8.Special Operators
Operators in C
• 1. Arithmetic Operators
• The operators which are used to perform arithmetic operation such as addition,
subtraction, multiplication, division and modulus operation are called arithmetic operators.

• The operator and meaning of the operator is shown below:


+ addition
- subtraction
*multiplication

/ division
% modulus
• Example:
c=a+b;
d=e-f;
Operators in C (Extra)
• The arithmetic operator is classified into three groups:
A.Integer Arithmetic
• When an arithmetic operation is performed on two whole numbers or integers then this
operation is called as integer arithmetic. It always gives an integer as the result.

Example:
• Let x = 32 and y = 5 be TWO integer numbers then x + y = 37
• x – y = 27
B.Floating point arithmetic
• When an arithmetic operation is performed on two real numbers or fraction numbers
then this operation is called floating point arithmetic.
Example:
• Let x = 24.0 and y = 4.0 be TWO real numbers then x + y = 28.0
• x – y = 20.0
Operators in C (Extra)

C.Mixed mode arithmetic


• If the arithmetic operation is performed on ONE real number and ONE integer number
then it is called as mixed mode arithmetic.
• The result of the mixed mode arithmetic operation is always real number.
• Example:
Let x=55 and y=10.0 then
x/y => 55/10.0 = 5.5
Operators in C
• 2. Relational Operators
• Relational Operators are used to compare the relationship between operands
and bring out a decision.


Operators in C
3.Logical Operators
 These operators are used to perform logical operations on the given expressions and
produce the results.
 There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and
logical NOT (!).
Operators in C
Operators in C
4.Assignment Operators

• In C programs, values for the variables are assigned using assignment operators.

• For example, if the value “20” is to be assigned for the variable “sum”, it can be assigned as

sum = 20;”

• There are 2 categories of assignment operators in C language. They are,


1.Simple assignment operator (Example: =)

2.Compound assignment operators (Example: +=, -=, *=, /=, %=, &=, ^=)
Operators in C
Operators in C
5.Increments and Decrement Operators
• Increment operators are used to increase the value of the variable by one
and decrement operators are used to decrease the value of the variable by
one in C programs.

• Syntax:
• Increment operator: ++var_name; (or) var_name++;

• Decrement operator: – -var_name; (or) var_name – -;

• Example:
• Increment operator : ++ i ; i ++ ;

• Decrement operator : – – i ; i––;


Operators in C
Operators in C
6.Conditional Operators

• Conditional operators return one value if condition is true and returns


another value is condition is false.
• This operator is also called as ternary operator.

• Syntax : (Condition? true_value: false_value);

• Example: (A > 500 ? 0 : 1);


• In above example, if A is greater than 500, 0 is returned else 1 is returned.
• This is equal to if else conditional statements.
Operators in C
Operators in C
7.
Example:
#include <stdio.h>
int main() {
int a = 5; // binary: 0101
int b = 3; // binary: 0011

printf("a = %d, b = %d\n", a, b);

// Bitwise AND
printf("a & b = %d\n", a & b); // 0101 & 0011 = 0001 → 1

// Bitwise OR
printf("a | b = %d\n", a | b); // 0101 | 0011 = 0111 → 7
Operators in C
8. Special Operators
Below are some of the special operators that the C
programming language offers.
EXPRESSIONS
The Expressions are combinations of variables, constants, and operators that are evaluated to produce a value.
Here are some common types of expressions in C:

1. Arithmetic Expressions
These involve arithmetic operators like +, -, *, /, and %.
EXAMPLE:
int a = 10;
int b = 5;
int sum = a + b; // sum is 15
int difference = a - b; // difference is 5
int product = a * b; // product is 50
int quotient = a / b; // quotient is 2
int remainder = a % b; // remainder is 0
3. Logical Expressions
These involve logical operators like &&, ||, and !.
EXAMPLE:
5. Conditional Expressions
bool a = true; These use the ternary operator ? : to evaluate a
bool b = false; condition and return one of two values.
bool resultAnd = a && b; // resultAnd is false EXAMPLE:
bool resultOr = a || b; // resultOr is true int a = 10;
bool resultNot = !a; // resultNot is false int b = 20;
int max = (a > b) ? a : b; // max is 20
4. Assignment Expressions These are some of the fundamental types of
expressions in C. They form the building blocks
These involve the assignment operator = and
for more complex operations and logic in your
compound assignment operators like +=, -=, *=, /=, and %=. programs. If you have any specific questions or
EXAMPLE: need further examples, feel free to ask!
int num = 10;
num += 5; // num is now 15
num -= 3; // num is now 12
num *= 2; // num is now 24
num /= 4; // num is now 6
num %= 5; // num is now 1
Type Conversion and type casting
Type Conversion and type casting

c
Decision control and looping statements:
What are Selection Statements in C?
• Selection statements are used to make decisions and execute different blocks of code
based on conditions.
• These statements are called as conditional branching statements and Decision
making statement.

• Definition: The statements that transfer the control from one statement to other
statement in the program with or without any condition are called branch statements.

• The branching statements are classified in two types


1. Conditional branch statements
2. Unconditional branch statements
Conditional branching statements
1. Conditional branch statements:
• Definition: The statements that transfer the control from one
statement to other statement in the program with any condition are
called conditional branching statements.

• They are also called as Selection statements or decision statements.


Conditional branching statements

• 1. if statement

• The if statement evaluates (verify) the condition inside the parenthesis.

• If the condition is evaluated to true (non-zero),

• statements inside the body of if is executed.

• If the condition is evaluated to false (0),

• statements inside the body of if are skipped from execution.


Conditional branching statements

• Definition: When a set of statements have to be executed when an expression is


evaluated to true (non-zero value) or when a set of statements have to be
skipped when an expression is evaluated to false (zero),then if statement is used.

• It is used when we have only one alternative. Hence it is also called one-way
decision statement.
Conditional branching statements
Conditional branching statements
Conditional branching statements (Extra)
Conditional branching statements
Conditional branching statements (Extra)
Conditional branching statements (Extra)
Conditional branching statements
• 2. if-else statement :
• The if-else statement executes statement1 if the condition is true (nonzero) and
statement2 if the condition is false (0).
Conditional branching statements
Example Program:
#include<stdio.h>
void main()
{
int n;
printf(“Enter any non-zero integer: \n”) ;
scanf(“%d”, &n)
if(n%2==0)
{
printf(“Number is EVEN number”);
}
else
{
printf(“Number is ODD number”);
}
}
Conditional branching statements
• 3. Nested if-else statement
• In C programming to nest if-else statements, which means you can use one if or else if
statement inside another if or else if statement(s)
Conditional branching statements
Flowchart
Conditional branching statements
Example Program:
#include <stdio.h>
int main() {
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
// outer if statement
if (a >= b)
{
// inner if...else
if (a >= c)
printf("%f is the largest number.", a);
else
printf("%f is the largest number.", b);
}
Conditional branching statements

// outer else statement


else {
// inner if...else
if (b >= c)
printf("%f is the largest number.", b);
else
printf("%f is the largest number.", c);
}
}
Conditional branching statements (Extra)
Conditional branching statements
• 4. else-if ladder statement
• The else-if ladder statement allows you to check for multiple conditions and execute
different codes for more than two conditions.
Conditional branching statements

Flowchart
Conditional branching statements
Example Program:
#include<stdio.h>
void main ( )
{
int b;
printf(“Enter any integer:”) ;
scanf(“%d”, &b)
if(b>0)
{
printf ("Number is Positive");
}
else if(b< 0)
{
printf ("Number is Negative");
}
Conditional branching statements

else if(b== 0)
{
printf ("Number is Zero");
}
else
{
printf ("Invalid input");
}
}
Conditional branching statements
• 5. SWITCH STATEMENT

• Switch statement is a control statement that allows us to choose only one choice among the many given
choices.

• The expression in switch evaluates to return an integral value, which is then compared to the values present
in different cases.

• It executes that block of code which matches the case value.

• If there is no match, then default block is executed.


Conditional branching statements
Conditional branching statements
Conditional branching statements
Example Program:
#include<stdio.h>
void main( )
{
int a,b,c,choice;
clrscr( );
printf("\n 1. Press 1 for addition");
printf("\n 2. Press 2 for subtraction");
printf("\n Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:

printf("Enter 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",c);
break;
Conditional branching statements

case 2:

printf("Enter 2 numbers");
scanf("%d%d",&a,&b);
c=a-b;
printf("%d",c);
break;
default:

printf("you have passed a wrong key");


}
getch();
}
Conditional branching statements (Extra)
Iterative statements

• A set of statements have to be repeatedly executed for a specified number of


times until a condition is satisfied.

• The statements that help us to execute the set of statements repeatedly


are called as looping constructs or loop control statements.

• The various looping constructs in C are:


Iterative statements
1) while Loop: It is an entry-controlled loop.
• In while loop, a condition is evaluated before processing a body of the loop.

• If a condition is true then only the body of a loop is executed.

• After the body of a loop is executed then control again goes back at the beginning, and
the condition is checked if it is true.

• the same process is executed until the condition becomes false.

• Once the condition becomes false, the control goes out of the loop.

• After exiting the loop, the control goes to the statements which are immediately after
the loop.
Iterative statements
Iterative statements
• while loop syntax explanation:
• while loop has 3 steps.
• Variable initialization.( e.g int n=0;)
• condition( e.g while( n<=10))
• Variable increment or decrement ( n++ or n-- or n=n+2 )
Iterative statements (Extra)
Iterative statements (Extra)
Iterative statements
2) do-while loop :

• It is also called an exit-controlled loop.

• The body is executed if and only if the condition is true.

• In some cases, we have to execute a body of the loop at least once even if the
condition is false.

• In the do-while loop, the body of a loop is always executed at least once.
Iterative statements
• After the body is executed, then it checks the condition.

• If the condition is true, then it will again execute the body of a loop otherwise
control is transferred out of the loop.

• Once the control goes out of the loop the statements which are immediately
after the loop is executed.
Iterative statements
Iterative statements
Iterative statements
Iterative statements
Iterative statements
Iterative statements
3) For loop:

• A for loop is a control statement using which the programmer can give
instructions to the computer to execute a set of statements repeatedly for a
specified number of times.

• Once the specified number of times the loop is executed , the control comes out
of the loop.
Iterative statements
Iterative statements
Iterative statements (Extra)
Iterative statements (Extra)
Iterative statements (Extra)
• Comparison of while loop and for loop:
Nested Loops[EXTRA]
• Nested loop means a loop statement inside another loop statement. That is why
nested loops are also called as “loop inside loop“.

• When the control moves from outer loop to inner loop.

• the control remains in the inner loop until the inner loop condition fails.

• once the condition fails the control continues with the outer loop condition.

• Again when the control comes to inner loop the inner loop is reset to the initial
value.

• The Nested for loop stops execution when the outer for loop condition fails.
Nested Loops
Nested Loops
UnConditional branching statements

2. Unconditional branch statements:


• Definition: The statements that transfer the control from one statement to other
statement in the program without any condition are called conditional branching
statements.
Break and continue statements

Break Statement
Break and continue statements
Break and continue statements
Continue Statement
Break and continue statements
goto statement

where,
goto statement
Example:2

#include <stdio.h>

int main() {
int num = 5;

if (num > 0) {
goto positive; // Jump to the label "positive" In this example:
}
•If the condition num > 0 is true, the program
negative: jumps to the positive label and executes that
printf("The number is negative.\n"); block of code.
return 0;
•Otherwise, the code after the goto statement will
positive:
execute.
printf("The number is positive.\n");
return 0;
}

Output: The number is positive.


Example: 3 Output:
i = 0, j = 0
#include <stdio.h> i = 0, j = 1
i = 0, j = 2
int main() { i = 1, j = 0
for (int i = 0; i < 3; i++) { i = 1, j = 1
for (int j = 0; j < 3; j++) { i = 1, j = 2
if (i == 2 && j == 1) { i = 2, j = 0
goto end; // Exit the nested loops i = 2, j = 1
} Exited the nested loops.
printf("i = %d, j = %d\n", i, j);
} Expalanation:
}
The loops iterate as follows:
end: •When i = 0, j runs from 0 to 2.
printf("Exited the nested loops.\n"); •When i = 1, j runs from 0 to 2.
•When i = 2 and j = 1, the condition i == 2 && j == 1
return 0; is true.
} •The goto end; statement causes the program to jump
to the end: label, skipping any remaining iterations.
int main() {
Extra…….[for loop difference]
int main() {
for (int i = 0; i < 3; i++) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j < 3; j++) {
for (int j = 0; j <= i; j++) {
printf("i = %d, j = %d\n", i, j);
Printf("1");
}
}}
}
OUTPUT: }
i = 0, j = 0 }
i = 0, j = 1
i = 0, j = 2 OUTPUT:
i = 1, j = 0 1
i = 1, j = 1 11
i = 1, j = 2 111
i = 2, j = 0 1111
i = 2, j = 1
i = 2, j = 2
Explanation: Explanation:

1)Outer loop (i): 1)Outer loop (i):


•Runs from 0 to 2 (i < 3). •Runs from 0 to 3 (i <= 3).
•For each single value of i, the inner loop (j) •For each value of i, the inner loop (j) runs
completes all of its iterations (j = 0 to 2) only up to the current value of i (j <= i).
before moving on to the next value of i.
2)Inner loop (j):
2)Inner loop (j): •The range of j depends on the current value
•Always runs independently from 0 to 2 (j < 3), of i. For example:
no matter what the value of i is. •If i = 0, j runs from 0 to 0.
•Once the inner loop finishes, the control goes •If i = 1, j runs from 0 to 1.
back to the outer loop to increment i •If i = 2, j runs from 0 to 2.
•If i = 3, j runs from 0 to 3.

You might also like