0% found this document useful (0 votes)
20 views24 pages

Notes Comp. Fundamental

The document provides an overview of the C programming language, detailing its history, importance, elements, data types, constants, variables, and structure of a C program. It highlights C's robustness, portability, and its foundational role in system and application software development. Additionally, it covers key concepts such as identifiers, keywords, operators, and functions like printf() and scanf().

Uploaded by

m05162800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views24 pages

Notes Comp. Fundamental

The document provides an overview of the C programming language, detailing its history, importance, elements, data types, constants, variables, and structure of a C program. It highlights C's robustness, portability, and its foundational role in system and application software development. Additionally, it covers key concepts such as identifiers, keywords, operators, and functions like printf() and scanf().

Uploaded by

m05162800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT 2ND NOTES

(Overview of C)

 HISTORY OF C LANGUAGE: -

C is a general-purpose procedural programming. Language. It is developed at AT & TC American


Telephone and Telegraph) Bells Laboratories of USA is 1972 by Denis Ritchie.

 Dennis Ritchie is known as the founder of the C Language. C has also greatly influenced
many other popular languages especially to C++ which was originally designed as an
enhancement to C.
 It is the most commonly used programming language for writing system software though it
is also widely used for writing applications.
 C is structured programming language that is machine independent and used to write
various applications like windows and many other complex programs.
 C is one of the high-level programming languages develop by Denis Ritche
 C was originally developed for UNIX operating system to beat the issues of previous
language such as B, BCPL etc.
 The Unix operating system development started in the year 1967 and its code was rewritten
in C in the year 1972.
 Now a days C is exclusively used for building Os, application packages and customized
software because of its power and efficiency.
 C is increasingly used by system programmers’ application developers and researches for a
spread of programming tasks.
 It is considered a high-level language because it allows the programmer to concentrate on
the problem at hand and not worry about the machine that the program will be using.

 IMPORTANCE OF C LANGUAGE: -

 C is robust language and has rich set of built in functions, data types and operators which
can be used to write any complex program.
 C has the capabilities of an assembly language with the feature of high-level language so it is
well suited for writing both system software and application software.
 C is highly portable language i.e. code writing in one machine can be moved to other which
is very important and powerful feature.
 C is a structured programming language which allow a complex program to be broken into
simple program called functions. It allows free movement of data across these function.
 C language is case sensitive which means lowercase and uppercase letters are treated
differently.
 C is a general-purpose programming language can efficiently work on enterprise applications
games, graphics etc.
 C programs are efficient and fast.
 C is an extensible programing language; this means that users can add their own function to
its library set.
 C language available various version Lite Turbo C, Borland C, ANSI C.
 C has a rich set of operators and provide compact representation to expressions.
 C language contain 32 keywords.
 ELEMENTS OF C: -

Character set = As every language contains a set of character used to construct word, Statements
etc. C language also has a set of character which include alphabets, digits and special symbols. C
language support a total of 256 character.

 Evay C program contains statements These statements are constructed using words and
these words are constructed using character from C character set C language character set
contained the following set of characters
1. Alphabets
2. Digits
3. Special Symbol

1) Alphabets: - C language support all the alphabets from English language Lower and
uppercase together support 52 alphabets. (A to Z and a to z).
2) Digits: - C language support 10 digits which are used to construct numerical values in C
Language (0 to 9).
3) Special symbols: - C language support a rich set of special symbols that include symbol to
perform mathematical operations to check conditions, which spaces, backspaces and other
special symbols like ~ @ # % ^ & * () _ - {} etc.

 IDENTIFIER: - An identifier is a word consisting of sequence of one or more letters or digits


along with a special character "_" (underscore). There are users defined names consists of a
sequence of letters and digits.

Rule of identifier: -

1) The first character of an identifies should either an alphabet or an underscore; and then it
can be followed by any of the character, digits or underscore.
2) Must Consist only of alphabetic character, digit or underscore No extra symbols and allowed
other than letters, digits and "_".
 They are always written in lowercase.

 DATA TYPE: -

A data type specifies the type of data that a variable can store such as integer; floating character etc
i.e. C has different data types for different type of data and can be broadly classified as: -

