0% found this document useful (0 votes)
42 views

PSD Unit-1

It's a c language course unit 1

Uploaded by

vasanthaxukai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

PSD Unit-1

It's a c language course unit 1

Uploaded by

vasanthaxukai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

Rajiv Gandhi University of Knowledge Technologies

Srikakulam Campus

23CS1181

Programming and Data


Structures
By
Mr. K. Dileep Kumar
M.Tech.,(Ph.D)
Asst. Professor,
Dept. of CSE,
RGUKT Srikakulam 1
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Course Content

2
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Course Content Contd..

3
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Course Outcome

4
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Assessment Method

5
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Unit-1
Introduction

6
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
The computer has mainly has two major
components:
 Hardware

 Software

7
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Computer Hardware

A computer hardware is the physical


components that make up a computer
system.

8
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Categories of Computer Hardware
Here are four main categories of computer
hardware:
 Input devices

 Output devices

 Storage devices

 Internal components

9
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Input Devices
Input devices allow the user to interact with the
computer.
Examples of input devices include:
 Keyboard

 Mouse

 Scanner

 Microphone

 Webcam

10
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Output Devices

Output devices display the information that the


computer has processed.
Examples of output devices include:
 Monitor

 Printer

 Speakers

11
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Storage Devices

Storage devices hold data for the


computer.
Examples of storage devices include:

 Hard disk drive (HDD)


 Solid-state drive (SSD)
 Optical drives (CD, DVD)

12
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Internal Components

Internal components are the parts that are


inside the computer case.
The main internal components include:
 Central processing unit (CPU)

 Motherboard

 Random access memory (RAM)

 Graphics processing unit (GPU) (optional)

13
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Conclusion

 Computer hardware is essential for a


computer to function.
 The different categories of computer
hardware work together to allow the
user to interact with the computer and
perform tasks.

14
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Bits and Bytes

15
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
History of Programming Languages
In the computer world, we have about 500+
programming languages with having their own syntax
and features.
• 1949: Assembly Language
• 1952: Autocode
• 1957: FORTRAN
• 1958: ALGOL
• 1959: COBOL
• 1964: BASIC
• 1972: C
• 1972: SQL
• 1978: MATLAB
• 1983: Objective-C, C++
• 1991: Python
• 1995: JAVA, PHP, JavaScript
• 2000: C#
• 2011: Kotlin
• 2014: Swift 16
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Computer Languages

• A computer language is a formal language used


to communicate with a computer.
• We need a special language to communicate
with computers, these languages are called
programming languages.
• Formal languages: Unlike natural languages
(English, French), programming languages have
strict rules (syntax) and a defined set of words
(keywords) the computer understands.

17
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
• There are two main categories:
• High-level languages (like Python, Java) are
closer to human languages, making them easier
to learn and use.

• Low-level languages (like machine code)


directly correspond to the computer's internal
instructions, but are harder for humans to
understand.

18
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Creating and Running Programs

Step 1 Creating Source Code


Press
F2 To Save

Step 2 Compile Source Code


Press
Alt + F9

Step 3 Run Executable Code


Press
Ctrl + F9

Step 4 Check Result


Press
Alt + F5

19
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Creating Source Code

What is source code?


- Source Code is the file which contains programming code in
high level language.

- To create source code, we need a text editor to write the


program.

- In C programming language every source code file must be


saved with “.c” extension.

20
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Creating Source Code

Steps to create source code?


- Open a text editor.

- Create New File.

- Type the program instructions.

- Save the file with “.c” extension.

21
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Compiling Source Code
What is Compilation?
- Compilation is the process of translating the code from high
level language to low level language.

- But before translation compiler check for the errors. If


there are any errors it does not translate, simply returns list
of errors.

- To compile a C source code in Turbo C, we use shortcut key


ALT + F9
Compile (or) Interpret Depending on the language
Compile - The code is translated into machine code the computer understands
directly.
Interpret - The code is translated line by line during execution.

22
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Executing/Running Executable Code

- After completing the compilation the compiler generates an


executable file with .exe extension.

- We need to execute this executable file to generate result

- To run a C executable code in Turbo C, we use shortcut key


CTRL + F9

23
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Checking Result

- After execution the result is placed in the window called


User Screen. We need to open that user screen to check
result

- To open user screen in Turbo C, we use shortcut key


ALT + F5

24
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Program Execution Process

Source Code Compiler Object Code Linker Executable Code User Screen

abc ALT+F9 add 110 CRTL+F9 ALT+F5

CPU

Sample.obj Sample.exe
Sample.c

stdio.h

List of ERRORS

Header Files

25
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Introduction to C Language

26
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
What is C Language?

C is a general-purpose computer programming


language.

C is also said to be structures programming language


or function oriented programming language.

C is a High level programming language.

27
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Why do we use C?

