Programming in C Unit-II BCA Semester I
1
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Unit-II: Input and Output, Operators, Control Structures/Statements
Introduction to Input and Output in C
• In C programming, input refers to the data a program receives from the user or an external
source, while output refers to the information a program displays on the screen or sends to
another device.
• By default, C uses standard input (stdin), which is usually the keyboard, and standard
output (stdout), which is usually the display screen. These streams allow the program to
interact with the outside world effectively.
Importance of I/O
• Every program needs to work with data from the user, files, or devices.
• I/O functions enable communication between the program and its environment.
• They help in reading user commands and displaying meaningful results.
• I/O is essential for debugging, as output helps monitor program behavior.
• Many real-world applications (billing systems, games, databases) rely heavily on I/O for
functionality.
Types of I/O Functions in C
C provides a wide range of functions to handle data input and output. These are classified as:
1. Formatted I/O Functions: Input/output done using format specifiers (printf(), scanf() )
2. Unformatted I/O Functions: Input/output done without any format. ( getchar(),
putchar(), gets(), puts())
Programming in C Unit-II BCA Semester I
2
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
I)Formatted I/O functions
• Formatted I/O functions allow data to be input or displayed in a specific format using
format specifiers.
• The two main formatted I/O functions in C are printf() and scanf().
1) printf() – Formatted Output in C
• printf() is used to display output on the screen. It prints text, variables, and results in a
formatted manner using format specifiers like %d, %f, %c, and %s.
• Header file required: #include <stdio.h>
Syntax:
printf("format string", variables);
The format string can include:
• Plain text
• Format specifiers for variables (%d for integer, %f for float, %c for character.)
• Escape sequences (like n for newline)
Example:
OUTPUT:
Programming in C Unit-II BCA Semester I
3
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
2. scanf() – Formatted Input in C
• scanf() is used to read input from the user. It accepts values in a specified format and
stores them in variables.
• Header file required: #include <stdio.h>
Syntax:
scanf("format string", &variables);
• The format string contains format specifiers to indicate the type of input.
• The & (address-of operator) is used to store the value in the variable (except for strings).
• scanf() reads the value typed by the user and assigns it to the given variable.
Example:
Programming in C Unit-II BCA Semester I
4
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
SAMPLE INPUT:
OUTPUT:
Comparison of printf() and scanf() in C
printf() scanf()
1. Used for displaying (output) data on the screen. 1. Used for reading (input) data from the user.
2. Formatted output function. 2. Formatted input function.
3. Data flows from Program → Screen. 3. Data flows from Keyboard → Program.
4. Uses format specifiers to print values (%d, %f, %s). 4. Uses format specifiers to read values.
5. Does not require the & operator. 5. Requires & operator for variables (except strings).
6. Only displays output; no input validation. 6. Returns number of inputs successfully read.
7. Example: printf("Age = %d", age); 7. Example: scanf("%d", &age);
Programming in C Unit-II BCA Semester I
5
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Control Strings and Escape Sequences in C
• Control strings and escape sequences in C are special character combinations used inside
printf() and scanf() to control how data is formatted, displayed, or interpreted.
• They help manage output formatting and represent characters that cannot be typed directly.
1)Control Strings (Format Strings)
• Control strings are the text written inside double quotes in formatted I/O functions such
as printf() and scanf().
• They tell the compiler how the data should be displayed (output) or read (input).
A control string contains:
• Format specifiers (like %d, %f, %c, %s)
• Escape sequences (like n, t)
• Normal characters and symbols
Common Control Strings
Control String Meaning Example Output
%d Prints an integer printf("Age: %d", 25); Age: 25
%f Prints a float printf("Pi: %f", 3.14); Pi: 3.140000
%.2f Prints float with 2 decimal
places
printf("Pi: %.2f", 3.14); Pi: 3.14
%c Prints a character printf("Grade: %c", 'A'); Grade: A
%s Prints a string printf("Name: %s", "John"); Name: John
Example:
OUTPUT:
Programming in C Unit-II BCA Semester I
6
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
2)Escape Sequences
• Escape sequences are special character combinations that begin with a backslash (). They
are used inside strings to control formatting, spacing, and printing of non-printable
characters.
• Used mainly in output formatting with printf().
Common Escape Sequences
Escape Sequence Meaning Example Code Output
n New line printf("HellonWorld"); Hello World
t Horizontal tab printf("AtB"); A B
v Vertical tab printf("XvY"); X Y
 Backslash  printf(""); 