1) Primary data types & (primitive or basic data type).


2) Secondary data types.

C Data Types

PRIMARY

 Character
 Integer
 Float
 Double
 void

SECONDARY

 Array
 Pointers
 Structures
 Union
 Enum etc.

1. Integer: - Integer are whole numbers that can have both zero, positive and negative values
but no decimal values we can use “int” for declaring and integer.
The size of integer is usually 2. bytes-and its range limited to 12768 to +32767
Example: - int = datatype
Sum; = variable name
Syntax: - int < variable >;
Here int is a keyword which is used to define integer number
2. Floating data type: - The float data type is und to store fractional numbers (real numbers
with 6 digits of precision. Floating point numbers are denoted by the keyword “float” The
size of float is 4 bytes and range Limited to 3-4e-38 to 3-4е+38 A flout value consist of an
exponential and a fractional portion. Syntax float <variable names>

Example: - float salary;

3. Character: - character type variable can hold a single character. we can use "char" for
declaring on character.
The size of character is 1 byte and its range limited to -128 to 127

Syntax: - Char <variable name>

Example Char Ch;

 The char data type is used for storing character such as alphabets and special symbols e.g. *,
$, # Computer use a numerical code called ASCII (America standard code for information
interchange) to represent this character.

(A-Z) = 65-90

(a-z) = 97-122

4. Void: - Void is an empty data type. The void type has no values therefore we Cannot declare
it as variable The void data type is usually used with functions to specify its return type

DATATYPE SIZE RANGE VALUE


Char or signed char 1 -128 to 127
Unsigned char 1 0 to 225
Int or signed int 2 -32768 to 32767
Unsigned int 2 0 to 65535
Float 4 3.4e-38 to 3.4e+38
double 8 1.7e-308 to 1.7e+308
 CONSTANT: - Constants refer to the fixed values that the program may not celter during its
execution. These fixed values are also called literals. Constant can be any of the basic data
types like an integer constant, floating constant, character constant Co string constant.
1. Integer Constant: - An integer constant can be decimal, octal or hexadecimal constant. A
prefix specifies the base or radix: Ox and OX for hexadecimal O for octal and nothing for
decimal.
Example: - 85 Decimal
0215 Octal
0x4b Hexadecimal
2. Floating point constant: - A floating point constant has an integer part, a decimal point, a
fractional part and an Exponent part. You can represent floating point constant either in
decimal form or exponential form. The exponent is introduced by e or E
Example: - 3.1415
510E-5
3. Character constant: - character constant enclosed in single quotes e.g. "x" can be sorted in a
simple variable of char type.
Example: - ‘a’
‘A’
4. String constant: - The string constant is a sequence of characters enclosed in double quotes
(" "). It can represent text, including regular strings, alphanumeric characters, space
characters, and special characters. Example: "Hello, world!"

 VARIABLE: - A variable is a name of the memory location. It is used to store data. Its
value can be changed and it can be reused many times.
 It is the way to represent memory location through symbol so that it can be easily identified.

int a;

float;

char;

Here a, b, c are variable and int, float, char are data types.

 Rules for defining variable: -


1. A variable can have alphabet, digits and underscore.
2. A variable name can Start with alphabet, and underscore only. It cannot start with a digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.
 ASSIGNMENT STATEMENT: - C provide an assignment operator for this purpose, assigning
the value to variable using assignment operator is known as assignment statement in C. The
function of this operator is to assign the value or values in variable on right hand side of an
expression to variable in the left-hand side.

Syntax: - variable = value

Example: - a=10;

sum = 20;

 “=” is the assignment operator and sum, a are the name of variable.
 KEYWORDS: - Keywords are the reserved words which have predefined meaning in C
language. Since they are reserved for specific purpose that's why also known as reserved
words.
 They are 32 keywords in C
 These have predefined fixed meaning and uses cannot be changed for any other
purpose in a C program, hence cannot be used as variable names, function names
etc.
 They are basic building block of the program.
 A complete list of C keywords as follows: -

auto double int strict

break else long switch

case enum register type

char extern return union

cost float short unsigned

