PROGRAMMING IN C l
1 ST INTERNAL l
3 RD SEMESTER, 2ND YEAR l
__________________________l
1. Discuss different operators in C with examples.
Solution: The different operators in C are as follow-:
I) Arithmetic
Examples of arithmetic operators are as +, -, *, /, % .
II) Relational
Examples of relational operators are as <, >, ==, >=, <=, ! =
III) Logical
Examples of logical operators are as logical AND(&&), Logical OR(||), Logical NOT(!)
IV) Assignment
Examples of assignment operators are as basic assignment (=), addition assignment (+=),
multiplication assignment (*=), subtraction assignment (-=), division assignment (/=), modulo
assignment (%=)
V) Increment or decrement
Examples of increment or decrement operators are as(++ , - -)
VI) Bitwise
Examples of bitwise operators are as (&, |, <<, >>, ~, ^)
VII) Conditional
Examples of conditional operators are as(? :)
2."goto is generally avoided" - Discuss. Differentiate between break and
continue statement.
Solution:The goto statement in C allows for an unconditional jump to a labeled statement
within the same function. Considering the feature of goto, there are following reasons for
which use of goto is generally avoided-:
i) Code Readability and Maintainability: Using goto can make code hard to read and follow
because it disrupts the normal flow of control. This can make understanding the code logic
difficult, especially in complex programs.
ii) Increased Risk of Bugs: It can lead to unexpected behaviour, such as skipping the
initialization of variables or bypassing critical sections of code like cleanup routines.
iii) Alternatives Exist: In most cases, structured programming constructs like loops (for,
while), conditionals (if, switch), and functions can achieve the same result as goto without
disrupting the code flow.
Difference Between break and continue
break Statement:
break Statement:
Purpose: Exits the nearest enclosing loop or switch statement.
, 1
Effect: When encountered inside a loop (e.g., for, while, do-while),
break terminates the loop immediately, and control passes to the
statement immediately following the loop.
Use Case: Often used to exit a loop prematurely when a certain
condition is met.
continue Statement:
Purpose: Skips the remaining code in the current loop iteration and
moves control to the next iteration.
Effect: When encountered, continue causes the loop to immediately
jump to the next iteration. In for loops, it skips the increment
step, and in while and do-while loops, it skips to the next
evaluation of the condition.
Use Case: Often used to skip certain iterations of a loop when
specific conditions are met.
3. Differentiate between the following-
a) while and do-while loop
b) while loop and for loop
Solution:
a) While and do-while loop
While loop:
1.The test condition is evaluated first
2.The minimum number of times the loop will be executed is
zero
3.It is also known as pretest or entry-controlled loop
4.Its syntax -
While (condition)
{
statements
}
5.There is no need for a semicolon
Do-while loop:
1.The loop is entered first then the test condition
is evaluated
2.The loop will be executed at least once
3.It is also known as post tester or exit-controlled loop
4.Its syntax-
Do
{
1 ST INTERNAL l
3 RD SEMESTER, 2ND YEAR l
__________________________l
1. Discuss different operators in C with examples.
Solution: The different operators in C are as follow-:
I) Arithmetic
Examples of arithmetic operators are as +, -, *, /, % .
II) Relational
Examples of relational operators are as <, >, ==, >=, <=, ! =
III) Logical
Examples of logical operators are as logical AND(&&), Logical OR(||), Logical NOT(!)
IV) Assignment
Examples of assignment operators are as basic assignment (=), addition assignment (+=),
multiplication assignment (*=), subtraction assignment (-=), division assignment (/=), modulo
assignment (%=)
V) Increment or decrement
Examples of increment or decrement operators are as(++ , - -)
VI) Bitwise
Examples of bitwise operators are as (&, |, <<, >>, ~, ^)
VII) Conditional
Examples of conditional operators are as(? :)
2."goto is generally avoided" - Discuss. Differentiate between break and
continue statement.
Solution:The goto statement in C allows for an unconditional jump to a labeled statement
within the same function. Considering the feature of goto, there are following reasons for
which use of goto is generally avoided-:
i) Code Readability and Maintainability: Using goto can make code hard to read and follow
because it disrupts the normal flow of control. This can make understanding the code logic
difficult, especially in complex programs.
ii) Increased Risk of Bugs: It can lead to unexpected behaviour, such as skipping the
initialization of variables or bypassing critical sections of code like cleanup routines.
iii) Alternatives Exist: In most cases, structured programming constructs like loops (for,
while), conditionals (if, switch), and functions can achieve the same result as goto without
disrupting the code flow.
Difference Between break and continue
break Statement:
break Statement:
Purpose: Exits the nearest enclosing loop or switch statement.
, 1
Effect: When encountered inside a loop (e.g., for, while, do-while),
break terminates the loop immediately, and control passes to the
statement immediately following the loop.
Use Case: Often used to exit a loop prematurely when a certain
condition is met.
continue Statement:
Purpose: Skips the remaining code in the current loop iteration and
moves control to the next iteration.
Effect: When encountered, continue causes the loop to immediately
jump to the next iteration. In for loops, it skips the increment
step, and in while and do-while loops, it skips to the next
evaluation of the condition.
Use Case: Often used to skip certain iterations of a loop when
specific conditions are met.
3. Differentiate between the following-
a) while and do-while loop
b) while loop and for loop
Solution:
a) While and do-while loop
While loop:
1.The test condition is evaluated first
2.The minimum number of times the loop will be executed is
zero
3.It is also known as pretest or entry-controlled loop
4.Its syntax -
While (condition)
{
statements
}
5.There is no need for a semicolon
Do-while loop:
1.The loop is entered first then the test condition
is evaluated
2.The loop will be executed at least once
3.It is also known as post tester or exit-controlled loop
4.Its syntax-
Do
{