" Double quote " printf(""C is fun""); "C is fun"
b Backspace printf("Hellob!"); Hell!
r Carriage return printf("HellorHi"); Hi (overwrites)
Example
OUTPUT:
Programming in C Unit-II BCA Semester I
7
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Comparison: Control Strings vs Escape Sequences in C
Control Strings Escape Sequences
1. Used in printf() and scanf() to control
input/output format.
1. Used inside strings to represent special
characters.
2. Include format specifiers like %d, %f, %s,
%c.
2. Begin with a backslash such as n, t, .
3. Tell the compiler how data should be printed
or read.
3. Tell the compiler how text should be
displayed.
4. Used mainly for formatted input and output
operations.
4. Used mainly for controlling text layout on
screen.
5. Examples: %d (int), %f (float), %c (char). 5. Examples: n (new line), t (tab), b
(backspace).
6. Do not affect screen spacing directly. 6. Directly affect display formatting like line
breaks and tabs.
II)Unformatted I/O functions
• Unformatted I/O functions in C read and write data in raw form without using format
specifiers. They handle input and output directly as characters or strings.
• Examples: getchar(), putchar(), gets(), puts()
1. getchar() Unformatted Input
• getchar() is an unformatted input function in C that reads one character at a time from
the standard input (keyboard).
• It is useful when you need character-by-character input. Unlike scanf(), it does not use any
format specifiers.
Syntax:
int getchar(void);
Programming in C Unit-II BCA Semester I
8
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
getchar() returns the ASCII value of the character entered. Its return type is int so that it can also
represent EOF (End of File).
Example
SAMPLE INPUT:
OUTPUT:
2. putchar() Unformatted Output
• putchar() is an unformatted output function in C that prints one character at a time to the
standard output (screen).
• It is commonly used for character-by-character output and often works together with
getchar().
Syntax:
int putchar(int ch);
Programming in C Unit-II BCA Semester I
9
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example
OUTPUT:
3. gets() unformatted input in C
• gets() is an unformatted input function in C used to read an entire line of text from the
keyboard.
• It stores the input in a character array and reads characters until a newline (n) is
entered, making it possible to read strings with spaces.
Syntax
char* gets(char *str);
• str → character array where input will be stored.
• Returns str on success, NULL on error.
Programming in C Unit-II BCA Semester I
10
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example
OUTPUT:
4. puts() unformatted output in C
• puts() is an unformatted output function in C used to print a string (character array) to
the screen.
• Unlike printf(), it automatically adds a newline (n) after printing the string.
Syntax
int puts(const char *str);
• str → the string to be displayed.
• Returns a non-negative value on success, EOF on error.
Programming in C Unit-II BCA Semester I
11
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example
OUTPUT:
Comparison of Formatted and Unformatted I/O Functions
Formatted I/O Functions Unformatted I/O Functions
1. Use format specifiers (e.g., %d, %f, %s) to
control the type and format of input/output.
1. Do not use any format specifiers; work
directly with raw characters or strings.
2. Allow precise control over data
representation (width, precision, alignment).
2. Offer no control over formatting; simply
read or write data as it is.
3. Functions include printf(), scanf(). 3. Functions include getchar(), putchar(),
gets(), puts().
4. Used for numbers, characters, strings, and
mixed data types.
4. Mainly used for single characters or
complete strings.
5. More complex and slower due to formatting
and type checking.
5. Faster since there is no formatting
overhead.
6. Provide flexibility for structured or formatted
output (e.g., tables, reports).
6. Suitable for simple tasks like character-
by-character input/output.
Programming in C Unit-II BCA Semester I
12
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Operators in C
Operators in C are special symbols that perform specific operations on variables and values.
They instruct the compiler to carry out tasks such as arithmetic calculation, comparison,
assignment, or logical processing.
Example
Let a = 10, b = 5
c = a + b;
.
Operators Used
1. Arithmetic Operator (+) → adds a and b → 10 + 5 = 15.
2. Assignment Operator (=) → assigns the result 15 to variable c.
Final Result: c = 15
Classification of Operators in C
In C programming, operators are classified based on the number of operands they work on. They
are grouped into:
1. Binary Operators – operate on two operands
2. Unary Operators – operate on one operand
3. Ternary Operators – operate on three operands
Programming in C Unit-II BCA Semester I
13
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
I. Binary Operators
A binary operator is an operator that acts on two operands to perform operations such as
arithmetic, comparison, or logical evaluation.
1. Arithmetic Binary Operators
Arithmetic binary operators perform basic mathematical operations like addition, subtraction,
multiplication, and division on two operands.
Operator Name Description Example
(a=10, b=3)
Result
+ Addition Adds two operands a + b 13
- Subtraction Subtracts right operand from left operand a - b 7
* Multiplication Multiplies two operands a * b 30
/ Division Divides left operand by right operand a / b 3
% Modulus Gives remainder of division a % b 1
C Program Example
Programming in C Unit-II BCA Semester I
14
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
2. Relational Binary Operators
Relational binary operators are used to compare two operands. They return a boolean result:
true (1) if the condition is satisfied, and false (0) otherwise.
Operator Name Description Example
(a = 10, b = 5)
Result
== Equal to Checks if two operands are equal a == b 0 (false)
!= Not equal to Checks if two operands are not equal a != b 1 (true)
> Greater than Checks if left operand is greater than
right
a > b 1 (true)
< Less than Checks if left operand is less than right a < b 0 (false)
>= Greater or
equal
Checks if left operand is greater or
equal
a >= b 1 (true)
<= Less or equal Checks if left operand is less or equal a <= b 0 (false)
C Program Example
Programming in C Unit-II BCA Semester I
15
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
3. Logical Binary Operators
Logical binary operators are used to combine two relational conditions. They return true (1) if
the combined condition is satisfied; otherwise, they return false (0).
Operator
Symbol
Symbol Description Example Result
&& Logical
AND
True if both
operands are true
(a > 0) && (b < 5) 1 if both true, 0 otherwise
|| Logical OR True if at least one
operand is true
(a > 0) || (b < 5) True if at least one
operand is true
C Program Example
OUTPUT:
Programming in C Unit-II BCA Semester I
16
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
4. Bitwise Binary Operators
Bitwise operators in C work bit by bit on integer values. They directly manipulate the individual
bits of the operands.
Operator Symbol Operator Name Example / Description / Result
& Bitwise AND 6 & 3 → (110 & 011) = 010 → 2
| Bitwise OR 6 | 3 → (110 & 011) = 111 → 7
^ Bitwise XOR 6 ^ 3 → (110 ^ 011) = 101 → 5
<< Left Shift 6 << 1 → (110 << 1) = 1100 → 12
>> Right Shift 6 >> 1 → (110 >> 1) = 11 → 3
C Program Example
OUTPUT:
Programming in C Unit-II BCA Semester I
17
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
5. Assignment Binary Operators
• Assignment operators in C are binary operators that assign values to variables.
• They can also perform an operation (like addition, subtraction, etc.) and assign the result
back to the variable in a single step.
Operator Symbol Operator Name Example Explanation
= Simple Assignment a = b; Assigns value of b to a.
+= Add and Assign a += b; Equivalent to a = a + b.
-= Subtract and Assign a -= b; Equivalent to a = a - b.
*= Multiply and Assign a *= b; Equivalent to a = a * b.
/= Divide and Assign a /= b; Equivalent to a = a / b.
%= Modulus and Assign a %= b; Equivalent to a = a % b.
<<= Left Shift and Assign a <<= 1; Equivalent to a = a << 1.
>>= Right Shift and Assign a >>= 1; Equivalent to a = a >> 1.
&= Bitwise AND and Assign a &= b; Equivalent to a = a & b.
|= Bitwise OR and Assign a|=b; Equivalent to a = a | b.
^= Bitwise XOR and Assign a ^= b; Equivalent to a = a ^ b.
Programming in C Unit-II BCA Semester I
18
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
C Program Example
OUTPUT:
Programming in C Unit-II BCA Semester I
19
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
II. Unary Operators
• A unary operator in C is an operator that operates on only one operand.
• It is used to perform operations like increment, decrement, negation, size determination,
etc.
1. Unary Arithmetic Operators
Operator
Symbol
Operator
Name
Example (Let a=5) Explanation
+ Unary Plus +a → +5 Represents a positive value (usually has no
effect, just indicates positivity).
- Unary Minus -a → -5 Negates the value of the operand (changes
sign).
++a Pre-
increment
a=5; ++a; → 6 Increases a by 1 before using it in an
expression.
a++ Post-
increment
a=5; a++; → 5 (then
a=6)
Uses a first, then increases it by 1 after
evaluation.
--a Pre-
decrement
a=5; --a; → 4 Decreases a by 1 before using it in an
expression.
a-- Post-
decrement
a=5; a--; → 5 (then
a=4)
Uses a first, then decreases it by 1 after
evaluation.
Note:
▪ Pre (++a / --a): Change happens before the value is used.
▪ Post (a++ / a--): Change happens after the value is used.
Programming in C Unit-II BCA Semester I
20
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
C Program Example
OUTPUT:
Programming in C Unit-II BCA Semester I
21
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
2. Unary Logical Operator
A unary logical operator works on a single operand. In C, the only unary logical operator is the
logical NOT (!), which reverses the truth value of an expression.
It inverts the Boolean value of an expression:
▪ If the operand is non-zero (true) → result is 0 (false).
▪ If the operand is 0 (false) → result is 1 (true).
Operator Name Example Explanation Result (if a=5, b=0)
! Logical NOT !a If a is non-zero → result is 0 !5 = 0
- - !b If b is zero → result is 1 !0 = 1
C Program Example
OUTPUT:
Programming in C Unit-II BCA Semester I
22
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
3. Unary Bitwise Operators
• Unary bitwise operators in C work on the bit-level representation of data.
• They perform operations on each bit of the operand.
Operator Name Example Explanation (for a = 5, i.e.,
binary 00000101)
Result
~ Bitwise
NOT
~a Inverts each bit:
00000101 → 11111010
-6 (in 2’s complement form)
Note:
• For signed integers, the result of ~ depends on the system’s representation (commonly 2’s
complement).
• Example: a = 5 (00000101) → ~a = 11111010 which represents -6 in decimal.
C Program Example
OUTPUT:
Programming in C Unit-II BCA Semester I
23
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Comparison: ~ vs ! in C
Operator Name Works
On
Operation Type Example
(a=5,
b=0)
Result Explanation
~ Bitwise
NOT
Integers Inverts each bit ~5 -6 5 (00000101) → 11111010
= -6 (two’s complement)
! Logical
NOT
Any
scalar
(int,
float,
etc.)
Converts
true→false and
false→true
!5 0 Non-zero is true → !true = 0
!0 1 Zero is false → !false = 1
Key Points:
• ~ → bitwise complement, flips all bits of an integer.
• ! → logical negation, only checks if value is zero or not.
Example C program:
OUTPUT:
Programming in C Unit-II BCA Semester I
24
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
4.Special Unary Operators
• Special unary operators in C operate on a single operand but do not fall strictly under
arithmetic, logical, or bitwise categories.
• They offer additional features such as accessing memory addresses, dereferencing
pointers, or determining the size of data types and variables.
Operator Name Example Explanation
& Address-of Operator p = &a; Returns the address of variable a.
* Value-of /
Dereference
x = *p; Accesses the value stored at the address in
pointer p.
sizeof Size-of Operator sizeof(int) Returns the size (in bytes) of a data type or
variable.
Example C program:
OUPUT:
Programming in C Unit-II BCA Semester I
25
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
III. Ternary/Conditional Operator
The ternary operator is the only operator in C that works with three operands. It is also known as
the conditional operator (? :) and is used as a short, concise alternative to an if–else statement.
Syntax:
(condition) ? expression1 : expression2;
• If condition is true (non-zero) → evaluates expression1.
• If condition is false (zero) → evaluates expression2.
Operator
Symbol
Operator
Name
Example Explanation Result
(if a=10, b=20)
?: Ternary
(Conditional)
(a > b) ? a : b If a > b, result is
a, else result is b.
20
- - (a == b) ? 100 : 200 If a equals b,
result is 100, else
result is 200.
200
- - (a % 2 == 0) ? "Even" : "Odd" If a divisible by 2,
result is "Even",
else result is
"Odd".
"Even"
C Program Example 1:
Programming in C Unit-II BCA Semester I
26
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
C Program Example 2: Biggest Among 3 Floats using Ternary Operator
OUTPUT:
Summary of Operators in C
Programming in C Unit-II BCA Semester I
27
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Operator Precedence and Associativity in C
Precedence and Associativity Table
Precedence Operator Description Associativity
1 () Parentheses (function call)
Left-to-Right
[] Array Subscript (Square Brackets)
. Dot Operator
-> Structure Pointer Operator
++ , -- Postfix increment, decrement
2 ++ / -- Prefix increment, decrement
Right-to-Left
+ / - Unary plus, minus
! , ~ Logical NOT, Bitwise complement
(type) Cast Operator
* Dereference Operator
& Addressof Operator
sizeof Determine size in bytes
3 *,/,% Multiplication, division, modulus Left-to-Right
4 +,- Addition, subtraction Left-to-Right
5 << , >> Bitwise shift left, Bitwise shift right Left-to-Right
6 < , <= Relational less than, less than or equal to Left-to-Right
> , >= Relational greater than, greater than or
equal to
7 == , != Relational is equal to, is not equal to Left-to-Right
8 & Bitwise AND Left-to-Right
9 ^ Bitwise exclusive OR Left-to-Right
10 | Bitwise inclusive OR Left-to-Right
11 && Logical AND Left-to-Right
12 || Logical OR Left-to-Right
13 ?: Ternary conditional Right-to-Left
14 = Assignment
Right-to-Left
+= , -= Addition, subtraction assignment
*= , /= Multiplication, division assignment
%= , &= Modulus, bitwise AND assignment
^= , |= Bitwise exclusive, inclusive OR
assignment
<<=, >>= Bitwise shift left, right assignment
15 , comma (expression separator) Left-to-Right
Programming in C Unit-II BCA Semester I
28
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
1. Operator Precedence
Operator precedence defines the priority level of operators when an expression contains more
than one operator. The operator with higher precedence is evaluated first.
Example:
int x = 10 + 20 * 3;
Here, * has higher precedence than +, so the multiplication happens first:
Evaluation: x = 10 + (20 * 3) = 70.
2. Operator Associativity
Associativity defines the direction of evaluation when two operators of the same precedence
appear in an expression. It can be either left-to-right or right-to-left.
• Left → Right (L→R): Most binary operators ( + - * / % < > == && || , )
• Right → Left (R→L): Unary operators (++ -- ! ~ sizeof), conditional (?:), assignment
(= += -= *= /= %=, etc.)
Example (Left-to-right):
int x = 10 - 5 - 2; // evaluated as (10 - 5) - 2 = 3
Example (Right-to-left):
Assignment operators (=, +=, -=, etc.) follow right-to-left associativity:
a = b = c = 5; // evaluated as a = (b = (c = 5));
Programming in C Unit-II BCA Semester I
29
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
I)Left-to-Right Associativity Examples
Example 1:
#include <stdio.h>
int main()
{
int result = 100 / 10 * 2; // both / and * have same precedence
printf("%dn", result);
return 0;
}
Step-by-step evaluation
• Division and multiplication have same precedence.
• They follow Left → Right associativity.
100 / 10 = 10
10 * 2 = 20
Output: 20
Example 2:
#include <stdio.h>
int main()
{
int result = 50 - 20 + 5; // both - and + have same precedence
printf("%dn", result);
return 0;
}
Step-by-step evaluation
Addition and subtraction have same precedence.
They follow Left → Right associativity.
50 - 20 = 30
30 + 5 = 35
Output: 35
Programming in C Unit-II BCA Semester I
30
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 3:
#include <stdio.h>
int main()
{
int result = 5 > 3 > 1; // both > operators have same precedence
printf("%dn", result);
return 0;
}
Step-by-step evaluation
Relational operators (>, <) have same precedence.
They follow Left → Right associativity.
5 > 3 → 1 (true)
1 > 1 → 0 (false)
Output: 0
II)Right-to-Left Associativity Examples
Example 1:
#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
a += b -= 2;
printf("%d %dn", a, b);
return 0;
}
Step-by-step evaluation
a += (b -= 2)
b = 5 - 2 = 3, then a = 10 + 3 = 13
Output: a = 13, b = 3
Programming in C Unit-II BCA Semester I
31
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2:
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
a = b = c; // assignment is Right-to-Left
printf("a = %d, b = %d, c = %dn", a, b, c);
return 0;
}
Step-by-step evaluation
• Assignment (=) operators have Right → Left associativity.
a = b = c; is evaluated as:
a = (b = c)
b = c → b = 30
a = 30
Output: a=30, b=30, c=30
Example 3:
#include <stdio.h>
int main()
{
int a = 5;
int b = ++a * ++a; // Unary ++ (Right-to-Left)
printf("a = %d, b = %dn", a, b);
return 0;
}
Step-by-step evaluation
Unary ++ is applied right-to-left.
First ++a (rightmost) → a = 6
Then left ++a → a = 7
So b = 7 * 6 = 42
Output: a = 7, b = 42
Programming in C Unit-II BCA Semester I
32
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Evaluation of Arithmetic Expressions
Steps:
1. Parentheses first ()
2. Unary operators (like ++, --, +, -)
3. Multiplication / Division / Modulus (*, /, %)
4. Addition / Subtraction (+, -)
5. Left-to-right evaluation for operators of same precedence
Example 2:
Programming in C Unit-II BCA Semester I
33
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 3:
#include <stdio.h>
int main()
{
int a = 5;
int result = -(a + 3) * 2;
printf("Result = %dn", result);
return 0;
}
Step-by-step:
• Parentheses: (a + 3) = (5 + 3) = 8
• Unary minus: -8
• Multiplication: -8 * 2 = -16
👉 Output: -16
Example 4:
#include <stdio.h>
int main()
{
int result = 25 / 4 % 3 * 2;
printf("Result = %dn", result);
return 0;
}
Step-by-step:
• Division: 25 / 4 = 6 (integer division)
• Modulus: 6 % 3 = 0
• Multiplication: 0 * 2 = 0
👉 Output: 0
Programming in C Unit-II BCA Semester I
34
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 5:
#include <stdio.h>
int main()
{
int a = 3;
int result = ++a * 2 + a--;
printf("Result = %dn", result);
return 0;
}
Step-by-step:
• ++a: a becomes 4
• Expression: (4 * 2) + a--
• Multiplication: 8 + a--
• a--: use 4 first, then a becomes 3
• 8 + 4 = 12
👉 Output: 12
Example 6:
#include <stdio.h>
int main() {
int result = (15 - 6) % 4 + 18 / 3 * 2;
printf("Result = %dn", result);
return 0;
}
Step-by-step:
1. Parentheses: (15 - 6) = 9
2. Modulus: 9 % 4 = 1
3. Division: 18 / 3 = 6
4. Multiplication: 6 * 2 = 12
5. Addition: 1 + 12 = 13
👉 Output: 13
Programming in C Unit-II BCA Semester I
35
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Control Structures/Statements
Control structures (or control statements) in C are statements that control the flow of program
execution by allowing decisions, repetitions, or jumps instead of simple sequential execution.
Classification of Control Structures
There are three types of control statements in C:
Programming in C Unit-II BCA Semester I
36
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
I)Decision-Making / Conditional / Branching Statements
These statements allow a C program to choose and execute different blocks of code based on
whether a condition evaluates to true or false.
Types of Decision-Making Statements in C
1. if statement
The if statement is a decision-making statement in C that checks a condition and executes a block
of code only when the condition is true; if the condition is false, the block is skipped and the
program continues normally.
Syntax:
if(boolean_expression)
{
// Statement 1
// Statement 2
...
...
// Statement n
}
• A condition is any expression that evaluates to either a true or false (or values
convertible to true or false).
• It checks a condition (expression).
• If the condition is true (non-zero) → the block of code inside if executes.
• If the condition is false (zero) → the block is skipped.
Programming in C Unit-II BCA Semester I
37
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Voting Eligibility Using if Statement
OUTPUT:
Example 2: Check if a number is within a range
OUTPUT:
Programming in C Unit-II BCA Semester I
38
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 3: Check Whether a Number is Even
OUTPUT:
2. if-else Statement
The if-else statement is a two-way decision-making control statement in C.
• If the condition is true, the if block executes.
• If the condition is false, the else block executes.
Syntax:
if(boolean_expression)
{
// Body of if
// If expression is true then execute this
}
else
{
// Body of else
// If expression is false then execute this
}
Programming in C Unit-II BCA Semester I
39
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Check Even or Odd
OUTPUT:
Example 2: Voting Eligibility
OUTPUT:
Programming in C Unit-II BCA Semester I
40
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 3:
OUTPUT:
Advanced Forms of if-else
The basic if-else handles two-way decisions. When problems need multiple conditions or
hierarchical checks, we use advanced forms of if-else.
a) if-else Ladder
An if-else ladder is a series of if–else-if statements used to check multiple conditions in sequence,
where the first true condition’s block is executed and the remaining conditions are skipped.
Syntax:
if (boolean_ condition_1)
{
// executes if condition_1 is true
}
else if (boolean_ condition_2)
{
// executes if condition_2 is true
}
else if (boolean_ condition_3)
Programming in C Unit-II BCA Semester I
41
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
{
// executes if condition_3 is true
}
else
{
// executes if none of the conditions are true
}
Example 1: Temperature Check
OUTPUT:
Programming in C Unit-II BCA Semester I
42
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Simple Calculator
OUTPUT:
Programming in C Unit-II BCA Semester I
43
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
b) Nested if-else
A nested if-else is an if-else statement placed inside another if or else block, used for multi-
level decision-making where one condition is checked only after another.
Syntax:
if (boolean_expression_1)  out if block
{
if(nested_expression_1)
{
// If boolean_expression_1 and
// nested_expression_1 both are true
}
else
{
// If boolean_expression_1 is true
// but nested_expression_1 is false
}
// If boolean_expression_1 is true
}
else
{
if(nested_expression_2)
{
// If boolean_expression_1 is false
// but nested_expression_2 is true
}
else
{
// If both boolean_expression_1 and
// nested_expression_2 is false
}
// If boolean_expression_1 is false
}
Programming in C Unit-II BCA Semester I
44
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Check Character Type (Alphabet, Digit, Special)
OUTPUT:
Programming in C Unit-II BCA Semester I
45
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Student Result (Pass/Fail with Class)
OUTPUT:
Programming in C Unit-II BCA Semester I
46
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
3. Switch Case Statement
A switch case statement evaluates an expression and directs the program to execute the block
associated with the matching case label; if no case matches, the default block is executed.
• The switch statement in C is a multi-way branch statement.
• It allows you to choose one block of code to execute out of many options, based on the
value of an expression (usually an integer or character).
• It makes the program cleaner and more readable compared to long chains of if-else-if.
Syntax
switch (expression)
{
case constant1:
// Code block 1
break;
case constant2:
// Code block 2
break;
...
default:
// Code block if no case matches
}
➢ expression → evaluated once
➢ case → matches expression with constant
➢ break → exits the switch after a case is executed
➢ default → optional, runs if no case matches
Programming in C Unit-II BCA Semester I
47
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Rules for Working with switch Case in C
1. Expression Type:
The expression in switch(expression) must be an integer or character. Floating-point
values are not allowed.
2. Case Constants:
Each case must have a constant value. Variables or ranges cannot be used.
Valid: case 1:, case 'A':
Invalid: case x:
3. Uniqueness:
All case values must be unique. Duplicate case labels are not permitted.
4. Default Case:
The default: block runs when no case matches. It is optional but recommended.
5. Break Statement:
Each case usually ends with a break to stop fall-through.
Without break, execution continues to the next case.
6. Order of Cases:
Cases may appear in any order. The program jumps directly to the matching case.
7. Nesting:
A switch can be nested inside another switch or inside other control structures.
8. Multiple Cases, One Block:
Two or more cases can share the same block of code.
switch (ch)
{
case 'a':
case 'A':
printf("Vowel An");
break;
}
9. Variable Scope:
Variables inside a case should be declared within { } braces to avoid scope errors.
Programming in C Unit-II BCA Semester I
48
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Simple Calculator
OUTPUT:
Programming in C Unit-II BCA Semester I
49
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Day of the Week
OUTPUT:
Programming in C Unit-II BCA Semester I
50
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Comparison: if…else…if vs switch…case
if…else…if switch…case
1. Can handle any valid expression including
integer, float, char, string, relational and logical
expressions.
1. Works only with integral, character or
enumeration constants (int, char, enum).
2. Can check ranges and complex conditions
using relational and logical operators.
2. Cannot check ranges; matches only
fixed constant values.
3. Becomes less readable when many conditions
are written.
3. Cleaner and more readable for multi-
way branching with many constant
options.
4. Slightly slower for many conditions because
each condition is tested sequentially.
4. Faster for a large number of cases as the
compiler may use a jump table.
5. Very flexible; supports relational (>, <) and
logical (&&,
5. Doesn’t support relational operators.
6. else handles conditions that do not match any
previous test.
6. default executes when no case value
matches.
7. Used when decisions depend on ranges,
complex conditions or expressions.
7. Used when checking fixed discrete
values such as menu choices or days of the
week.
Programming in C Unit-II BCA Semester I
51
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
II) Iterative / Looping Statements
Iterative or looping statements are used to repeatedly execute a block of code as long as a
specified condition is true, helping to reduce code repetition.
Types of Looping Statements
1. for Loop
A for loop is an entry-controlled looping statement that repeatedly executes a block of code
as long as a specified condition is true.
It is mainly used when the number of iterations is known in advance and consists of three parts:
1. Initialization → sets the starting value of the loop control variable.
2. Condition → tested before every iteration; if true, loop runs; if false, loop terminates.
3. Update → modifies the loop control variable after every iteration.
Syntax
for (initialization; condition; update)
{
// loop body (statements to be executed repeatedly)
}
Example 1: Print First 10 Natural Numbers
OUTPUT:
Programming in C Unit-II BCA Semester I
52
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Sum of Numbers from 1 to N
OUTPUT:
Nested for Loop
A nested for loop is a for loop placed inside another for loop. The outer loop controls the
number of passes, while the inner loop executes fully for each iteration of the outer loop. It is
commonly used for patterns, matrices, tables, and multi-level iterations.
Syntax
for (initialization1; condition1; update1)
{
// Outer loop body
for (initialization2; condition2; update2)
{
// Inner loop body
}
}
Programming in C Unit-II BCA Semester I
53
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Multiplication Table (1 to 7)
OUTPUT:
Programming in C Unit-II BCA Semester I
54
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Matrix Input and Display
OUTPUT:
Programming in C Unit-II BCA Semester I
55
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
2. while loop
A while loop is an entry-controlled looping statement in C that repeatedly executes a block of
code as long as the specified condition is true. The loop terminates when the condition becomes
false.
Syntax
while (condition)
{
// loop body (statements to execute)
}
• condition → any valid expression.
• If condition is true (non-zero), loop body executes.
• If condition is false (zero), loop terminates.
Example 1: Print 1 to N Numbers
Output:
Programming in C Unit-II BCA Semester I
56
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Reverse a Number
OUTPUT:
Nested while loop
A nested while loop is a while loop placed inside another while loop. The inner loop executes
completely for each iteration of the outer loop and is used for tasks like tables, matrices, and
multi-level repetitive operations.
Syntax
while (condition1)
{
// Outer loop
// Outer loop statements
while (condition2)
{
// Inner loop
// inner loop statements
}
Programming in C Unit-II BCA Semester I
57
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Multiplication Table (1 to 5, each up to 10)
OUTPUT:
Programming in C Unit-II BCA Semester I
58
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
3. do...while loop
A do…while loop is an exit-controlled looping statement in C that executes a block of code at
least once and then repeats execution as long as the specified condition is true.
Syntax
do
{
// loop body (statements to execute)
}
while (condition);
Note: A semicolon (;) must be placed at the end of the while statement.
Example 1: Print 1 to N Numbers
OUTPUT:
Programming in C Unit-II BCA Semester I
59
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Nested do…while loop
A nested do…while loop is a do…while loop placed inside another do…while loop. The inner
loop executes completely for each iteration of the outer loop, and both loops run at least once
because the condition is checked after the loop body.
Syntax
do
{
// outer loop body
do
{
// inner loop body
} while (condition2);
} while (condition1);
Comparison Table: for vs while vs do...while in C
for loop while loop do…while loop
1. Entry-controlled loop. 1. Entry-controlled loop. 1. Exit-controlled loop.
2. Executes only if
condition is true initially.
2. Executes only if condition is
true initially.
2. Executes at least once.
3. Best when number of
iterations is known.
3. Best when number of
iterations is unknown.
3. Best when loop body must
run at least once.
4. Initialization, condition,
and update in one line.
4. Initialization before loop;
update inside loop body.
4. Condition checked after
loop body.
5. Common use: counting,
arrays, series.
5. Common use: input
validation, sentinel-controlled
repetition.
5. Common use: menu-driven
programs, retry mechanisms.
6. Update statement
usually in loop header.
6. Update statement inside loop
body.
6. Update statement inside
loop body.
Programming in C Unit-II BCA Semester I
60
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
III) Jump Statements
• Jump statements in C are control statements that unconditionally transfer program
control from one part of the program to another, altering the normal sequential flow.
• They are commonly used in loops, conditional blocks, and switch cases to control
execution efficiently.
Types of Jump Statements
1. break statement
The break statement in C is a jump statement used to terminate the nearest enclosing loop or
switch case immediately. After execution, control transfers to the statement following the loop or
switch block.
Syntax
break;
• It should always be inside a loop or a switch case.
• Using it outside these will cause a compilation error.
Example 1: Using break in a Loop
OUTPUT:
Programming in C Unit-II BCA Semester I
61
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Prime Number Check using break
OUTPUT:
Programming in C Unit-II BCA Semester I
62
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
2. continue statement
The continue statement in C is a jump statement used inside loops to skip the remaining
statements of the current iteration and immediately proceed to the next iteration of the loop.
Syntax
continue;
• Must be used inside a loop.
• Using it outside a loop causes a compilation error.
Example 1: Skip Even Numbers (Print only odd numbers)
OUTPUT:
Here, whenever i is even, continue skips printing and goes to the next loop iteration.
Programming in C Unit-II BCA Semester I
63
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 2: Skip Negative Numbers in an Array
OUTPUT:
Here, continue is used to ignore negative numbers.
3.goto statement
The goto statement in C is a jump statement that unconditionally transfers program control to
a labeled statement within the same function. It is sometimes used to exit nested loops or handle
special cases, but overuse can reduce code readability.
Syntax
goto label;
...
label:
// statements
• label is a user-defined identifier followed by a colon (:).
• Control jumps directly to the statement after the label.
Programming in C Unit-II BCA Semester I
64
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Example 1: Simple Jump
OUTPUT:
Example 2: Breaking Out of Nested Loops
OUTPUT:
Programming in C Unit-II BCA Semester I
65
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
4.return statement
• The return statement in C is a jump statement that terminates a function and transfers
control back to the calling function, optionally returning a value.
• It plays a key role in function execution and communication between functions.
Syntax
return; // used in void functions (no value returned)
return value; // used in functions that return a value
Example 1: Return Value from a Function
OUTPUT:
Programming in C Unit-II BCA Semester I
66
Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432
📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca
Comparison of Jump Statements in C
break continue goto return
1. Terminates the
nearest loop or switch
immediately.
1. Skips the
remaining
statements of the
current iteration.
1. Jumps
unconditionally to a
labeled statement.
1. Exits a function
and optionally
returns a value.
2. Control transfers to
the statement after the
loop or switch.
2. Control moves to
the next iteration of
the loop.
2. Can be used
anywhere inside a
function.
2. Transfers control
to the calling
function.
3. Used to exit loops
early or stop a switch
case.
3. Used to skip
specific iterations in
loops.
3. Useful for exiting
nested loops or special
cases.
3. Used to return
results from
functions.
4. Cannot be used
outside loops or
switch statements.
4. Only valid inside
loops.
4. Overuse may reduce
code readability.
4. Essential for
function result
passing.
5. Does not execute
remaining statements
in the loop or case.
5. Remaining
statements in the
current iteration are
skipped.
5. Provides
unconditional control
transfer.
5. Immediately ends
function execution.
6. Simple and clear
control transfer within
loops and switch.
6. Helps manage
selective iterations
efficiently.
6. Can make program
flow harder to follow if
overused.
6. Communicates
with the caller
efficiently.
*** ** **