continue for signed void

default got size of volatile

do if static while

 SYMBOLIC CONSTANT: -

A symbolic constant is name given to some numeric constant, or character constant String constant
or any other constant Symbolic constant names are also known as constant identifiers. Pre-
processor directive # define is used for defining symbolic constants.

Syntax: -

# define symbolic constant name

Example: -

# define PI 3.141592

# define max 500

Here PI, MAX are symbolic constants.

Advantages of using symbolic Constant: -

 They can be used to assign names to values.


 Replacement of value has to be clone at one place and whenever the name appears in the
text it gets the value by execution of the preprocessor.
 This saves time if the symbolic constant appears 20 time in the program; it needs to be
changed at one place only.

 STRUCTURE OF C PROGRAM: -
A C program is divided into different section There are six main sections to basic C program.
The six sections are: -
1. Documentation section
2. Link section
3. Definition section
4. Global Declaration Section
5- Main function
6- Subprogram section

1. Documentation section: - The documentation section is the part of the program


where the programmer gives the details associated with the program, He usually
gives the name of the program.
Example: - /* A program to display Hello World */
2. Link section: - this part of the code is used to declared all the header that will be
used in the program. This leads to the compiler being told to link the header files to
the system libraries.
Example: - # include <stdio.h>
3. Definition Section: - In this section, we define different constants. The keyword
defines is used in this part
# define PI.3.14
4. Global declaration Section: - This part of code, is the part where the global variables
are declared. All the global variable used are declared in the part. The user defined
functions are also declared in this part of the program.
Example: - int a=10;
5. Main function section: - Every C program needs to have the main function. Each
main function contains two parts. A declaration part and an execution part. The
declaration part is the part where all the variables are declaration The execution
part begins with the curly brackets and ends with the curly bracket.
Example: -
Void main ()
{
Int a=10;
Print f (“%d”, a)
}
6. Sub Program Section: - All the user defined functions are defined in this section of
the program.
Example: - int add (int a, int b)
{
Return a+b;
}
 SAMPLE C PROGRAM: -
/* program to calculate area of circle */ Documentation
#include <stdio.h> // link section
#define PI 3.14 // definition section
float area (float r) // global declaration
void main () // main function
{
float r;
print f (" Enter the radius");
scan f ("% f", & r);
print f (“the area is % if, area (r));
return 0;
}
Float area (float r)
{
Return PT *r*r; // sub program
}
 PRINT f () FUNCTION: - C provide the print f is function to display the data on the screen
print f () function is defined in the stdio.h header file in C library.
1. Print f () function is inbuilt library functions in C programming language
2. We use print f () function with % d format specifier to display the value of an integer
variable.
3. Similarly, % c is used to display character, % f for float variable, % s for string variable.
4. Syntax print f ("Hello world");
 SCAN f () FUNCTION: - Data can be entered into the computer from input device like
keyboard use the scan f () function i.e. scan f () in used to read the input.
 Scan f () function is declared in stdio.h header, file in C library.
