2. 2016
• What are the basic data types and their
associated range? (2.5 marks)
2015
What do you mean by the term data type?
Explain any four data types with examples.(2.5
marks)
3. Data Types in C
• Each variable in C has an associated data type.
• Each data type requires different amounts of
memory.
4. • Following are the examples of some very
common data types used in C:
• char: The most basic data type in C. It stores a
single character.
• int: As the name suggests, an int variable is
used to store an integer.
• float: It is used to store decimal numbers
(numbers with floating point value) with single
precision.
• double: It is used to store decimal numbers
(numbers with floating point value) with
double precision.
5. • Different data types also have different
ranges upto which they can store numbers.
• These ranges may vary from compiler to
compiler.
• Below is list of ranges along with the memory
requirement and format specifiers on 32 bit
gcc compiler.
• We can use the sizeof() operator to check the
size of a variable.
• See the following C program for the usage of
the various data types:
7. 2’s complement form
• For signed Numbers : (-2^(n-1)) to (2 ^(n-1)-1)
For example Range for singed char :
n=8 bits
(-2^(8-1)) to (2^(8-1)-1)
(-2^(7)) to (2^(7)-1)
-128 to 127
• For unsigned Numbers: (0 to (2^n)-1)
For example Range for unsigned char
0 to ((2^8)-1)
0 to 256-1
0 to 255
12. • Keywords are specific reserved words in C
each of which has a specific feature associated
with it.
13. Variables
• A variable in simple terms is a storage place
which has some memory allocated to it.
• Basically, a variable used to store some form
of data.
• Different types of variables require different
amounts of memory
• Example :
• Char c; c is a variable which takes 2 bytes in
memory
14. Variable Declaration Rules in C
• Every variable name should start with alphabets
or underscore (_).
• No spaces are allowed in variable declaration.
• Except underscore (_) no other special symbol are
allowed in the middle of the variable declaration
• Maximum length of variable is 8 characters
depend on compiler and operation system.
• Every variable name always should exist in the
left hand side of assignment operator
(invalid -> 10=a; valid -> a=10;).
15. • Variable Declaration:
A typical variable declaration is of the form:
• type variable_name;
or for multiple variables:
• type variable1_name, variable2_name,
variable3_name;
• A variable name can consist of alphabets
(both upper and lower case), numbers and the
underscore ‘_’ character.
• However, the name must not start with a
number.
18. • Difference b/w variable declaration and
definition
Variable declaration refers to the part where
a variable is first declared or introduced
before its first use.
• Variable definition is the part where the
variable is assigned a memory location and a
value.
• Most of the times, variable declaration and
definition are done together.
19. Note
• Is it possible to have separate declaration and
definition?
It is possible in case of extern variables
20. 2019
• Write a C program to test weather a given
integer is a prime number or not. Display an
appropriate message in the output(5.5
marks)
24. 2015
• Write a program to swap two numbers
without using third variable.(2.5 marks)
25.
26.
27. Arrays in C
• An array is a collection of items stored at
contiguous memory locations and elements
can be accessed randomly using index's of an
array.
• They are used to store similar type of
elements as in the data type must be the
same for all elements.
• Given below is the picturesque representation
of an array.
28.
29.
30.
31. 2017
What is an array? Write a program to print
average of all elements of an array.(6.5 marks)
36. Constants in C
• As the name suggests the name constants is
given to such variables or values in C
programming language which cannot be
modified once they are defined.
• They are fixed values in a program.
• There can be any types of constants like
integer, float, octal, character constants etc.
37. Defining Constants:
In C/C++ program we can define constants in
two ways as shown below:
1. Using #define preprocessor directive
2. Using a const keyword
38. • Let us now learn about above two ways in
details:
1. Using #define preprocessor directive:
• We can use this to declare a constant as
shown below:
#define identifierName value
– identifierName: It is the name given to constant.
– value: This refers to any value assigned to
identifierName.
41. 2. using a const keyword:
The qualifier const can be applied to the
declaration of any variable to specify that its
value will not be changed
Declaration and definition of var variable in same line
44. 2015
• What is Enumerated Data type? Explain with
suitable example.(2.5 marks)
45. Enumeration (or enum) in C
• Enumeration (or enum) is a user defined data
type in C.
• It is mainly used to assign names to integral
constants, the names make a program easy to
read and maintain.
49. 2017
What are different type of operators in c?
Briefly explain. (6 marks)
2016
Define operators. What are the different types
of operators (2.5 marks)
2019
Discuss the various operators in C. (5 marks)
50. • Operators allow us to perform different kinds
of operations on operands.
53. In C, operators can be categorized in following
categories:
1. Arithmetic Operators
Binary operators : (+, -, *, /, %)
Unary Operators : (post-increment, pre-
increment, post-decrement, pre-decrement)
(Arithmetic Operators)
54. • The binary operators falling in this category
are:
– Addition: The ‘+’ operator adds two operands.
For example, x+y.
– Subtraction: The ‘-‘ operator subtracts two
operands.
For example, x-y.
– Multiplication: The ‘*’ operator multiplies two
operands.
For example, x*y.
– Division: The ‘/’ operator divides the first operand
by the second.
For example, x/y.
55. – Modulus: The ‘%’ operator returns the remainder
when first operand is divided by the second.
For example, x%y.
60. Restrictions of the modulo operator
• The % operator cannot be applied to floating-
point numbers i.e float or double.
• If you try to use the modulo operator with
floating-point constants or variables, the
compiler will produce a error:
66. In C, operators can be categorized in following
categories:
1. Arithmetic Operators
Binary operators : (+, -, *, /, %)
Unary Operators : (post-increment, pre-
increment, post-decrement, pre-decrement)
(Arithmetic Operators)
67. • The ones falling into the category of unary
arithmetic operators are:
• Increment: The ‘++’ operator is used to
increment the value of an integer.
• When placed before the variable name (also
called pre-increment operator), its value is
incremented instantly.
• For example, ++x.
And when it is placed after the variable name
(also called post-increment operator), its
value is preserved temporarily until the
execution of this statement and it gets
updated before the execution of the next
statement. For example, x++.
68. • Decrement: The ‘ – – ‘ operator is used to
decrement the value of an integer.
• When placed before the variable name (also
called pre-decrement operator), its value is
decremented instantly.
• For example, – – x.
And when it is placed after the variable name
(also called post-decrement operator), its
value is preserved temporarily until the
execution of this statement and it gets
updated before the execution of the next
statement.
• For example, x – –.
75. Relational operators
• Relational operators are used for comparison
of two values.
For example,
less than, greater than, equal to etc.
• Let’s see them one by one
76. 1. Equal to operator:
Represented as ‘==’, the equal to operator
checks whether the two given operands are
equal or not.
If so, it returns true.
Otherwise it returns false.
For example,
1. 5==5 will return true.
2. 6==5 will return false.
77. 2. Not equal to operator:
Represented as ‘!=’, the not equal to operator
checks whether the two given operands are
equal or not.
If not, it returns true.
Otherwise it returns false.
It is the exact boolean complement of
the ‘==’ operator.
For example,
1. 5!=5 will return false.
2. 6!=5 will return true
78. 3. Less than operator:
Represented as ‘<‘, the less than operator
checks whether the first operand is lesser than
the second operand.
If so, it returns true.
Otherwise it returns false.
For example,
1. 5<5 will return false.
2. 5<6 will return true.
79. 4. Less than or equal to operator:
Represented as ‘<=’, the less than or equal
tooperator checks whether the first operand is
less than or equal to the second operand.
• If so, it returns true
• else false.
For example,
1. 2<=5 will return true
2. 6<=5 will return false.
3. 5<=5 will return true.
80. 5. Greater than operator:
• Represented as ‘>’, the greater than operator
checks whether the first operand is greater
than the second operand or not.
• If so, it returns true.
• Otherwise it returns false.
For example,
1. 6>5 will return true
2. 5>5 will return false
81. 6. Greater than or equal to operator:
Represented as ‘>=’, the greater than or equal to
operator checks whether the first operand is
greater than or equal to the second operand.
• If so, it returns true
• else it returns false.
For example,
7>=5 will return true.
6>=8 will return false.
5>=5 will return true.
88. Logical Operators
1. Logical AND operator:
• The ‘&&’ operator returns true when both the
conditions under consideration are satisfied.
Otherwise it returns false.
• For example, a && b returns true when both a
and b are true (i.e. non-zero).
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
Using 2 variables we can have 4 combinations
89. 2. Logical OR operator:
The ‘||’ operator returns true even if one (or
both) of the conditions under consideration is
satisfied. Otherwise it returns false.
For example, a || b returns true if one of a or b
or both are true (i.e. non-zero).
Of course, it returns true when both a and b are
true.
A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1
Using 2 variables we can have 4 combinations
90. 3. Logical NOT operator:
• Logical NOT is denoted by exclamatory
characters (!), it is used to check the opposite
result of any given test condition.
• If any condition's result is non-zero (true), it
returns 0 (false) and if any condition's result is
0(false) it returns 1 (true).
A !A
0 1
1 0
Using 1 variables we can have 2 combinations
97. Assignment Operators in C
• Assignment operators are used to assign
value to a variable.
• The left side operand of the assignment
operator is a variable and right side operand
of the assignment operator is a value.
• For example :
int a; // variable a is declare and define
a= 5; // a is assigned value 5.
98. • Different types of assignment operators are
shown below:
1. “=”:
This is the simplest assignment operator.
This operator is used to assign the value on the
right to the variable on the left.
For example :
int a; a = 10;
99. 2. “+=”:
• This operator is combination of ‘+’ and ‘=’
operators.
+= is known as compound assignment.
• This operator first adds the current value of
the variable on left to the value on the right
and then assigns the result to the variable on
the left.
For Example
(a += b) can be written as (a = a + b)
If initially value stored in a is 5 and b is 6
Then (a += 6) OR (a=a+6)
a=5+6=11
100. 3. “-=”
• This operator is combination of ‘-‘ and ‘=’
operators
-= is known as compound assignment
• This operator first subtracts the current value
of the variable on left from the value on the
right and then assigns the result to the
variable on the left.
For Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8 and b is 6
Then (a -= 6) OR a=a-6
a=8-6 = 2
101. 4. “*=”
This operator is combination of ‘*’ and ‘=’
operators
*= is known as compound assignment
• This operator first multiplies the current
value of the variable on left to the value on
the right and then assigns the result to the
variable on the left.
Example:
• (a *= b) can be written as (a = a * b)
• If initially value stored in a is 5 and b is 6
• Then (a *= 6) = 30.
102. 5. “/=”
• This operator is combination of ‘/’ and ‘=’
operators
/= is known as compound assignment
• This operator first divides the current value
of the variable on left by the value on the
right and then assigns the result to the
variable on the left.
Example:
• (a /= b) can be written as (a = a / b)
• If initially value stored in a is 6 and b is 2
• Then (a /= 2) a=(6/2)=3.
103. 6. “%=”
• This operator is combination of ‘%’ and ‘=’
operators
%= is known as compound assignment
• This operator first modulus the current value
of the variable on left by the value on the
right and then assigns the result to the
variable on the left.
Example:
• (a %= b) can be written as (a = a % b)
• If initially value stored in a is 6 and b is 2
• Then (a %= 2) a=(6%2)=0.
108. “%=”
• This operator is combination of ‘%’ and ‘=’
operators
%= is known as compound assignment
• This operator first modulus the current value
of the variable on left by the value on the
right and then assigns the result to the
variable on the left.
Example:
• (a %= b) can be written as (a = a % b)
• If initially value stored in a is 6 and b is 2
• Then (a %= 2) a=(6%2)=0.
109.
110.
111. 2017
• Explain bitwise operators available in C. (2.5
marks
2016
what are bitwise operators?( 2.5 marks)
114. Bitwise Operators in C
• The & (bitwise AND) in C takes two numbers
as operands and does AND on every bit of
two numbers. The result of AND is 1 only if
both bits are 1.
Example :
2 in binary 1 0
3 in binary 1 1
2&3 ____________________________
1 0 = 2
_____________________________
LSB
MSB
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
Truth table
115. Bitwise Operators in C
• The | (bitwise OR) in C takes two numbers as
operands and does OR on every bit of two
numbers.
• The result of OR is 1 if any of the two bits is 1.
Example :
2 in binary = 1 0
3 in binary = 1 1
2 | 3 ____________________
1 1 = 3
______________________
LSB
MSB
A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1
Truth table
116. Bitwise Operators in C
• The ^ (bitwise XOR) in C takes two numbers as
operands and does XOR on every bit of two
numbers. The result of XOR is 1 if the two bits
are different.
Example :
2 in binary = 1 0
3 in binary = 1 1
2^3 ___________________
0 1 = 1
____________________
A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
MSB LSB
Truth table
Note:
When both the operand
are same we get 0
Otherwise 1
120. 1. Bitwise Left Shift Operator <<
2. Bitwise Right Shift Operator >>
3. Bitwise One's Complement ~
Bitwise Operators in C
121. Bitwise Operators in C
• The << (left shift) in C takes two numbers.
• left shift operator shifts the bits of the first
operand and the second operand decides the
number of places to shift.
Example :
• First Operand : 8
8 in binary = 1 0 0 0
• Second Operand : 3 ( so shift the operand by 3
bits)
=Shift 8 by 3 bits left = 8<<3
= 8*(2 pow 3)
=8*8
=64 in binary = 1 0 0 0 0 0 0
Short cut trick is append
3 0’s in right side of first
operand
122. • The >> (right shift) in C takes two numbers.
• right shift operator shifts the bits of the first
operand and the second operand decides the
number of places to shift.
Example :
• First Operand : 8
8 in binary = 1 0 0 0
• Second Operand : 3 (so shift the operand by 3
bits)
=Shift 8 by 3 bits right = 8>>3
= 8/(2 pow 3)
=8/8
=1 in binary = 0 0 0 1
Bitwise Operators in C
123. • The ~ (bitwise 1’s complement) in C takes
one number and inverts all bits of it
Example :
Number 8 in binary 0 1 0 0 0
1’s complement 1 0 1 1 1
of 8 is (-8)
-8 in binary = 10111
Negative numbers are stored in 2’s
complement form :
• Short trick to calculate 1’s complement :
~n=-(n+1)
• n=8 = - (8+1) = -9
MSB bit is 0 means number is
positive
MSB bit is 1 means number is
negative
128. Conditional or Ternary Operator (?:)
Working:
Here, Expression1 is the condition to be evaluated.
If the condition(Expression1) is True then Expression2 will be
executed and the result will be returned.
Otherwise, if the condition(Expression1) is false
then Expression3 will be executed and the result will be
returned.
135. if statement in C
• if statement is the most simple decision
making statement.
• It is used to decide whether a certain
statement or block of statements will be
executed or not i.e
• if a certain condition is true then a block of
statement is executed otherwise not.
136.
137. • Here, condition after evaluation will be either
true or false.
• if statement accepts boolean values – if the value
is true then it will execute the block of statements
below it otherwise not.
• If we do not provide the curly braces ‘{‘ and ‘}’
after if(condition) then by default if statement
will consider the first immediately below
statement to be inside its block.
138.
139. if-else in C
• The if statement alone tells us that if a
condition is true it will execute a block of
statements and if the condition is false it
won’t.
• But what if we want to do something else if
the condition is false.
• Here comes the C else statement.
• We can use the else statement
with if statement to execute a block of code
when the condition is false.
145. 2019
• Explain the if..else statement with all its
variations. Compare it with switch case.
Write a c program that demonstrate the
difference between the two.(7 marks)
146. if-else-if ladder in C
• Here, a user can decide among multiple
options.
• The C if statements are executed from the top
down.
• As soon as one of the conditions controlling
the if is true, the statement associated with
that if is executed, and the rest of the C else-if
ladder is bypassed.
• If none of the conditions are true, then the
final else statement will be executed.
155. • What are nested control statements? Give
examples(2.5 marks)
156. nested-if in C
• A nested if in C is an if statement that is the
target of another if statement.
• Nested if statements means an if statement
inside another if statement.
• Yes, both C and C++ allows us to nested if
statements within if statements, i.e, we can
place an if statement inside another if
statement.
157.
158.
159.
160.
161. Jump Statements in C/C++
• These statements are used in C orC++ for
unconditional flow of control through out the
funtions in a program.
• They support four type of jump statements:
162. 2017
What is the function of break and continue
statement in C? (2.5 marks)
2016
Differentiate between break and continue.
163. • C break:
• This loop control statement is used to terminate
the loop.
• As soon as the break statement is encountered
from within a loop, the loop iterations stops
there and control returns from the loop
immediately to the first statement after the loop.
Syntax:
break;
• Basically break statements are used in the
situations when we are not sure about the actual
number of iterations for the loop or we want to
terminate the loop based on some condition.
164.
165. 2. C continue: This loop control statement is just
like the break statement.
The continue statement is opposite to that of
break statement, instead of terminating the
loop, it forces to execute the next iteration of
the loop.
As the name suggest the continue statement
forces the loop to continue or execute the next
iteration.
When the continue statement is executed in the
loop, the code inside the loop following the
continue statement will be skipped and next
iteration of the loop will begin.
169. The GOTO STATEMENT
• A goto statement requires a label in order to
identify the place where the branch is to be
made.
• A label is any valid variable name,and must
be followed by a colon.
• The label is placed immediately before the
statement where the control is to be
transferred.
• The general forms of goto and label
statements are shown in next slide
170.
171. The label: can be anywhere in the program
either before or after the goto label; statement.
During running of a program when a statement
like
goto begin;
is met, the flow of control will jump to the
statement immediately following the label
begin:.
This happens unconditionally.
172. • Note that a goto breaks the normal
sequential execution of the program.
• If the label: is before the statement goto
label; a loop will be formed and some
statements will be executed repeatedly.
• Such jump is known as a backward jump
• On the other hand, if the label : is placed after
goto label; some statements will be skipped
and the jump is known as a forward jump.
173. 2016
• Why does goto statement is not preferred in
c programming (2.5 marks)
174. Why Goto should be avoided
Due to the unconditional goto statement at the
end, the control is always transferred back to
the input statement.
In fact, program puts the computer in a
permanent loop known as an infinite loop.
The computer goes round and round until we
take some special steps to terminate the loop.
Such infinite loops should be avoided.
185. • C return:
• The return in C or C++ returns the flow of the
execution to the function from where it is
called.
• This statement does not mandatorily need any
conditional statements.
• As soon as the statement is executed, the
flow of the program stops immediately and
return the control from where it was called.
• The return statement may or may not return
anything for a void function, but for a non-
void function, a return value is must be
returned.
Syntax: return[expression];
188. 2017
• Explain loop statements in c. (6.5 marks)
2016
• List the different types of loop control
statements and explain them with suitable
examples( 6 marks)
2019
• Explain the different loop statements in c
through suitable examples.(5 marks)
189. Types of C Loops
• There are three types of loops in C
language that is given below:
1. do while
2. while
3. for
190. do-while loop in C
• The do-while loop continues until a given
condition satisfies.
• It is also called post tested loop.
• It is used when it is necessary to execute the
loop at least once (mostly menu driven
programs).
193. Decision making using if-else
statement
• In if statement if a condition is true it will
execute a block of statements.
• But what if we want to execute the statements
if the condition is false.
• So we require else statement.
211. • In if-else-if ladder statement, if a condition is
true then the statements defined in the if
block will be executed.
• otherwise if some other condition is true then
the statements defined in the else-if block will
be executed.
• at the last if none of the condition is true then
the statements defined in the else block will
be executed.
• There are multiple else-if blocks possible.
218. • The switch statement is a multiway branch
statement.
• It provides an easy way to dispatch execution
to different parts of code based on the value
of the expression.
• Switch is a control statement that allows a
value to change control of execution
219. Rules for switch statement
1) The switch expression must be of an integer
or character type.
2) The case value must be an integer or
character constant.
3) The case value can be used only inside the
switch statement.
4) The break statement in switch case is
optional.
• If there is no break statement found in the
case, all the cases will be executed present
after the matched case.
220. 5) The default statement is executed if no case
constant-expression value is equal to the value
of expression
227. • The goto statement is known as jump
statement.
• As the name suggests, goto is used to transfer
the program control to a predefined label.
• The goto statment can be used to repeat some
part of the code for a particular condition.
• Goto is known as unconditional Jump
237. • The break is a keyword in C which is used to
bring the program control out of the loop.
• The break statement is used inside loops or
switch statement.
Break statement
244. • The continue statement brings the program
control to the starting of the loop.
• The continue statement skips some lines of
code inside the loop and continues with the
next iteration.
• It is mainly used for a condition so that we can
skip some code for a particular condition.
250. • Loops in programming come into use when
we need to repeatedly execute a block of
statements.
• For example:
• Suppose we want to print “Hello World” 10
times.
• This can be done in two ways as shown below
1. Iterative Method
2. Using Loops
251. 1. Iterative Method
• An iterative method can be used to write the
printf() statement 10 times.
254. 2. Using Loops
• In Loop, the statement needs to be written
only once and the loop will be executed n
times
• In our case printf(“Hellon”) is written only
ones and Hello is printed 10 times using loop.
• A loop is a sequence of instructions that is
repeated until a certain condition is reached.
255. There are mainly two types of loops:
1. Entry Controlled loops: In this type of loops
the test condition is tested before entering
the loop body.
• For Loop and While Loop are entry controlled
loops.
2. Exit Controlled Loops: In this type of loops
the test condition is tested at the end of loop
body.
• Therefore, the loop body will execute atleast
once, irrespective of whether the test
condition is true or false.
• do – while loop is exit controlled loop.
256. 1. for Loop
• A for loop is a repetition control structure
which allows us to write a loop that is
executed for a specific number of times.
Syntax of for loop
1 2
3
4
261. Types of C Loops
• There are three types of loops in C
language that is given below:
1. for
2. do-while
3. while
262. 2. do-while loop in C
• The do-while loop continues until a given
condition satisfies.
• It is used when it is necessary to execute the
loop at least once
• do-while loop is a exit controlled Loop.
Syntax of do-while loop
277. What is infinite loop?
• An infinite loop is a looping construct that
does not terminate the loop and executes the
loop forever.
• It is also called an indefinite loop or
an endless loop.
278. • We can create an infinite loop using following
ways:
1. for loop
2. while loop
3. do-while loop
4. go to statement
5. C macros
279. 1. For loop
all the parts of the 'for' loop are optional,
and in the above for loop, we have not
mentioned any condition; so, this loop will
execute infinite times.
Syntax
296. • A type cast is basically a conversion from one
type to another.
• There are two types of type conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion
297. • Also known as ‘automatic type conversion’.
• Done by the compiler on its own.
• All the data types of the variables are
upgraded to the data type of the variable with
largest data type.
1. Implicit Type Conversion
300. 2. Explicit Type Conversion
• This process is also called type casting and it is
user defined.
• Here the user can type cast the result to make
it of a particular data type.
• Data can be lost when explicit conversion is
done.
Lower
data type
Higher data
type
309. Solution
• In program "Expression syntax" error occur
because the while() loop must have
conditional expression.
310. Question 2
• The break statement is used to take control
out of switch and continue statement is used
to take control of the beginning of the switch?
• The above statement is true or false
311. Solution
• No, because continue statement can work
only with loops in C-programming and not
with switch.
313. Solution
• A character variable can at a time store only
one character. In fact, if we execute the
following statements, what gets stored in
variable ch is not really the character constant,
but the ASCII value of 'A', which is 65.
318. • A function is a set of statements that take
inputs, do some specific computation and
produces output.
• The idea is to put some commonly or
repeatedly done task together and make a
function so that instead of writing the same
code again and again for different inputs, we
can call the function.
320. Types of Functions
• There are two types of functions in C
programming:
• Library Functions: are the functions which are
declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
• User-defined functions: are the functions
which are created by the C programmer, so
that he/she can use it many times. It reduces
the complexity of a big program and optimizes
the code.
321. Different aspects of function calling
1. function without arguments and without
return value
2. function without arguments and with return
value
3. function with arguments and without return
value
4. function with arguments and with return
value
322.
323. for Loop
• A for loop is a repetition control structure
which allows us to write a loop that is
executed a specific number of times.
• The loop enables us to perform n number of
steps together in one line.
324. • In for loop, a loop variable is used to control
the loop.
• Initialization Expression: In this expression we
have to initialize the loop counter to some
value.
• For example: int i=1;
• Test Expression: In this expression we have to
test the condition.
• If the condition evaluates to true then we will
execute the body of loop and go to update
expression otherwise we will exit from the for
loop.
For example: i <= 10;
325. • Update Expression:
• After executing loop body this expression
increments/decrements the loop variable by
some value.
• For example: i++;
326.
327. 2018
Explain while loop and do-while loop with an
example. Elaborate difference between them(5
marks).
2016
Differentiate between entry control loop and
exit control loop.(2.5 marks)
328. while loop
• A while loop is a control flow statement that
allows code to be executed repeatedly based on
a given Boolean condition.
• The while loop can be thought of as a repeating if
statement. and therefore is an example of Entry
Control Loop.
Syntax :
while (boolean condition)
{
loop statements...
}
329.
330. do-while loop
• do while loop is similar to while loop with the
only difference that it checks for the condition
after executing the statements, and therefore is
an example of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);
331.
332.
333.
334.
335. 2015
• Write a program to display first n prime
numbers(6 marks)
339. Switch Statement in C
• Switch case statements are a substitute for
long if statements that compare a variable to
several integral values
• The switch statement is a multiway branch
statement.
• It provides an easy way to dispatch execution
to different parts of code based on the value
of the expression.
• Switch is a control statement that allows a
value to change control of execution.
340. Important Points about Switch Case
Statements:
1. The expression provided in the switch should
result in a constant value otherwise it would
not be valid.
Valid expressions for switch:
341. 1. Duplicate case values are not allowed.
• The default statement is optional. Even if the
switch case statement do not have a default
statement,
it would run without any problem.
2. The break statement is used inside the switch to
terminate a statement sequence.
• When a break statement is reached, the switch
terminates, and the flow of control jumps to the
next line following the switch statement.
3. The break statement is optional. If omitted,
execution will continue on into the next case.
• The flow of control will fall through to
subsequent cases until a break is reached.
342. 4. Nesting of switch statements are allowed,
which means you can have switch statements
inside another switch.
However nested switch statements should be
avoided as it makes program more complex and
less readable.
367. • Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without any
external trigger from the user.
• Generally takes place when in an expression
more than one data type is present.
• In such condition type conversion (type
promotion) takes place to avoid lose of data.
• All the data types of the variables are
upgraded to the data type of the variable
with largest data type.
368. • bool -> char -> short int -> int -> unsigned int
-> long -> unsigned -> long long -> float ->
double -> long double
372. • This process is also called type casting and it is
user defined.
• Here the user can type cast the result to make
it of a particular data type.
The syntax in C:
(type) expression
375. Advantages of Type Conversion
• This is done to take advantage of certain
features of type hierarchies or type
representations.
• It helps us to compute expressions containing
variables of different data types.
380. Comma in C
1) Comma as an operator:
The comma operator (represented by the token,
) is a binary operator that evaluates its first
operand and discards the result, it then
evaluates the second operand and returns this
value (and type).
• The comma operator has the lowest
precedence of any C operator, and acts as
a sequence point.