Programming in C: Unit-II Input and Output, Operators, Control Structures/Statements

  • 1.
    Programming in CUnit-II BCA Semester I 1 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Unit-II: Input and Output, Operators, Control Structures/Statements Introduction to Input and Output in C • In C programming, input refers to the data a program receives from the user or an external source, while output refers to the information a program displays on the screen or sends to another device. • By default, C uses standard input (stdin), which is usually the keyboard, and standard output (stdout), which is usually the display screen. These streams allow the program to interact with the outside world effectively. Importance of I/O • Every program needs to work with data from the user, files, or devices. • I/O functions enable communication between the program and its environment. • They help in reading user commands and displaying meaningful results. • I/O is essential for debugging, as output helps monitor program behavior. • Many real-world applications (billing systems, games, databases) rely heavily on I/O for functionality. Types of I/O Functions in C C provides a wide range of functions to handle data input and output. These are classified as: 1. Formatted I/O Functions: Input/output done using format specifiers (printf(), scanf() ) 2. Unformatted I/O Functions: Input/output done without any format. ( getchar(), putchar(), gets(), puts())
  • 2.
    Programming in CUnit-II BCA Semester I 2 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca I)Formatted I/O functions • Formatted I/O functions allow data to be input or displayed in a specific format using format specifiers. • The two main formatted I/O functions in C are printf() and scanf(). 1) printf() – Formatted Output in C • printf() is used to display output on the screen. It prints text, variables, and results in a formatted manner using format specifiers like %d, %f, %c, and %s. • Header file required: #include <stdio.h> Syntax: printf("format string", variables); The format string can include: • Plain text • Format specifiers for variables (%d for integer, %f for float, %c for character.) • Escape sequences (like n for newline) Example: OUTPUT:
  • 3.
    Programming in CUnit-II BCA Semester I 3 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 2. scanf() – Formatted Input in C • scanf() is used to read input from the user. It accepts values in a specified format and stores them in variables. • Header file required: #include <stdio.h> Syntax: scanf("format string", &variables); • The format string contains format specifiers to indicate the type of input. • The & (address-of operator) is used to store the value in the variable (except for strings). • scanf() reads the value typed by the user and assigns it to the given variable. Example:
  • 4.
    Programming in CUnit-II BCA Semester I 4 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca SAMPLE INPUT: OUTPUT: Comparison of printf() and scanf() in C printf() scanf() 1. Used for displaying (output) data on the screen. 1. Used for reading (input) data from the user. 2. Formatted output function. 2. Formatted input function. 3. Data flows from Program → Screen. 3. Data flows from Keyboard → Program. 4. Uses format specifiers to print values (%d, %f, %s). 4. Uses format specifiers to read values. 5. Does not require the & operator. 5. Requires & operator for variables (except strings). 6. Only displays output; no input validation. 6. Returns number of inputs successfully read. 7. Example: printf("Age = %d", age); 7. Example: scanf("%d", &age);
  • 5.
    Programming in CUnit-II BCA Semester I 5 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Control Strings and Escape Sequences in C • Control strings and escape sequences in C are special character combinations used inside printf() and scanf() to control how data is formatted, displayed, or interpreted. • They help manage output formatting and represent characters that cannot be typed directly. 1)Control Strings (Format Strings) • Control strings are the text written inside double quotes in formatted I/O functions such as printf() and scanf(). • They tell the compiler how the data should be displayed (output) or read (input). A control string contains: • Format specifiers (like %d, %f, %c, %s) • Escape sequences (like n, t) • Normal characters and symbols Common Control Strings Control String Meaning Example Output %d Prints an integer printf("Age: %d", 25); Age: 25 %f Prints a float printf("Pi: %f", 3.14); Pi: 3.140000 %.2f Prints float with 2 decimal places printf("Pi: %.2f", 3.14); Pi: 3.14 %c Prints a character printf("Grade: %c", 'A'); Grade: A %s Prints a string printf("Name: %s", "John"); Name: John Example: OUTPUT:
  • 6.
    Programming in CUnit-II BCA Semester I 6 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 2)Escape Sequences • Escape sequences are special character combinations that begin with a backslash (). They are used inside strings to control formatting, spacing, and printing of non-printable characters. • Used mainly in output formatting with printf(). Common Escape Sequences Escape Sequence Meaning Example Code Output n New line printf("HellonWorld"); Hello World t Horizontal tab printf("AtB"); A B v Vertical tab printf("XvY"); X Y Backslash printf(""); " Double quote " printf(""C is fun""); "C is fun" b Backspace printf("Hellob!"); Hell! r Carriage return printf("HellorHi"); Hi (overwrites) Example OUTPUT:
  • 7.
    Programming in CUnit-II BCA Semester I 7 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Comparison: Control Strings vs Escape Sequences in C Control Strings Escape Sequences 1. Used in printf() and scanf() to control input/output format. 1. Used inside strings to represent special characters. 2. Include format specifiers like %d, %f, %s, %c. 2. Begin with a backslash such as n, t, . 3. Tell the compiler how data should be printed or read. 3. Tell the compiler how text should be displayed. 4. Used mainly for formatted input and output operations. 4. Used mainly for controlling text layout on screen. 5. Examples: %d (int), %f (float), %c (char). 5. Examples: n (new line), t (tab), b (backspace). 6. Do not affect screen spacing directly. 6. Directly affect display formatting like line breaks and tabs. II)Unformatted I/O functions • Unformatted I/O functions in C read and write data in raw form without using format specifiers. They handle input and output directly as characters or strings. • Examples: getchar(), putchar(), gets(), puts() 1. getchar() Unformatted Input • getchar() is an unformatted input function in C that reads one character at a time from the standard input (keyboard). • It is useful when you need character-by-character input. Unlike scanf(), it does not use any format specifiers. Syntax: int getchar(void);
  • 8.
    Programming in CUnit-II BCA Semester I 8 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca getchar() returns the ASCII value of the character entered. Its return type is int so that it can also represent EOF (End of File). Example SAMPLE INPUT: OUTPUT: 2. putchar() Unformatted Output • putchar() is an unformatted output function in C that prints one character at a time to the standard output (screen). • It is commonly used for character-by-character output and often works together with getchar(). Syntax: int putchar(int ch);
  • 9.
    Programming in CUnit-II BCA Semester I 9 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example OUTPUT: 3. gets() unformatted input in C • gets() is an unformatted input function in C used to read an entire line of text from the keyboard. • It stores the input in a character array and reads characters until a newline (n) is entered, making it possible to read strings with spaces. Syntax char* gets(char *str); • str → character array where input will be stored. • Returns str on success, NULL on error.
  • 10.
    Programming in CUnit-II BCA Semester I 10 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example OUTPUT: 4. puts() unformatted output in C • puts() is an unformatted output function in C used to print a string (character array) to the screen. • Unlike printf(), it automatically adds a newline (n) after printing the string. Syntax int puts(const char *str); • str → the string to be displayed. • Returns a non-negative value on success, EOF on error.
  • 11.
    Programming in CUnit-II BCA Semester I 11 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example OUTPUT: Comparison of Formatted and Unformatted I/O Functions Formatted I/O Functions Unformatted I/O Functions 1. Use format specifiers (e.g., %d, %f, %s) to control the type and format of input/output. 1. Do not use any format specifiers; work directly with raw characters or strings. 2. Allow precise control over data representation (width, precision, alignment). 2. Offer no control over formatting; simply read or write data as it is. 3. Functions include printf(), scanf(). 3. Functions include getchar(), putchar(), gets(), puts(). 4. Used for numbers, characters, strings, and mixed data types. 4. Mainly used for single characters or complete strings. 5. More complex and slower due to formatting and type checking. 5. Faster since there is no formatting overhead. 6. Provide flexibility for structured or formatted output (e.g., tables, reports). 6. Suitable for simple tasks like character- by-character input/output.
  • 12.
    Programming in CUnit-II BCA Semester I 12 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Operators in C Operators in C are special symbols that perform specific operations on variables and values. They instruct the compiler to carry out tasks such as arithmetic calculation, comparison, assignment, or logical processing. Example Let a = 10, b = 5 c = a + b; . Operators Used 1. Arithmetic Operator (+) → adds a and b → 10 + 5 = 15. 2. Assignment Operator (=) → assigns the result 15 to variable c. Final Result: c = 15 Classification of Operators in C In C programming, operators are classified based on the number of operands they work on. They are grouped into: 1. Binary Operators – operate on two operands 2. Unary Operators – operate on one operand 3. Ternary Operators – operate on three operands
  • 13.
    Programming in CUnit-II BCA Semester I 13 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca I. Binary Operators A binary operator is an operator that acts on two operands to perform operations such as arithmetic, comparison, or logical evaluation. 1. Arithmetic Binary Operators Arithmetic binary operators perform basic mathematical operations like addition, subtraction, multiplication, and division on two operands. Operator Name Description Example (a=10, b=3) Result + Addition Adds two operands a + b 13 - Subtraction Subtracts right operand from left operand a - b 7 * Multiplication Multiplies two operands a * b 30 / Division Divides left operand by right operand a / b 3 % Modulus Gives remainder of division a % b 1 C Program Example
  • 14.
    Programming in CUnit-II BCA Semester I 14 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 2. Relational Binary Operators Relational binary operators are used to compare two operands. They return a boolean result: true (1) if the condition is satisfied, and false (0) otherwise. Operator Name Description Example (a = 10, b = 5) Result == Equal to Checks if two operands are equal a == b 0 (false) != Not equal to Checks if two operands are not equal a != b 1 (true) > Greater than Checks if left operand is greater than right a > b 1 (true) < Less than Checks if left operand is less than right a < b 0 (false) >= Greater or equal Checks if left operand is greater or equal a >= b 1 (true) <= Less or equal Checks if left operand is less or equal a <= b 0 (false) C Program Example
  • 15.
    Programming in CUnit-II BCA Semester I 15 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 3. Logical Binary Operators Logical binary operators are used to combine two relational conditions. They return true (1) if the combined condition is satisfied; otherwise, they return false (0). Operator Symbol Symbol Description Example Result && Logical AND True if both operands are true (a > 0) && (b < 5) 1 if both true, 0 otherwise || Logical OR True if at least one operand is true (a > 0) || (b < 5) True if at least one operand is true C Program Example OUTPUT:
  • 16.
    Programming in CUnit-II BCA Semester I 16 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 4. Bitwise Binary Operators Bitwise operators in C work bit by bit on integer values. They directly manipulate the individual bits of the operands. Operator Symbol Operator Name Example / Description / Result & Bitwise AND 6 & 3 → (110 & 011) = 010 → 2 | Bitwise OR 6 | 3 → (110 & 011) = 111 → 7 ^ Bitwise XOR 6 ^ 3 → (110 ^ 011) = 101 → 5 << Left Shift 6 << 1 → (110 << 1) = 1100 → 12 >> Right Shift 6 >> 1 → (110 >> 1) = 11 → 3 C Program Example OUTPUT:
  • 17.
    Programming in CUnit-II BCA Semester I 17 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 5. Assignment Binary Operators • Assignment operators in C are binary operators that assign values to variables. • They can also perform an operation (like addition, subtraction, etc.) and assign the result back to the variable in a single step. Operator Symbol Operator Name Example Explanation = Simple Assignment a = b; Assigns value of b to a. += Add and Assign a += b; Equivalent to a = a + b. -= Subtract and Assign a -= b; Equivalent to a = a - b. *= Multiply and Assign a *= b; Equivalent to a = a * b. /= Divide and Assign a /= b; Equivalent to a = a / b. %= Modulus and Assign a %= b; Equivalent to a = a % b. <<= Left Shift and Assign a <<= 1; Equivalent to a = a << 1. >>= Right Shift and Assign a >>= 1; Equivalent to a = a >> 1. &= Bitwise AND and Assign a &= b; Equivalent to a = a & b. |= Bitwise OR and Assign a|=b; Equivalent to a = a | b. ^= Bitwise XOR and Assign a ^= b; Equivalent to a = a ^ b.
  • 18.
    Programming in CUnit-II BCA Semester I 18 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca C Program Example OUTPUT:
  • 19.
    Programming in CUnit-II BCA Semester I 19 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca II. Unary Operators • A unary operator in C is an operator that operates on only one operand. • It is used to perform operations like increment, decrement, negation, size determination, etc. 1. Unary Arithmetic Operators Operator Symbol Operator Name Example (Let a=5) Explanation + Unary Plus +a → +5 Represents a positive value (usually has no effect, just indicates positivity). - Unary Minus -a → -5 Negates the value of the operand (changes sign). ++a Pre- increment a=5; ++a; → 6 Increases a by 1 before using it in an expression. a++ Post- increment a=5; a++; → 5 (then a=6) Uses a first, then increases it by 1 after evaluation. --a Pre- decrement a=5; --a; → 4 Decreases a by 1 before using it in an expression. a-- Post- decrement a=5; a--; → 5 (then a=4) Uses a first, then decreases it by 1 after evaluation. Note: ▪ Pre (++a / --a): Change happens before the value is used. ▪ Post (a++ / a--): Change happens after the value is used.
  • 20.
    Programming in CUnit-II BCA Semester I 20 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca C Program Example OUTPUT:
  • 21.
    Programming in CUnit-II BCA Semester I 21 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 2. Unary Logical Operator A unary logical operator works on a single operand. In C, the only unary logical operator is the logical NOT (!), which reverses the truth value of an expression. It inverts the Boolean value of an expression: ▪ If the operand is non-zero (true) → result is 0 (false). ▪ If the operand is 0 (false) → result is 1 (true). Operator Name Example Explanation Result (if a=5, b=0) ! Logical NOT !a If a is non-zero → result is 0 !5 = 0 - - !b If b is zero → result is 1 !0 = 1 C Program Example OUTPUT:
  • 22.
    Programming in CUnit-II BCA Semester I 22 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 3. Unary Bitwise Operators • Unary bitwise operators in C work on the bit-level representation of data. • They perform operations on each bit of the operand. Operator Name Example Explanation (for a = 5, i.e., binary 00000101) Result ~ Bitwise NOT ~a Inverts each bit: 00000101 → 11111010 -6 (in 2’s complement form) Note: • For signed integers, the result of ~ depends on the system’s representation (commonly 2’s complement). • Example: a = 5 (00000101) → ~a = 11111010 which represents -6 in decimal. C Program Example OUTPUT:
  • 23.
    Programming in CUnit-II BCA Semester I 23 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Comparison: ~ vs ! in C Operator Name Works On Operation Type Example (a=5, b=0) Result Explanation ~ Bitwise NOT Integers Inverts each bit ~5 -6 5 (00000101) → 11111010 = -6 (two’s complement) ! Logical NOT Any scalar (int, float, etc.) Converts true→false and false→true !5 0 Non-zero is true → !true = 0 !0 1 Zero is false → !false = 1 Key Points: • ~ → bitwise complement, flips all bits of an integer. • ! → logical negation, only checks if value is zero or not. Example C program: OUTPUT:
  • 24.
    Programming in CUnit-II BCA Semester I 24 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 4.Special Unary Operators • Special unary operators in C operate on a single operand but do not fall strictly under arithmetic, logical, or bitwise categories. • They offer additional features such as accessing memory addresses, dereferencing pointers, or determining the size of data types and variables. Operator Name Example Explanation & Address-of Operator p = &a; Returns the address of variable a. * Value-of / Dereference x = *p; Accesses the value stored at the address in pointer p. sizeof Size-of Operator sizeof(int) Returns the size (in bytes) of a data type or variable. Example C program: OUPUT:
  • 25.
    Programming in CUnit-II BCA Semester I 25 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca III. Ternary/Conditional Operator The ternary operator is the only operator in C that works with three operands. It is also known as the conditional operator (? :) and is used as a short, concise alternative to an if–else statement. Syntax: (condition) ? expression1 : expression2; • If condition is true (non-zero) → evaluates expression1. • If condition is false (zero) → evaluates expression2. Operator Symbol Operator Name Example Explanation Result (if a=10, b=20) ?: Ternary (Conditional) (a > b) ? a : b If a > b, result is a, else result is b. 20 - - (a == b) ? 100 : 200 If a equals b, result is 100, else result is 200. 200 - - (a % 2 == 0) ? "Even" : "Odd" If a divisible by 2, result is "Even", else result is "Odd". "Even" C Program Example 1:
  • 26.
    Programming in CUnit-II BCA Semester I 26 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca C Program Example 2: Biggest Among 3 Floats using Ternary Operator OUTPUT: Summary of Operators in C
  • 27.
    Programming in CUnit-II BCA Semester I 27 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Operator Precedence and Associativity in C Precedence and Associativity Table Precedence Operator Description Associativity 1 () Parentheses (function call) Left-to-Right [] Array Subscript (Square Brackets) . Dot Operator -> Structure Pointer Operator ++ , -- Postfix increment, decrement 2 ++ / -- Prefix increment, decrement Right-to-Left + / - Unary plus, minus ! , ~ Logical NOT, Bitwise complement (type) Cast Operator * Dereference Operator & Addressof Operator sizeof Determine size in bytes 3 *,/,% Multiplication, division, modulus Left-to-Right 4 +,- Addition, subtraction Left-to-Right 5 << , >> Bitwise shift left, Bitwise shift right Left-to-Right 6 < , <= Relational less than, less than or equal to Left-to-Right > , >= Relational greater than, greater than or equal to 7 == , != Relational is equal to, is not equal to Left-to-Right 8 & Bitwise AND Left-to-Right 9 ^ Bitwise exclusive OR Left-to-Right 10 | Bitwise inclusive OR Left-to-Right 11 && Logical AND Left-to-Right 12 || Logical OR Left-to-Right 13 ?: Ternary conditional Right-to-Left 14 = Assignment Right-to-Left += , -= Addition, subtraction assignment *= , /= Multiplication, division assignment %= , &= Modulus, bitwise AND assignment ^= , |= Bitwise exclusive, inclusive OR assignment <<=, >>= Bitwise shift left, right assignment 15 , comma (expression separator) Left-to-Right
  • 28.
    Programming in CUnit-II BCA Semester I 28 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 1. Operator Precedence Operator precedence defines the priority level of operators when an expression contains more than one operator. The operator with higher precedence is evaluated first. Example: int x = 10 + 20 * 3; Here, * has higher precedence than +, so the multiplication happens first: Evaluation: x = 10 + (20 * 3) = 70. 2. Operator Associativity Associativity defines the direction of evaluation when two operators of the same precedence appear in an expression. It can be either left-to-right or right-to-left. • Left → Right (L→R): Most binary operators ( + - * / % < > == && || , ) • Right → Left (R→L): Unary operators (++ -- ! ~ sizeof), conditional (?:), assignment (= += -= *= /= %=, etc.) Example (Left-to-right): int x = 10 - 5 - 2; // evaluated as (10 - 5) - 2 = 3 Example (Right-to-left): Assignment operators (=, +=, -=, etc.) follow right-to-left associativity: a = b = c = 5; // evaluated as a = (b = (c = 5));
  • 29.
    Programming in CUnit-II BCA Semester I 29 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca I)Left-to-Right Associativity Examples Example 1: #include <stdio.h> int main() { int result = 100 / 10 * 2; // both / and * have same precedence printf("%dn", result); return 0; } Step-by-step evaluation • Division and multiplication have same precedence. • They follow Left → Right associativity. 100 / 10 = 10 10 * 2 = 20 Output: 20 Example 2: #include <stdio.h> int main() { int result = 50 - 20 + 5; // both - and + have same precedence printf("%dn", result); return 0; } Step-by-step evaluation Addition and subtraction have same precedence. They follow Left → Right associativity. 50 - 20 = 30 30 + 5 = 35 Output: 35
  • 30.
    Programming in CUnit-II BCA Semester I 30 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 3: #include <stdio.h> int main() { int result = 5 > 3 > 1; // both > operators have same precedence printf("%dn", result); return 0; } Step-by-step evaluation Relational operators (>, <) have same precedence. They follow Left → Right associativity. 5 > 3 → 1 (true) 1 > 1 → 0 (false) Output: 0 II)Right-to-Left Associativity Examples Example 1: #include <stdio.h> int main() { int a = 10; int b = 5; a += b -= 2; printf("%d %dn", a, b); return 0; } Step-by-step evaluation a += (b -= 2) b = 5 - 2 = 3, then a = 10 + 3 = 13 Output: a = 13, b = 3
  • 31.
    Programming in CUnit-II BCA Semester I 31 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: #include <stdio.h> int main() { int a = 10, b = 20, c = 30; a = b = c; // assignment is Right-to-Left printf("a = %d, b = %d, c = %dn", a, b, c); return 0; } Step-by-step evaluation • Assignment (=) operators have Right → Left associativity. a = b = c; is evaluated as: a = (b = c) b = c → b = 30 a = 30 Output: a=30, b=30, c=30 Example 3: #include <stdio.h> int main() { int a = 5; int b = ++a * ++a; // Unary ++ (Right-to-Left) printf("a = %d, b = %dn", a, b); return 0; } Step-by-step evaluation Unary ++ is applied right-to-left. First ++a (rightmost) → a = 6 Then left ++a → a = 7 So b = 7 * 6 = 42 Output: a = 7, b = 42
  • 32.
    Programming in CUnit-II BCA Semester I 32 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Evaluation of Arithmetic Expressions Steps: 1. Parentheses first () 2. Unary operators (like ++, --, +, -) 3. Multiplication / Division / Modulus (*, /, %) 4. Addition / Subtraction (+, -) 5. Left-to-right evaluation for operators of same precedence Example 2:
  • 33.
    Programming in CUnit-II BCA Semester I 33 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 3: #include <stdio.h> int main() { int a = 5; int result = -(a + 3) * 2; printf("Result = %dn", result); return 0; } Step-by-step: • Parentheses: (a + 3) = (5 + 3) = 8 • Unary minus: -8 • Multiplication: -8 * 2 = -16 👉 Output: -16 Example 4: #include <stdio.h> int main() { int result = 25 / 4 % 3 * 2; printf("Result = %dn", result); return 0; } Step-by-step: • Division: 25 / 4 = 6 (integer division) • Modulus: 6 % 3 = 0 • Multiplication: 0 * 2 = 0 👉 Output: 0
  • 34.
    Programming in CUnit-II BCA Semester I 34 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 5: #include <stdio.h> int main() { int a = 3; int result = ++a * 2 + a--; printf("Result = %dn", result); return 0; } Step-by-step: • ++a: a becomes 4 • Expression: (4 * 2) + a-- • Multiplication: 8 + a-- • a--: use 4 first, then a becomes 3 • 8 + 4 = 12 👉 Output: 12 Example 6: #include <stdio.h> int main() { int result = (15 - 6) % 4 + 18 / 3 * 2; printf("Result = %dn", result); return 0; } Step-by-step: 1. Parentheses: (15 - 6) = 9 2. Modulus: 9 % 4 = 1 3. Division: 18 / 3 = 6 4. Multiplication: 6 * 2 = 12 5. Addition: 1 + 12 = 13 👉 Output: 13
  • 35.
    Programming in CUnit-II BCA Semester I 35 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Control Structures/Statements Control structures (or control statements) in C are statements that control the flow of program execution by allowing decisions, repetitions, or jumps instead of simple sequential execution. Classification of Control Structures There are three types of control statements in C:
  • 36.
    Programming in CUnit-II BCA Semester I 36 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca I)Decision-Making / Conditional / Branching Statements These statements allow a C program to choose and execute different blocks of code based on whether a condition evaluates to true or false. Types of Decision-Making Statements in C 1. if statement The if statement is a decision-making statement in C that checks a condition and executes a block of code only when the condition is true; if the condition is false, the block is skipped and the program continues normally. Syntax: if(boolean_expression) { // Statement 1 // Statement 2 ... ... // Statement n } • A condition is any expression that evaluates to either a true or false (or values convertible to true or false). • It checks a condition (expression). • If the condition is true (non-zero) → the block of code inside if executes. • If the condition is false (zero) → the block is skipped.
  • 37.
    Programming in CUnit-II BCA Semester I 37 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Voting Eligibility Using if Statement OUTPUT: Example 2: Check if a number is within a range OUTPUT:
  • 38.
    Programming in CUnit-II BCA Semester I 38 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 3: Check Whether a Number is Even OUTPUT: 2. if-else Statement The if-else statement is a two-way decision-making control statement in C. • If the condition is true, the if block executes. • If the condition is false, the else block executes. Syntax: if(boolean_expression) { // Body of if // If expression is true then execute this } else { // Body of else // If expression is false then execute this }
  • 39.
    Programming in CUnit-II BCA Semester I 39 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Check Even or Odd OUTPUT: Example 2: Voting Eligibility OUTPUT:
  • 40.
    Programming in CUnit-II BCA Semester I 40 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 3: OUTPUT: Advanced Forms of if-else The basic if-else handles two-way decisions. When problems need multiple conditions or hierarchical checks, we use advanced forms of if-else. a) if-else Ladder An if-else ladder is a series of if–else-if statements used to check multiple conditions in sequence, where the first true condition’s block is executed and the remaining conditions are skipped. Syntax: if (boolean_ condition_1) { // executes if condition_1 is true } else if (boolean_ condition_2) { // executes if condition_2 is true } else if (boolean_ condition_3)
  • 41.
    Programming in CUnit-II BCA Semester I 41 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca { // executes if condition_3 is true } else { // executes if none of the conditions are true } Example 1: Temperature Check OUTPUT:
  • 42.
    Programming in CUnit-II BCA Semester I 42 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Simple Calculator OUTPUT:
  • 43.
    Programming in CUnit-II BCA Semester I 43 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca b) Nested if-else A nested if-else is an if-else statement placed inside another if or else block, used for multi- level decision-making where one condition is checked only after another. Syntax: if (boolean_expression_1)  out if block { if(nested_expression_1) { // If boolean_expression_1 and // nested_expression_1 both are true } else { // If boolean_expression_1 is true // but nested_expression_1 is false } // If boolean_expression_1 is true } else { if(nested_expression_2) { // If boolean_expression_1 is false // but nested_expression_2 is true } else { // If both boolean_expression_1 and // nested_expression_2 is false } // If boolean_expression_1 is false }
  • 44.
    Programming in CUnit-II BCA Semester I 44 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Check Character Type (Alphabet, Digit, Special) OUTPUT:
  • 45.
    Programming in CUnit-II BCA Semester I 45 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Student Result (Pass/Fail with Class) OUTPUT:
  • 46.
    Programming in CUnit-II BCA Semester I 46 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 3. Switch Case Statement A switch case statement evaluates an expression and directs the program to execute the block associated with the matching case label; if no case matches, the default block is executed. • The switch statement in C is a multi-way branch statement. • It allows you to choose one block of code to execute out of many options, based on the value of an expression (usually an integer or character). • It makes the program cleaner and more readable compared to long chains of if-else-if. Syntax switch (expression) { case constant1: // Code block 1 break; case constant2: // Code block 2 break; ... default: // Code block if no case matches } ➢ expression → evaluated once ➢ case → matches expression with constant ➢ break → exits the switch after a case is executed ➢ default → optional, runs if no case matches
  • 47.
    Programming in CUnit-II BCA Semester I 47 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Rules for Working with switch Case in C 1. Expression Type: The expression in switch(expression) must be an integer or character. Floating-point values are not allowed. 2. Case Constants: Each case must have a constant value. Variables or ranges cannot be used. Valid: case 1:, case 'A': Invalid: case x: 3. Uniqueness: All case values must be unique. Duplicate case labels are not permitted. 4. Default Case: The default: block runs when no case matches. It is optional but recommended. 5. Break Statement: Each case usually ends with a break to stop fall-through. Without break, execution continues to the next case. 6. Order of Cases: Cases may appear in any order. The program jumps directly to the matching case. 7. Nesting: A switch can be nested inside another switch or inside other control structures. 8. Multiple Cases, One Block: Two or more cases can share the same block of code. switch (ch) { case 'a': case 'A': printf("Vowel An"); break; } 9. Variable Scope: Variables inside a case should be declared within { } braces to avoid scope errors.
  • 48.
    Programming in CUnit-II BCA Semester I 48 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Simple Calculator OUTPUT:
  • 49.
    Programming in CUnit-II BCA Semester I 49 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Day of the Week OUTPUT:
  • 50.
    Programming in CUnit-II BCA Semester I 50 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Comparison: if…else…if vs switch…case if…else…if switch…case 1. Can handle any valid expression including integer, float, char, string, relational and logical expressions. 1. Works only with integral, character or enumeration constants (int, char, enum). 2. Can check ranges and complex conditions using relational and logical operators. 2. Cannot check ranges; matches only fixed constant values. 3. Becomes less readable when many conditions are written. 3. Cleaner and more readable for multi- way branching with many constant options. 4. Slightly slower for many conditions because each condition is tested sequentially. 4. Faster for a large number of cases as the compiler may use a jump table. 5. Very flexible; supports relational (>, <) and logical (&&, 5. Doesn’t support relational operators. 6. else handles conditions that do not match any previous test. 6. default executes when no case value matches. 7. Used when decisions depend on ranges, complex conditions or expressions. 7. Used when checking fixed discrete values such as menu choices or days of the week.
  • 51.
    Programming in CUnit-II BCA Semester I 51 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca II) Iterative / Looping Statements Iterative or looping statements are used to repeatedly execute a block of code as long as a specified condition is true, helping to reduce code repetition. Types of Looping Statements 1. for Loop A for loop is an entry-controlled looping statement that repeatedly executes a block of code as long as a specified condition is true. It is mainly used when the number of iterations is known in advance and consists of three parts: 1. Initialization → sets the starting value of the loop control variable. 2. Condition → tested before every iteration; if true, loop runs; if false, loop terminates. 3. Update → modifies the loop control variable after every iteration. Syntax for (initialization; condition; update) { // loop body (statements to be executed repeatedly) } Example 1: Print First 10 Natural Numbers OUTPUT:
  • 52.
    Programming in CUnit-II BCA Semester I 52 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Sum of Numbers from 1 to N OUTPUT: Nested for Loop A nested for loop is a for loop placed inside another for loop. The outer loop controls the number of passes, while the inner loop executes fully for each iteration of the outer loop. It is commonly used for patterns, matrices, tables, and multi-level iterations. Syntax for (initialization1; condition1; update1) { // Outer loop body for (initialization2; condition2; update2) { // Inner loop body } }
  • 53.
    Programming in CUnit-II BCA Semester I 53 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Multiplication Table (1 to 7) OUTPUT:
  • 54.
    Programming in CUnit-II BCA Semester I 54 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Matrix Input and Display OUTPUT:
  • 55.
    Programming in CUnit-II BCA Semester I 55 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 2. while loop A while loop is an entry-controlled looping statement in C that repeatedly executes a block of code as long as the specified condition is true. The loop terminates when the condition becomes false. Syntax while (condition) { // loop body (statements to execute) } • condition → any valid expression. • If condition is true (non-zero), loop body executes. • If condition is false (zero), loop terminates. Example 1: Print 1 to N Numbers Output:
  • 56.
    Programming in CUnit-II BCA Semester I 56 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Reverse a Number OUTPUT: Nested while loop A nested while loop is a while loop placed inside another while loop. The inner loop executes completely for each iteration of the outer loop and is used for tasks like tables, matrices, and multi-level repetitive operations. Syntax while (condition1) { // Outer loop // Outer loop statements while (condition2) { // Inner loop // inner loop statements }
  • 57.
    Programming in CUnit-II BCA Semester I 57 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Multiplication Table (1 to 5, each up to 10) OUTPUT:
  • 58.
    Programming in CUnit-II BCA Semester I 58 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 3. do...while loop A do…while loop is an exit-controlled looping statement in C that executes a block of code at least once and then repeats execution as long as the specified condition is true. Syntax do { // loop body (statements to execute) } while (condition); Note: A semicolon (;) must be placed at the end of the while statement. Example 1: Print 1 to N Numbers OUTPUT:
  • 59.
    Programming in CUnit-II BCA Semester I 59 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Nested do…while loop A nested do…while loop is a do…while loop placed inside another do…while loop. The inner loop executes completely for each iteration of the outer loop, and both loops run at least once because the condition is checked after the loop body. Syntax do { // outer loop body do { // inner loop body } while (condition2); } while (condition1); Comparison Table: for vs while vs do...while in C for loop while loop do…while loop 1. Entry-controlled loop. 1. Entry-controlled loop. 1. Exit-controlled loop. 2. Executes only if condition is true initially. 2. Executes only if condition is true initially. 2. Executes at least once. 3. Best when number of iterations is known. 3. Best when number of iterations is unknown. 3. Best when loop body must run at least once. 4. Initialization, condition, and update in one line. 4. Initialization before loop; update inside loop body. 4. Condition checked after loop body. 5. Common use: counting, arrays, series. 5. Common use: input validation, sentinel-controlled repetition. 5. Common use: menu-driven programs, retry mechanisms. 6. Update statement usually in loop header. 6. Update statement inside loop body. 6. Update statement inside loop body.
  • 60.
    Programming in CUnit-II BCA Semester I 60 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca III) Jump Statements • Jump statements in C are control statements that unconditionally transfer program control from one part of the program to another, altering the normal sequential flow. • They are commonly used in loops, conditional blocks, and switch cases to control execution efficiently. Types of Jump Statements 1. break statement The break statement in C is a jump statement used to terminate the nearest enclosing loop or switch case immediately. After execution, control transfers to the statement following the loop or switch block. Syntax break; • It should always be inside a loop or a switch case. • Using it outside these will cause a compilation error. Example 1: Using break in a Loop OUTPUT:
  • 61.
    Programming in CUnit-II BCA Semester I 61 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Prime Number Check using break OUTPUT:
  • 62.
    Programming in CUnit-II BCA Semester I 62 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 2. continue statement The continue statement in C is a jump statement used inside loops to skip the remaining statements of the current iteration and immediately proceed to the next iteration of the loop. Syntax continue; • Must be used inside a loop. • Using it outside a loop causes a compilation error. Example 1: Skip Even Numbers (Print only odd numbers) OUTPUT: Here, whenever i is even, continue skips printing and goes to the next loop iteration.
  • 63.
    Programming in CUnit-II BCA Semester I 63 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 2: Skip Negative Numbers in an Array OUTPUT: Here, continue is used to ignore negative numbers. 3.goto statement The goto statement in C is a jump statement that unconditionally transfers program control to a labeled statement within the same function. It is sometimes used to exit nested loops or handle special cases, but overuse can reduce code readability. Syntax goto label; ... label: // statements • label is a user-defined identifier followed by a colon (:). • Control jumps directly to the statement after the label.
  • 64.
    Programming in CUnit-II BCA Semester I 64 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Example 1: Simple Jump OUTPUT: Example 2: Breaking Out of Nested Loops OUTPUT:
  • 65.
    Programming in CUnit-II BCA Semester I 65 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca 4.return statement • The return statement in C is a jump statement that terminates a function and transfers control back to the calling function, optionally returning a value. • It plays a key role in function execution and communication between functions. Syntax return; // used in void functions (no value returned) return value; // used in functions that return a value Example 1: Return Value from a Function OUTPUT:
  • 66.
    Programming in CUnit-II BCA Semester I 66 Notes by Dr.Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432 📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca Comparison of Jump Statements in C break continue goto return 1. Terminates the nearest loop or switch immediately. 1. Skips the remaining statements of the current iteration. 1. Jumps unconditionally to a labeled statement. 1. Exits a function and optionally returns a value. 2. Control transfers to the statement after the loop or switch. 2. Control moves to the next iteration of the loop. 2. Can be used anywhere inside a function. 2. Transfers control to the calling function. 3. Used to exit loops early or stop a switch case. 3. Used to skip specific iterations in loops. 3. Useful for exiting nested loops or special cases. 3. Used to return results from functions. 4. Cannot be used outside loops or switch statements. 4. Only valid inside loops. 4. Overuse may reduce code readability. 4. Essential for function result passing. 5. Does not execute remaining statements in the loop or case. 5. Remaining statements in the current iteration are skipped. 5. Provides unconditional control transfer. 5. Immediately ends function execution. 6. Simple and clear control transfer within loops and switch. 6. Helps manage selective iterations efficiently. 6. Can make program flow harder to follow if overused. 6. Communicates with the caller efficiently. *** ** **