Syntax = Scan f (“format specifier", & variable name);
Scan f (“% d", & num) statement reads integer number from the console
and store the given value in number variable.
 OPERATORS: - An operator is a symbol that tells the compiler to perform specific
mathematical a logical function. C language is rich in built-in operators and provide following
types of operators: -
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. unary operator
5. Bitwise operator
6. Assignment operator
7. Shorthand assignment operator
8. Conditional operator
9. Increment and Decrement operator
1. Arithmetic operator: - Arithmetic operators are used to perform arithmetic operations such
as addition, subtraction multiplication, division etc. on numerical values.

Operators Name of Operators


+ Addition
- Subtraction
* Multiplication
/ Division
% Modula (Remainder)

Example: - a+b
Here a and b are operands and + is operator the data item on which operators act upon are
called operands.
2. Relational operator: - Relational operates are symbols that are used to compare the value of
operands. They are used to find the relationship b/w two operands and called relational
operators.
Operators Names
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to

 The operands bring out a decision result in form of logical value i.e. true or false
Example: - 5>13 false
6>=6 true
12==13 false
3. Logical operators: - Logical operators are used to Combine two or more relational
expressions. After evaluation, expression consisting of logical operators results is either true
or false such type of expression are called logical expression Logical operators are commonly
used in decision making

Operator Meaning Example


&& Logical and true only if all If c=5 and d=2 ((c==5) &&
operand is true d>5) equal to 0
|| Logical or true only if either If c=5 and d=2 ((c==5) ||
one operand is true d>5) equal to 1
! Logical not true only if the If c=5 then! ((c==5) equal to
operand is 0 0

4. Assignment operator: - An assignment operator assigns the value or result of the right-hand
side expression to the left-hand side value or variable. ''=” is known as assignment operator
which is uses to copy the result of an expression into a memory location identified by a
variable name.
Syntax: - variable name = expression;
Example: - a = 10;
B = 2+5;
5. Shorthand assignment operator: - C support a short variant of assignment operator called
shorthand assignment operator. shorthand assignment operator combines one of the
arithmetic operators with assignment operator.
Syntax: - variable operators=exp. a+= 2; a = a+2;
Example: - int a = 5
a = a+2;
a+=2;

Shorthand assignment Example Meaning


operator
+= a+=10 a=a+10
-= a-=10 a=a-10
*= a*=10 a=a*10
/= a/=10 a=a/10
%= a%=10 a=a%10
6. Increment and decrement operators: -
 Increment operator: - This is an unary operator and used in pre increment and post
increment for pre increment in which increment (++a) is placed before the operand (a++)
and post increment in which increment operator (++) is placed after the operands (a++).
 Increment operator increases the value by 1.
For example: - m=25
n=m++;
In this case value of n will be 25 and the value of m will be 26.
 Decrement operator: - The is an unary operator and this is also used in pre decrement and
post decrement form Pre decrement form in which decrement operator is places before the
operand (a--).
 Decrement operator decreases the value by 1.
For example: - m= 25
n= --m;
In the case the value of n and m would be 24.
m=25
n=m--
In this case value of n will be 25 and the value of m will be 24.
7. Conditional operator: - The conditional operator is also known as ternary operator which
consists of two symbols the question mark (?) and the colon (:)
Syntax: - exp 1? exp 2: exp 3;
The ternary operator work as follows: exp 1 is evaluated first. If the expression 1 is true then
exp 2 is evaluated & its value becomes the value of the expression. If the expression 1 is
false then expression 3 is evaluated and its value becomes the value of the expression.
Note: - Only one of the expressions is evaluated.
For example: - x=100;
y=150;
z=(x>y)? x: y
Here expression (x>y) gives out result false therefore y is assigned to z i.e. z is assigned by
value 150.
8. Bitwise operator: - A bitwise operate operates on each bit of data. C provide six operators
for bit manipulation.

Operator Name
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise X OR operator
~ Bitwise complement operator
<< Shift Left operator
>> Shift Right operator

 Bitwise AND operator: - The output of bitwise AND 1 if the corresponding bits of
two operands is 1. If either bit of an operand is O, the result of corresponding bit is
evaluated to 0.
A=0011 1100
B=0000 1101
ARB=0000 1100
 Bitwise OR operator: - The output of bitwise OR is 1 if at least one corresponding bit
of two operand is 1.
A=0011 1100
B=0000 1101
A|B=0011 1101
 Bitwise X OR operator: - The result of bitwise XOR operator is 1 if the corresponding
bits of two operands are opposite.
A=0011 1100
B=0000 1101
A^B=0011 0001
 Bitwise complement operator: - Bitwise complement operator is an unary operator.
It changes 1to0 and 0to1.
A=0011 1100
~A=1100 0011
 Shift Right operator: - Right shift operator, shifts all bits towards right by certain no.
of specified bits. It is denoted by ».
A=||0||0||
A>>2
A=00||0||0
 Shift Left operator: - shifts all bits towards left by certain no. of specified bits. It is
denoted by <<.
A=0|0| ||0|
A<<3
A=|||0|000
 Unary operators: - Unary operators are operators that act upon a single operand to
produce a new value.
Example: - Increment (++)
decrement (--)
Address of operator (&)
Size of ()
Not (!)
 ARITHMETIC EXPRESSIONS: -
An expression is combination of variable, constant and operators written according to the
syntax of C language i.e. an expression is a combination of arithmetic operators and
operand.
Arithmetic expressions are used to perform meaning computation.
Example: - 2/5÷2+5-6

Priority Operators
st
1 */%
2nd +-
rd
3 =
 Rules for evaluation of expression: -
1. First parenthesis expression from left to Right are evaluated.
2. If parenthesis is nested then innermost expression evaluated first.
3. Arithmetic expressions are evaluated from left to right using the rules of precedence.
i =2*3/4+4/4+8-2+5/8
i = 6/4+4/4+8-2+5/8
i = 1+4/4+8-2+5/8
i = 1+1+8-2+0
i = 1+1+8-2+0
i = 2+8-2+0
i = 10-2+0
i = 8+0
8

i = 3/2*4+3/8+3
i = 1*4+3/8+3
i = 4+3/8+3
i = 4+0+3
i = 4+3
i=7

 OPERATOR ASSOCIATIVITY: - The operator of the Same precedence are evaluated either
from left to Right or from right to left this is known as operator associativity.

 OPERATOR PRECEDENCE: - Each operator has a precedence associated Precedence is used


to determine how an expression involving more than one operator is evaluated. The
operator at the higher level of precedence are evaluated first.

1 / % → Left to right
= → Right to left
+ - → left to Right
 OPERATOR HIERARCHY: - The higher the position of an operator in the table, higher its
priority.

! Logical NOT
*/% Arithmetic operator
+- Arithmetic
<><=>= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment

 TYPE CASTING AND TYPE CONVERSION: - Type Casting is a way to convert a variable from
one data type to another data type.
Syntex: - (type name) expression
Example: - # include <stdio.h>
Void main ()
{
int Sum = 17, count = 5;
double mean;
mean = (double) sum /count;
printf ("Vales of mean: %f", mean);
}
Output: - Value of mean 3.400000

Type conversion

Implicit type conversion Explicit type conversion

IMPLICIT TYPE CONVERSION: - If a compiler Converts one data type into another type of data
automatically is called implicit type conversion.

There is no data loss (lower to higher data type)

float a= 20.2;

int b=a; || implicit conversion

EXPLICIT TYPE CONVERSION: - When data of one type is converted explicitly to another type with
the help of predefined functions. (done forcefully)

There is a data loss:

 Some conversions cannot be made implicit like int to short


 when we convent from higher data type to lower data type there is a chance of data
loss.

long double
double
float
long
int
short
char
UNIT 3rd NOTES
(Decision Making & Looping)

DECISION MAKING: - Decision making stricture requires that the programmer specifies one
or more conditions to be evaluated by the programs along with a statement to be executed
if condition is to be true and other statements to be executed of condition to be false.
Theres are 2 categories of control statement decision making statement

 BRANCHING STATEMENT: - A block of code wire be executed based on given condition.


Branching is decision which action to take from two or more choices
(a) If statement (b) If else statement (C) Nested If statement (d) Else If Ladder (e) switch
statement (f) goto statement
1. Simple If Statement: - This is the simplest form of the control statements. It is also
known as one-way branching statement It is used in decision making and control the
flow of execution of program.
The syntax of simple statement is:
if (condition)
{
Statements;
}
Here first of all condition is tested which results either a true or a false if the
result is true then the statement or block of statement gets execute.
Otherwise, C if result is false) these statements are skipped and the
statement that immediately follows it is executed.
Flow chart

Example: - #include <stdio.h>

void main ()

int age;

printf ("Enter age of a person");

scanf ("%d", &age);


if (age>=18)

printf (" He or she have right to vote ");

2. If else statement: - This is used when you have two choices, one group of statement
is executed when condition is true and another group is executed when it is false. It
is also known as two-way branching statement or two-way decision statement.
SYNTAX
if (Condition)
{
Statement 1;
}
else
{
Statement 2
First condition is tested; if condition is true statement 1 is executed and if condition
is false statement 2 is executed.
Flow chart

Example
#include <stdio.h>
void main ()
{
int age;
printf (" enter age of person");
scanf ("%d", & age);
if (age>=18)
{
printf (" He/she have right to vote");
}
else
{
printf (" He/she has no right to vote");
}
}

3. Nested If statement: - Some times there is more than one condition that should be
true before certain action is performed. In that case nested if used. When a If
statement is used within another if statement it is called Nested If Statement.
SYNTAX
if (condition-1)
{

if (condition-2)
{
Statement 1,
}
else
{
Statement 2;
}

Else
{
statement 3;
}

First of all, condition 1 is tested if it is true condition 2 is tested if condition 2 is true, statement 1 is
executed if condition 2 is false statement 2 is executed. if condition 1 is false statement 3 is
executed.

FLOW CHART

Example

#include <stdio.h>

void main ()

int var1, var2;

printf ("Enter the value of var1");

Scanf ("%d", & var1);

printf ("Enter the value of var2");

Scanf ("%d", & var2);

if (var1! = var 2)

printf ("var1 is not equal to var 2");


if (var1 > var2)

printf ("var1 is greater than var 2");

else

printf (“var2 is greater than var1");

else

printf (" var1 is equal to vor2");

4. Else If Ladder: - Sometimes we wish to make a multiway decision based on several


conditions. The most general way of doing this is by using the else if Ladder
statement. This works by cascading several comparisons. This is the most general
way of writing multi-way decision. due to this it is also known as multi-way decision
statement.
SYNTAX
if (Condition-1)
{
statement 1;
}
else if (condition-2)
{
statement 2;
}
else if (Condition 3)
{
statement 3;
}
……
else if (Condition n)
{
statement n;
}
else
{
default statement;
}
FLOW CHART
Example: - #include <stdio.h>
void main ()
{
int sub1, subz2, sub3;
float tot, per;
printf ("Enter marks obtained in three subjects");
Scanf ("%d%d%d", &sub1, &sub2, &sub3);
Tot = Sub1 + sub2 + sub3;
per =(tot/500) ×100
printf (" Total obtained marks is % f", tot);
printf (" Percentage is % f", per);
if (per >= 80)
printf ("Grade A");
else if (per>=70)
printf ("Grade B");
else if (per>=60)
printf (“Grade C");
else if (per>=50)
printf ("Grade D");
else if (per>=40)
printf ("Grade E");
else
printf ("fail");
}
5. Switch statement: - A switch statement allows a variable to be tested for equality
against ta list of values. Each value is called a case, and the variable being switched
on is checked for each switch case.
Nested if allows to select one of the many alternatives but it is time consuming. To
overcome this is another form of multiway decision statement is used known as
Switch statement.
SYNTAX
Switch (expression)
{
Case Label 1:
Statement1;
break;
case Label 2:
Statement 2;
break;
………………….
Case Label n:
Statement n;
break;
default
default statement;
break;
}
 The expression is an integer or character expression.
 Only one variable is tested, all branches must depend on the value of that variable.
 The break statement at the end of each block tells to the compiler about the end of
particular case and causes an exit from the switch statement block, transferring Control to
the next statement of the program.
 Default i's an optional case, it will be executed if the value of the expression does not match
with any of the case label value.
FLOW CHART

Example: - #include <stdio.h>

void main ()

int week;

printf ("Enter week number (1-7) ");

scanf ("%d", & week);

Switch (week)

case1: printf ("monday");

break;

case 2: printf ("Tuesday");

break;

case 3: printf (" Wednesday");

break;

case 4: printf("Thursday");

break;

case 5: printf ("Friday");


break;

case 6: printf ("Saturday");

break;

case 7: printf ("Sunday");

break;

default: printf ("Invalid output");

6. GOTO STATEMENT: - It is an unconditional control statement. It is used to transfer


the control from one location to another location without checking any condition.
Syntax
Goto label:
---------
---------
Label: Statement;
Here Label is a identifier associated with goto Statement shown in the above syntax
and can and can be used anywhere in the program either before or after goto label
statement.
when goto statement is executed, control transfers to the definition of the Label.
FLOW CHART

Example: - if (a>b)
{
goto a1
}
else
{
goto a2;
}
a1: printf (" Largest number is %d", a);
a2: printf ("Largest number is %d", b);

In C program you can have or can use any number of goto statements but no two
statements can have same Label.
 LOOPING STATEMENT: - These are those statements which can be used for repeating a
block of code in specified number of times of until the condition returns false. These are also
known as Iterative statements.
There are three types of wop statements:
1) while loop
2) do-while loop
3) for loop
1) while loop Statement: - A while loop in C programming repeatedly executes a target
statement as long as a given condition is true.
when the condition is false, the following body Statements is skipped and the control is
transferred out of the loop.
while loop is a entry controlled loop.
SYNTAX
initialization;
while (condition)
{
Statement;
}
while loop can be addressed as an entry control loop. It is completed in 3 steps:
1. Variable initialization (int x = 0)
2. Condition (x <= 10)
3. Variable increment/ decrement (x++ or x--)
FLOW CHART

Example: - #include <stdio.h>


void main ()
{
int x;
x=1;
while (x<=10)
{
printf ("%d\t", x);
x++;
}
}

Output = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

2) do-while loop: - In Some situation it is necessary to execute body of loop before testing the
condition. Such situation can be handled with the help of do-while wop. do statement
evaluates the body of loop first and at the end, the condition is checked using while
statement. It means the body of the loop will be executed at least once, even though the
starting condition inside while is initialized to be false.
SYNTAX
Initialization
Do
{
statement
Inc/dec
} while (condition);
Afta executing do statement the condition will be checked If it returns tree then only repeat
the Code otherwise control transfers to the next statement immediately after do-while loop.

FLOW CHART

Example: -
/* Program to print first to multiple of 5*/
#include <stdio.h>
void main ()
{
int a, i;
a=5;
do
{
Printf ("%d\t", a*i)
I++;
} while (i<= 10);
Output = 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
3) for loop: - The for-loop statement works well where the number of iterations of the loop is
known in advance. The is known in advance. format of the wop consists of three parts
Separated by semicolons.
In for loop we have exactly two semicolons, one after initialization and second after the
condition.
In this loop we Can have more then one initialization or inc/dec separated by comma
operator
SYNTAX
for (initialization; condition; increment/dec)
{
Statement;
}
FLOW CHART
Example: - /* program to print first 10 Natural No*/
#include <stdio.h>
void main ()

int x;
for (x=1; x <= 10; x++)
{
Printf ("%d\t", x);
}

Output = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 NESTED LOOP: - Nesting of loop is the feature in C that allow you to define looping of
Statement inside Any number of loops can be defined inside another loop i.e. there is no
restriction for defining any number of loops. for example, you can define while loop inside a
for loop.
SYNTAX
Outer loop

inner loop
{
inner loop statement;
}
Outer loop statement:

Example: -

#include <stdio.h>

void main ()

int n;

printf ("Enter the value of n:");


scanf ("%d", &n);

for (int i=1; i<=n; i++)

For (int j=1; j<= 10; j++)

Printf ("%d\t", (i*j));

print("\n");

Output = Enter the value of n:2

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

2, 4, 6, 8, 10, 12, 14, 16, 18, 20

 BREAK STATEMENT: - The Break statement is used to exit from the loop constructs.
The break statement is usually used with the Switch statement and it can be also use it
within the while loop, do-while loop, or the fir loop.
When break statement is encountered then the control is exited from the loop construct
immediate
SYNTAX Break;

FLOW CHART

Example: -

#include <stdio.h>

int main ()

int i;

for (i=0; i<=5, i++)

if (i ==3)
break;

printf ("%d", i);

return 0;

Output = 0, 1, 2

 CONTINUE STATEMENT: - When continue statement is encountered then the control


automatically passed from the beginning of the loop statement Continue statement can be
used within the while loop, do- while wop, for loop.
SYNTAX continue;

FLOW CHART

Example: -
#include <stdio.h>
int main ()
{
int i;
for (i=0; i<5; i++)
{
if (i ==3)
Continue;
Printf ("%d", i);
}
return 0;
}

Output = 0, 1, 2, 4

You might also like