C language is used to create applications or


software.

Initially, C was developed to create an operating


system called UNIX.

The popular software like Linux OS, PHP & MySQL


are created using C language.

28
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Why do we use C?

Generally C Language is used to create the


following…
Operating Systems
Language Compilers
Assemblers
Interpreters
Text Editors
Network Drivers
Databases

29
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
History of C Language?
C Language was developed by Dennis Ritchie in the
year of 1972.
Born – September 9, 1941
Profession – Computer Scientist
Place – Bell Labs, US
Known As – Father of C & UNIX
Awards – Turing Award(1983)
National Medal of Technology(1998)
IEEE Medal(1990)
Computer Pioneer Award(1994)
Computer History Museum Fellow(1997) Dennis Ritchie
Harold Pender Award(2003)
Died – October 12, 2011

30
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
History of C Language?
Let’s see the programming languages that were developed before C
language…

CPL – Common Programming Language invented by


Martin Richards in 1960’s
BCPL – Basic Combined Programming Language by
Martin Richards in 1966 Ken Thompson
B Language – by Ken Thompson & Dennis Ritchie in
1969
Traditional C – by Dennis Ritchie in 1972
K&R C – by Kernighan & Dennis Ritchie in 1978
ANSI C – by ANSI Committee in 1989 Dennis Ritchie
ANSI/ISO C – by ISO Committee in 1990
C99 – by Standardization Committee in 1999

31
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Basics of C Programming?

C is a structured programming language. So every


instruction in a c program must follow the
predefined structure (Syntax).

C is also known as Function Oriented Programming


Language. So every executable statement must
written inside a function.

32
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

/* Documentation */

Pre-Processing Statements

Global Declarations

Main method

Used defined methods implementation

33
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

Documentation /* Documentation */
- It is used to provide brief information Pre-Processing Statements
of the program. Global Declarations

- This part is written using comments. Main method

- Generally the documentation part does Used defined methods


not executed by compiler & it is implementation
optional part.

34
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

Pre-Processing Statements
- It is used to link the header files, /* Documentation */
define the constants, etc... Pre-Processing Statements
Global Declarations

- Every preprocessor statement starts


