Unit Ii
Unit Ii
Conditional/Decision Statements: if, if-else, nested if-else, else-if ladder, and switch-
statement with suitable illustrative programs.
Control Structures: while, do-while and for with suitable illustrative programs.
SAMPLE QUESTIONS
Category
Associativity
3) X=3*4/5+10/5+8-2+7/8
=12/5+10/5+8-2+7/8
=2+2+8-2+0
=4+6+0
=10+0
=10
4) X=2*3+5
=6+5
=11
5) X=2*(3+5)
=2*8
=16
6) X=2*(3+5*6)
=2*(3+30)
=2*33
=66
Type Conversion in C
Type conversion in C is the process of converting one data type to another. The type
conversion is only performed to those data types where conversion is possible. Type
conversion is performed by a compiler. In type conversion, the destination data type can’t be
smaller than the source data type. Type conversion is done at compile time and it is also
called widening conversion because the destination data type can’t be smaller than the source
data type. There are two types of Conversion:
1. Implicit Type Conversion
Implicit type conversion is also called automatic type conversion. Some of its few
occurrences are mentioned below:
Conversion Rank
Conversions in Assignment Expressions
Conversion in other Binary Expressions
Example of Type Implicit Conversion
Example no 1
// An example of implicit
conversion
#include <stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to
int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted
to float
float z = x + 1.0;
This process is also called type casting and it is user-defined. Here the user can typecast the
result to make it of a particular data type. The syntax in C Programming:
(type) expression
Type indicated the data type to which the final result is converted.
Example no 2
int main()
{
double x = 1.2;
return 0;
}
Output
sum = 2
Example no 3
int main() {
float a = 1.5;
int b = (int)a;
printf("a = %f\
n", a);
printf("b = %d\
n", b);
return 0;
}
Output
a = 1.500000
b = 1
Control Structures
Definition:-The structure which controls the flow of execution of the statements is called
control structure.Control structures are classified into three types:
1) Sequence 2) Selection 3) Iteration
Sequence: In this, the statements are executed one by one from start to end.
Selection: In this, some statements are executed based on condition result only.
Iteration: In this, some statements are executed repeatedly as long as condition is
true.
Decision making is an important part of programming. Every programming language supports
decision making statements allowing programmers to branch according to the condition. In C
programming language, if statement is used to check condition and make decision. The decisions or
statements are enclosed inside curly braces, however if only a single statement has to be executed,
curly braces are not mandatory. Depending upon the number of conditions to be checked, we have
following types of if statement:
1. if statement
2. if … else statement
3. if … else if … else statement
4. Nested if
5. switch
if statement
if statement is used for branching when a single condition is to be checked. The condition enclosed in
if statement decides the sequence of execution of instruction. If the condition is true, the statements
inside if statement are executed, otherwise they are skipped. In C programming language, any non
zero value is considered as true and zero or null is considered false.
Syntax of if statement
if (condition)
statements;
Flowchart of if statement
Example of if statement
#include<stdio.h>
int main()
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n<10)
printf("Square = %dn",n*n);
return 0;
This program is an example of using if statement. A number is asked from user and stored in
variable n. If the value of n is less than 10, then its square is printed on the screen. If the condition is
false the program, execution is terminated.
Output
Enter a number:6
6 is less than 10
Square = 36
if … else statement
if … else statement is a two way branching statement. It consists of two blocks of statements each
enclosed inside if block and else block respectively. If the condition inside if statement is true,
statements inside if block are executed, otherwise statements inside else block are executed. Else
block is optional and it may be absent in a program.
if (condition)
statements;
else
statements;
#include<stdio.h>
int main()
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2 == 0)
printf("%d is even",n);
else
printf("%d is odd",n);
return 0;
Here, a number is entered by user which is stored in n. The if statement checks if the remainder of that
number when divided by 2 is zero or not. If the remainder is zero, the number is even which is printed
on the screen. If the remainder is 1, the number is odd.
Note: If there is only one statement inside if block, we don’t need to enclose it with curly brackets { }.
Enter a number:18
18 is even
Enter a number:33
33 is odd
It is used when more than one condition is to be checked. A block of statement is enclosed inside if,
else if and else part. Conditions are checked in each if and else if part. If the condition is true, the
statements inside that block are executed. If none of the conditions are true, the statements inside else
block are executed. A if … else if … else statement must have only one if block but can have as many
else if block as required. Else part is optional and may be present or absent.
if (condition 1)
statements;
else if (condition 2)
statements;
else if (condition n)
statements;
else
statements;
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
if(n<0)
printf("Number is negative");
else if(n>0)
printf("Number is positive");
else
return 0;
In this program, a number is entered by user stored in variable n. The if … else if … else statement
tests two conditions:
Output
Enter a number:109
Number is positive
Enter a number:-56
Number is negative
Enter a number:0
Nested if statements
When a if statement is kept inside another if statement, it is called nested if statement. Nested if
statements are used if there is a sub condition to be tested. The depth of nested if statements depends
upon the number of conditions to be checked.
statements;
if (sub condition 1)
statements;
statements;
else if (condition 2)
statements;
if (sub condition 2)
statements;
statements;
else
statements;
if (sub condition n)
statements;
Example 4: C program to check if a number is less than 100 or not. If it is less than 100 then
check if it is odd or even.
#include<stdio.h>
int main()
int n;
printf("Enter a number:");
if(n<100)
if(n%2 == 0)
printf("%d is even",n);
else
printf("%d is odd",n);
else
return 0;
Output
Enter a number:46
46 is even
Enter a number:67
67 is odd
switch
The switch-case statement is used to check the multiple conditions at a time.
1) switch without break
2) switch with break.
1) switch without break;-
Syntax:- switch(expression)
{ case value-1: statement1;
case value-2: statement2;
case value-3: statement3;
case value-4: statement4;
------------------------------
[default:statement n;]
}
Execution procedure:-
First the expression will be evaluated and it must return a constant value like numeric
or character.
Now , the expression constant value is compared with the case constant values like
value-1, value-2, value-3,..........etc.
If the expression constant value is match with any case value then the execution start
from that corresponding case statements to last statement of the switch block.
LOOPS:-
1. while loop
2. do-while loop and
3. for loop.
1) while loop:-
while loop is also called as Entry controlled loop.
Syntax:-
.................
Flow chart:-
.................
While block
Syntax:-
Flow chart
Geeks
Geeks
Geeks
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
When the condition is false, the body is not The body of the do…while loop is executed at least
executed not even once. once even when the condition is false.
for loop
The for loop in C Language provides a functionality/feature to repeat a set of statements a defined
number of times. The for loop is in itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization, condition, and
updating statements as part of its syntax. It is mainly used to traverse arrays, vectors, and other data
structures.
Syntax of for Loop
for(initialization; check/test expression; updation)
{
// body consisting of multiple statements
}
Step 1: Initialization is the basic step of for loop this step occurs only once during the start of the
loop. During Initialization, variables are declared, or already existing variables are assigned some
value.
Step 2: During the Second Step condition statements are checked and only if the condition is the
satisfied loop we can further process otherwise loop is broken.
Step 3: All the statements inside the loop are executed.
Step 4: Updating the values of variables has been done as defined in the loop.
Continue to Step 2 till the loop breaks.
for(i=0;k<10;i++)
{
printf(“%d “,i);
}
for(i=0;k<10;)
{
printf(“%d “,i);
i++;
}
int i=0;
for(; i<10;i++)
{
printf(“%d “,i);
int i=0;
for(; i<10;)
{
printf(“%d “,i);
i++;
}
int main()
// conditional statement
for (i = 1; i <= 5; i++)
{
// statement will be printed
printf("Anurag University\n");
}
break statement
continue statement and
goto statement.
Break statement:- The break statement is used for terminating the loop or block . That
means when the break statement is occurred at that time the loop gets the termination.
Examples:-
while(condition) do for(init;cond;inc/dec)
{ {statement1; { statement1;
Statement1; if(condition) if(condition)
if(condition) {satement2; { statement2;
{ statement2; continue; continue;
continue; }while(condition); }
} }
}
Write a C program for displaying numbers from 1 to n except which are divisible by 3.
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,n;
clrscr( ) ;
printf(“Enter n value:\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
if(i%3= =0)
{ i=i+1;
continue;
goto statement:- It is used for transferring the control from one location to another location
of the program.
Syntax:-
goto label;
un-conditional goto
goto
conditional goto
even:
printf("%d is even", num);
// return if even
return;
odd:
printf("%d is odd", num);
}
int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}
Output
26 is even
C
// C program to print elements of Three-Dimensional Array
// with the help of nested for loop
#include <stdio.h>
int main()
{
// initializing the 3-D array
int arr[2][3][2]
= { { { 0, 6 }, { 1, 7 }, { 2, 8 } },
{ { 3, 9 }, { 4, 10 }, { 5, 11 } } };
while(condition) {
C
// C program to print pattern using nested while loops
#include <stdio.h>
int i = 1;
do{
while(condition) {
C
// C Program to print all prime factors
// of a number using nested loop
#include <math.h>
#include <stdio.h>
C
// C program to show working of break statement
// inside nested for loops
#include <stdio.h>
int main()
{
int i = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
// This inner loop will break when i==3
if (i == 3) {
break;
}
printf("* ");
}
printf("\n");
}
return 0;
}
Output
***
***
***
***
In the above program, the first loop will iterate from 0 to 5 but here if i will be equal to 3 it will break
and will not print the * as shown in the output.
Continue Inside Nested loops
Whenever we use a continue statement inside the nested loops it skips the iteration of the innermost
loop only. The outer loop remains unaffected.
Example:
C
int main()
{
int i = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
// This inner loop will skip when j==2
if (j==2) {
continue;
}
printf("%d ",j);
}
printf("\n");
}
return 0;
}