Unit 2
Unit 2
Prof. Athulya S
Department of Computer Science and Engineering
is used %d int
used for I/O signed integer
value
Syntax:
%c char Used for I/O character value
scanf (“format”, &num1, &num2,….);
● Here, format is the format %f float
Used for I/O decimal floating-
point value
specification string.
● The function scanf() reads %s string
Used for I/O string/group of
characters
and converts characters from the
standards input depending on the
Used for I/O long signed
format specification string and stores %ld long int
integer value
the input in memory locations
represented by the other arguments unsigned Used for I/O unsigned integer
%u
int value
(num1, num2,….).
Used for I/O fractional or
Example: %lf double
floating data
scanf(“%c%d”, &Grade, &Roll No);
Formatted Output (Continued…)
Format Data
Description
Specifier Type
● For formatted output, printf() function is
used. used for I/O signed integer
%d int
value
Syntax:
%c char Used for I/O character value
printf (“format”, data1, data2,……..);
● Here, format is the format %f float
Used for I/O decimal floating-
point value
specification string.
● String contains a specification beginning Used for I/O string/group of
with the symbol % followed by a %s string characters
Between the character % and the conversion character, there may be:
• A minus sign: Denoting left adjustment of the data.
• A digit: Specifying the minimum width in which the data is to be output, if the data has
a larger number of characters then the specified width occupied by the output is larger.
If the data consists of fewer characters then the specified width, it is padded to the right
or to the left (if minus sign is not specified) with blanks. If the digit is prefixed with a
zero, the padding is done with zeros instead of blanks.
• A period: Separating the width from the next digit.
• A digit following the period: specifying the precision (number of decimal places for
numeric data) or the maximum number of characters to be output.
• Letter L: To indicate that the data item is a long integer and not an int.
Control Statements
Control Statements
Selection
Iterative Statements Transfer Statements
Statements
I. Selection Statements
Output:
Program to check whether the entered character is vowel or
consonant.
Quadratic Equation
Quadratic Equation (Continued…)
Program to find the roots and nature of a quadratic equation.
Output
Iterative Statements
● Syntax
● Example
“for” Loop (Continued…)
In for loop, a loop variable is used to control the loop. Firstly we initialize the loop
variable with some value, then check its test condition. If the statement is true then
control will move to the body and the body of for loop will be executed. Steps will
be repeated till the exit condition becomes true. If the test condition will be false
then it will stop.
• Initialization Expression: In this expression, we assign a loop variable or loop
counter to some value. for example: int i=1;
• Test Expression: In this expression, test conditions are performed. If the
condition evaluates to true then the loop body will be executed and then an update
of the loop variable is done. If the test expression becomes false then the control
will exit from the loop. for example, i<=9;
• Update Expression: After execution of the loop body loop variable is updated by
some value it could be incremented, decremented, multiplied, or divided by any
value.
“for” Loop (Continued…)
Output:
“while” Loop (Continued…)
Output:
“do-while” Loop (Continued…)
● The do-while loop is similar to a while loop but the only difference lies in the
do-while loop test condition which is tested at the end of the body. In the do-
while loop, the loop body will execute at least once irrespective of the test
condition.
● Syntax:
“do-while” Loop (Continued…)
Output:
Description of All Types of Loops
(Continued…)
Description of All Types of Loops
Difference in Output using “while” and “do-while” Loop
Output: Output:
Transfer Statements
● Unconditional branching
● Four Types of branching control statements
i. break
ii. continue
iii. goto
iv. return
i. break Statement
Output:
ii. continue Statement
Output:
iii. goto Statement
● The C goto statement is a jump statement which is sometimes also referred to
as an unconditional jump statement. The goto statement can be used to jump
from anywhere to anywhere within a function.
● goto is a Keyword and Label is an identifier
● In the above syntax, the first line tells the compiler to go to or jump to the
statement marked as a label. Here, the label is a user-defined identifier that
indicates the target statement. The statement immediately followed after
‘label:’ is the destination statement. The ‘label:’ can also appear before the
‘goto label;’ statement in the above syntax.
iii. goto Statement (Continued…)
…
iv. return Statement
●}