with hash (#) symbol. Main method

- Every Preprocessor statement tells to Used defined methods


the compiler to perform required implementation

pre-processing before the actual


compilation.

35
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

Pre-Processing Statements
- Examples /* Documentation */
#include Pre-Processing Statements
#define Global Declarations
#undef
#ifdef
Main method
#ifndef
#if
#else Used defined methods
#elif implementation
#endif
#error
#pragma

36
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

Global Declaration
- This part is used to declare the /* Documentation */
variables which are common for Pre-Processing Statements
multiple methods. Global Declarations

- In this section, we also declare Main method


enumeration, structure, unions,
userdefined methods etc... Used defined methods
implementation

- It is also optional part. According to


our requirement we write this section.
6

37
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

Main method
- main method is the compulsory part for any c /* Documentation */
program. Pre-Processing Statements
- C language is a function oriented programming Global Declarations
language, so every c program must have at least
one function and that must be main. Main method
- Main is a userdefined method which specifies
the starting point of the program execution. Used defined methods
implementation
- Every c program execution starts with main
method and ends with main method itself.

38
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program

Userdefined Methods
- In this section of the program we write the /* Documentation */
actual code for the userdefined methods. Pre-Processing Statements
- Userdefined methods can be implemented Global Declarations
either before or after the method.
Main method
- If it is implemented after the main then it
must be declared either inside or before the
main method. Used defined methods
implementation
- If it is implemented before the main then the
declaration can be ignored.

39
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
General Structure of a C Program - Example

/* Program to print a message Hello World! */

#include<stdio.h>

void main()
{

printf(“Hello World!!!!”);

40
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Rules for Writing C programs

Every c program must contain exact one main


method
Every executable instruction must end with
semicolon (;)
All the system defined words (Keywords) must
be used in lowercase letters
Every open brace ({) must have the respective
closing brace (})
The variables must be declared in the
declaration section before they are used

41
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
42
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
What is C Character set?

C Character set is a collection of characters


supported in C programming language.

C Programming language has a rich set of characters


which are used to construct c program instructions.

43
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
What does C Character Set contains?

Alphabets
- C Language supports all alphabets of English. It supports
both UPPERCASE & lowercase letters

Digits
- C Language supports 10 digits to construct numbers. Those
10 digits are 0,1,2,3,4,5,6,7,8,9
Special Symbols
- C supports a rich set of special symbols that include
symbols to perform mathematical operations, condition
checking, white space, back space, etc…

44
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Commonly used Special Symbols with ASCII Values

45
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Note

- In C Programming Language, the character set follow ASCII


(American Standard Code for Information Interchange) code
text format.

- Every character in C language has the respective ASCII value


which is used to convert a character into Binary language.

46
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Keywords in C

47
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
48
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Variables

Variable is a name given to a memory location


where we can store different values of the
same datatype during the program execution.

49
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
 Every variable in c programming language
must be declared in the declaration section
before it is used.
 Every variable must have a datatype that
determines the range and type of values be
stored and the size of the memory to be
allocated.
 A variable name may contain letters, digits
and underscore symbol. The following are
the rules to specify a variable name...
50
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
 Variable name should not start with a digit.
 Keywords should not be used as variable
names.
 A variable name should not contain any
special symbols except underscore(_).
 A variable name can be of any length but
compiler considers only the first 31
characters of the variable name.

51
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Declaration Syntax:
datatype variableName;

Example: int number;

(The above declaration tells to the compiler that


allocates 2 bytes of memory with the name number and
allows only integer values into that memory location.)

In C programming language, the declaration can be performed either


before the function as global variables or inside any block or function. But
it must be at the beginning of block or function.
52
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Identifiers

 The identifier is a user-defined name of an


entity to identify it uniquely during the
program execution

Examples:
int marks;
char studentName[30];

Here, marks and studentName are identifiers.

53
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Rules for Creating Identifiers

 An identifier can contain letters (UPPERCASE


and lowercase), numerics & underscore symbol
only.
 An identifier should not start with a numerical
value. It can start with a letter or an
underscore.
 We should not use any special symbols in
between the identifier even whitespace.
However, the only underscore symbol is
allowed.

54
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
 Keywords should not be used as identifiers.
 There is no limit for the length of an
identifier.
 However, the compiler considers the first 31
characters only.
 An identifier must be unique in its scope.

55
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Data Types in C

The Data type is a set of value with predefined


characteristics. data types are used to declare
variable, constants, arrays, pointers, and
functions.

56
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
57
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
58
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
In the c programming language, data types are
classified as follows

1.Primary data types


(Basic data types OR Predefined data types)

2. Derived data types


(Secondary data types OR User-defined data types)

3. User Defined Data type

59
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Primary data types
 The primary data types in the C programming
language are the basic data types.
 All the primary data types are already defined in
the system.
 Primary data types are also called as Built-In data
types.
 The following are the primary data types in c
programming language.

1.Integer data type


2.Floating Point data type
3.Double data type
4.Character data type 60
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Integer Data type

61
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Floating Point data type

62
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Character data type

63
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Operators in C

An operator is a symbol used to perform


arithmetic and logical operations in a program.
That means an operator is a special symbol that
tells the compiler to perform mathematical or
logical operations.

64
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C programming language supports a rich set
of operators that are classified as follows.

1.Arithmetic Operators
2.Relational Operators
3.Logical Operators
4.Increment & Decrement Operators
5.Assignment Operators
6.Bitwise Operators
7.Conditional Operator
8.Special Operators
65
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Arithmetic Operators (+, -, *, /, %)

The arithmetic operators are the symbols that are used to perform basic
mathematical operations like addition, subtraction, multiplication, division
and percentage modulo.

66
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Relational Operators
(<, >, <=, >=, ==, !=)

the relational operators are used to check the relationship


between two values. Every relational operator has two results
TRUE or FALSE.

67
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Increment & Decrement Operators (++
& --)
• The increment and decrement operators are called unary operators
because both need only one operand.
• The increment operators adds one to the existing value of the operand
and the decrement operator subtracts one from the existing value of
the operand.

68
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Assignment Operators (=, +=, -=, *=,
/=, %=)

69
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Bitwise Operators (&, |, ^, ~, >>, <<)

70
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Conditional Operator (?:)

The conditional operator is also called a ternary operator


because it requires three operands.
This operator is used for decision making.
First we verify a condition, then we perform one operation out
of the two operations based on the condition result.

Condition ? TRUE Part : FALSE Part;


Example
A = (10<15)?100:200; A value is 100

71
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
The following are the special operators in c
programming language.

72
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Expressions
What is an expression?
 An expression is a collection of operators and operands that represents a
specific value.

Expression Types in C
In the C programming language, expressions are divided into THREE
types. They are as follows...
1.Infix Expression
2.Postfix Expression
3.Prefix Expression

73
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Constants
In C programming language, a constant is
similar to the variable but the constant hold
only one value during the program execution.
A constant is a named memory location which
holds only one value throughout the program
execution.
Creating constants in C
In a c programming language, constants can be
created using two concepts...
Using the 'const' keyword
Using '#define' preprocessor

74
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Using the 'const' keyword:
const datatype constantName ;
(OR)
const datatype constantName = value ;
Example
const int x = 10 ;
Here, 'x' is a integer constant with fixed
value 10.

75
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Using '#define' preprocessor:
#define CONSTANTNAME value
Example
#define PI 3.14
Here, PI is a constant with value 3.14

76
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Input Functions
C programming language provides built-in
functions to perform input operations.
The input operations are used to read user
values (input) from the keyboard.
The c programming language provides the
following built-in input functions.

scanf()
getchar()
getch()
gets()
fscanf()
77
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
scanf() function:
 The scanf() function is used to read multiple
data values of different data types from the
keyboard.
 The scanf() function is built-in function
defined in a header file called "stdio.h".
 When we want to use scanf() function in our
program, we need to include the respective
header file (stdio.h) using #include
statement.
78
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
79
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
80
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
81
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
82
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
C Output Functions

 C programming language provides built-in


functions to perform output operation. The
output operations are used to display data
on user screen (output screen) or printer or
any file.
 The c programming language provides the
following built-in output functions.
printf()
putchar()
puts()
fprintf()
83
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
printf() function
 The printf() function is used to print string
or data values or a combination of string and
data values on the output screen (User
screen).
 The printf() function is built-in function
defined in a header file called "stdio.h".
 When we want to use printf() function in our
program we need to include the respective
header file (stdio.h) using the #include
statement.
84
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
85
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
The printf() function is also used to display data values.
When we want to display data values we use format string of the
data value to be displayed.

86
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Conditional Statements
 Decision-making statements are the
statements that are used to verify a given
condition and decide whether a block of
statements gets executed or not based on
the condition result.

 In the c programming language, there are


two decision-making statements they are as
follows.
1. if statement
2. switch statement
87
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
if statement in c:
 The if statement verifies the given condition
and decides whether a block of statements
are executed or not based on the condition
result.
 In c, if statement is classified into four types
as follows.
1. Simple if statement
2. if-else statement
3. if-else-if statement (if-else ladder)
4. Nested if statement
88
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Simple if statement:

89
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
if-else statement in C:
 The if-else statement is used to verify the
given condition and executes only one out of
the two blocks of statements based on the
condition result.
 The if-else statement evaluates the
specified condition.
 If it is TRUE, it executes a block of
statements (True block).
 If the condition is FALSE, it executes another
block of statements (False block).
90
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
If-else statement:

91
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
if-else-if statement (if-else ladder):
Writing a if statement inside else of an if
statement is called if-else-if statement.

92
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Nested if statement:
Writing a if statement inside another if
statement is called nested if statement.

93
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
94
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
'switch' statement in C

95
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
96
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Repetition: Loops

 The looping statements are used to execute


a single statement or block of statements
repeatedly until the given condition is
FALSE.

 C language provides three looping


statements.
1. while statement
2. do-while statement
3. for statement
97
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
'while' statement in C

The while statement is used to execute a single


statement or block of statements repeatedly as
long as the given condition is TRUE.
The while statement is also known as Entry
control looping statement.

98
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Flowchart

99
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
while is a keyword so it must be used only in
lower case letters.
If the condition contains a variable, it must be
assigned a value before it is used.
The value of the variable used in condition
must be modified according to the requirement
inside the while block.
In a while statement, the condition may be a
direct integer value, a variable or a condition.
A while statement can be an empty statement.

100
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
101
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
'do-while' statement in C
The do-while statement is used to execute a
single statement or block of statements
repeatedly as long as given the condition is
TRUE.
The do-while statement is also known as the
Exit control looping statement.

102
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
103
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
Both do and while are keywords so they must be used
only in lower case letters.
If the condition contains a variable, it must be
assigned a value before it is used.
The value of the variable used in the condition must
be modified according to the requirement inside the
do block.
In a do-while statement, the condition may be a
direct integer value, a variable or a condition.
A do-while statement can be an empty statement.
In do-while, the block of statements is executed at
least once.
104
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
105
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
‘for’ Statement in C

The for statement is used to execute a single


statement or a block of statements repeatedly
as long as the given condition is TRUE.

106
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
 At first, the for statement executes initialization
followed by condition evaluation.
 If the condition is evaluated to TRUE, the single
statement or block of statements of for statement are
executed.
 Once the execution gets completed, the modification
statement is executed and again the condition is
evaluated.
 If it is TRUE, again the same statements are executed.
 The same process is repeated until the condition is
evaluated to FALSE.
 Whenever the condition is evaluated to FALSE, the
execution control moves out of the for block.
107
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
108
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
109
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM
 for is a keyword so it must be used only in lower
case letters.
 Every for statement must be provided with
initialization, condition, and modification (They
can be empty but must be separated with ";")
Ex: for ( ; ; ) or for ( ; condition ; modification ) or for ( ; condition ; )
 In for statement, the condition may be a direct
integer value, a variable or a condition.
 The for statement can be an empty statement.

110
Dileep Kumar Koda | Asst. Prof. | CSE | RGUKT-SKLM

You might also like