Download - C Intro Class1
-
8/2/2019 C Intro Class1
1/108
KGISL ITECH 1
KGiSL- iTech
-
8/2/2019 C Intro Class1
2/108
KGISL ITECH 2
a language designed for programming computers
is an artificial language designed to expresscomputations that can be performed by a machine,particularly a computer.
a series of instructions written by a programmeraccording to a given set of rules or conventions(syntax ).
is a notation for writing programs. All PL language can be divided into three categories
Problem Oriented or High Level Languages
Machine Oriented or Low Level Languages
Middle Level Language2
-
8/2/2019 C Intro Class1
3/108
KGISL ITECH 3
Machine languageNatural language of a particular computer
Consists of strings of numbers(1s, 0s)
Instruct computer to perform elementary operationsone at a time
Machine dependant
3
-
8/2/2019 C Intro Class1
4/108
KGISL ITECH 4
Assembly Language
English like abbreviations
Translators programs called Assemblers to
convert assembly language programs to machinelanguage.
E.g. add overtime to base pay and store result ingross pay
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY 4
-
8/2/2019 C Intro Class1
5/108
KGISL ITECH 5
High-level languages
To speed up programming even further Singlestatements for accomplishing substantial tasks
Translator programs called Compilers toconvert high-level programs into machinelanguage
E.g. add overtime to base pay and store result ingross pay
grossPay = basePay + overtimePay
5
-
8/2/2019 C Intro Class1
6/108
KGISL ITECH 6
Procedural Programming Languages
every program comes with a starting phase, a list of tasksand operations, and an ending stage. made up ofprocedures, subroutines, or methods.Fortran, Basic
Structured Programming Languages
requires that programmers break program structure into smallpieces of code.
associated with a "top-down"approach to design.C, Pascal
Object-Oriented Programming Languages requires the designer to specify the data structures as well as the
types of operations to be applied on those data structures. a program thus becomes a collection of cooperating objects,
rather than a list of instructions.C++, Java, Python
6
-
8/2/2019 C Intro Class1
7/108KGISL ITECH 7
s A language written by Brian Kernighan and Dennis
Ritchie atBell Laboratories.
s C is a general-purpose computer programming languagedeveloped in 1972.
s A Programming language (PL) C is a set of instructionsused to communicate with the computer
s The set of instruction written in a PL is known asprogram
7
-
8/2/2019 C Intro Class1
8/108KGISL ITECH 8
Evolved from two previous languages BCPL , B
BCPL (Basic Combined Programming
Language) used for writing OS & compilers
B used for creating early versions ofUNIXOS
Both were typeless languages
8
-
8/2/2019 C Intro Class1
9/108KGISL ITECH 9
C provides:
xEfficiency, high performance and high quality s/w
x flexibility and power
xmany high-level and low-level operations middle level
xStability and small size code
xProvide functionality through rich set of functionlibraries
xGateway for other professional languages likeC C++ Java
9
-
8/2/2019 C Intro Class1
10/108KGISL ITECH 10
C is used:
xSystem software Compilers, Editors, embeddedsystems
x
data compression, graphics and computationalgeometry, utility programs
xdatabases, operating systems, device drivers,system level routines
xAlso used in application programs
10
-
8/2/2019 C Intro Class1
11/108KGISL ITECH 11
C systems consist of 3 parts Environment Language C Standard Library
Development environment has 6 phases Edit Pre-processor
Compile Link Load Execute
11
-
8/2/2019 C Intro Class1
12/108KGISL ITECH 12
Editor DiskPhase 1Program edited inEditor and stored
on disk
Preprocessor DiskPhase 2
Preprocessor
program processesthe code
CompilerDisk
Phase 3
Creates object code
and stores on disk
Linker DiskPhase 4
Links object code
with libraries and
stores on disk
12
-
8/2/2019 C Intro Class1
13/108KGISL ITECH 13
LoaderPhase 5
Puts program in
memory
Primary memory
CPUPhase 6
Takes each instruction
and executes it storingnew data values
Primary memory
13
-
8/2/2019 C Intro Class1
14/108
KGISL ITECH 14
Four stages
Editing:Writing the source code by using some IDE or editor
Preprocessing or libraries: Already available routines
compiling: translates or converts source to object code for aspecific platform source code -> object code
linking: resolves external references and produces the
executable module
14
-
8/2/2019 C Intro Class1
15/108
KGISL ITECH 15
Include files
Function subprogram
Main function
15
Global variable and
function declaration
-
8/2/2019 C Intro Class1
16/108
KGISL ITECH 16
/* A first C Program*/
#include
voidmain()
{
printf(Learning C Programming is
easy \n");}
16
-
8/2/2019 C Intro Class1
17/108
KGISL ITECH 17
Line 1: #include
As part of compilation, the C compiler runs a programcalled the C preprocessor. The preprocessor is able toadd and remove code from your source file.
In this case, the directive #includetells the preprocessorto include code from the filestdio.h.
This file contains declarations for functions that theprogram needs to use. A declaration for theprintf()
function is in this file.
17
-
8/2/2019 C Intro Class1
18/108
KGISL ITECH 18
Line 2: void main() This statement declares the main function.
A C program can contain many functions but must alwayshave one main function.
A function is aself-contained module of code that canaccomplish some task.
Functions are examined later.
The "void"specifies the returntypeof main. In this case,nothing is returned to the operating system.
18
-
8/2/2019 C Intro Class1
19/108
KGISL ITECH 19
Line 3: {
This opening bracket denotes thestartof theprogram.
19
-
8/2/2019 C Intro Class1
20/108
KGISL ITECH 20
Line 4: printf(Learning C Programming is easy\n");
printf() is a function from a standard C library that isused to print strings to the standard output, normally yourscreen.
The compiler links code from thesestandard libraries tothe code you have written to produce the final executable.
The "\n"is a special format modifierthat tells theprintfto put a line feed at the end of the line.
If there were another printf in this program, its stringwould print on the next line.
20
-
8/2/2019 C Intro Class1
21/108
KGISL ITECH 21
Line 5: }
This closing bracket denotes the endof theprogram.
Note
s C is a Case Sensitive
s All C statements must be terminated by a semi-colon ( ; )
21
-
8/2/2019 C Intro Class1
22/108
KGISL ITECH 22
Comment should be enclosed between /* */
Single Line Comment is //
It is used to increase the readability of the
program.
Any number of comments can be given at anyplace in the program.
Comment cannot be nested.
It can be split over more than one line
22
-
8/2/2019 C Intro Class1
23/108
KGISL ITECH 23
\n new line \t tab \r carriage return
\a alert \\ backslash \ double quote \? Question Mark \ Quotation Mark \0 Null
23
-
8/2/2019 C Intro Class1
24/108
KGISL ITECH 24
Alphabets
DigitsSpecial-symbols
Constants
VariablesKeywords
Instruction Program
24
-
8/2/2019 C Intro Class1
25/108
KGISL ITECH 25
A character denotes any alphabet, digit orspecial symbol used to representinformation.
25
-
8/2/2019 C Intro Class1
26/108
KGISL ITECH 26
The alphabets, numbers and specialsymbolwhen properly combined form constants,variables and keywords
A constant is a quantity that doesntchange
A variable is a name given to the location inmemory where the constant is stored
26
-
8/2/2019 C Intro Class1
27/108
KGISL ITECH 27
C constants can be divided into two majorcategories Primary Constants
Secondary Constants
C Constants
Primary Constants Secondary constants
Integer Constant
Real Constant
Character Constant
Array, PointerStructure, Union
Enum
27
-
8/2/2019 C Intro Class1
28/108
KGISL ITECH 28
An integer constant must have at least onedigit
It must not have a decimal point
It could be either positive or negative If no sign precedes an integer constant, it isassumed to be positive ex., 123, -321, +78
No commas or blanks are allowed within aninteger constant
-
8/2/2019 C Intro Class1
29/108
KGISL ITECH 29
Real constants (RC) must have at leastone digit
It must have a decimal point
It could be eitherpositive or negative
Defaultsign ispositive
No commas or blankare allowed within a RC
Ex., 0.0083, -0.75, +247.0
29
-
8/2/2019 C Intro Class1
30/108
KGISL ITECH 30
A Single character constant is either a singlealphabet, a single digit or a single specialsymbol enclosed within single quotes
The maximum length of a character constantcan be 1 character
Eg a, 1, 5, = (Valid)asd, 12 (Invalid)
30
-
8/2/2019 C Intro Class1
31/108
KGISL ITECH 31
It is a sequence of characters enclosed indouble quotes
Characters may be letters, numbers, specialcharacters enclosed in double quotes.
Eg., Hello! 1987 ?...! X
31
-
8/2/2019 C Intro Class1
32/108
KGISL ITECH 32
Identifiers are names given to variousprogram elements, such as variables,functions and arrays
A variable name is any combination ofalphabets, digits or underscores
The first character in the variable name mustbe an alphabet
32
-
8/2/2019 C Intro Class1
33/108
KGISL ITECH 33
Keywords are the reservedwords
They cannotbe used as variable names.
There are only 32 keywords available in c
auto double if static do
break else int struct goto
case enum long switch signed
char extern near typedef while
const float register union defaultcontinue far return unsigned for
short void
33
-
8/2/2019 C Intro Class1
34/108
KGISL ITECH 34
There are basically four types of instructionsin C
Type declaration instruction
Eg: int sum;float ave;char name,code;
Input/Output instruction Eg: scanf(),
printf()
34
-
8/2/2019 C Intro Class1
35/108
KGISL ITECH 35
Arithmetic instructionEg :
x = sum +2;
Control instruction
Eg :
while do statement
for statementif statement
35
-
8/2/2019 C Intro Class1
36/108
KGISL ITECH 36
C Supports several different types of data, each of which
may be represented differentlywithin the computersmemory.
Basic data types are listed below:
Data Type Description Typical Memory
int integer quantity 2 bytes
char single character 1 bytes
float floating point number 4 bytes
double double-precisionfloating point number 8 bytes
36
-
8/2/2019 C Intro Class1
37/108
KGISL ITECH 37
A variable is a data name that used to store a data value
A variable may take different values at different times duringexecution
A variable name should be meaningful
Ex: Average
Total
Height
1.They must begin with a letter. Some system allows _ as thefirst character.
2.ANSI standard recognizes a length of 31 characters.
3.Length should not be normally more than 8 characters.4.Upper case and lower case are significant.
5.It should not be a keyword.
6.White space is not allowed.
37
-
8/2/2019 C Intro Class1
38/108
KGISL ITECH 38
ValidJohn Value X1 ph_value
mark
Invalid 123
% 25th (area) group 1
38
-
8/2/2019 C Intro Class1
39/108
KGISL ITECH 39
Declaration does two things It tells the compilers what the variable name is It specifies what type of data it holds
Declaration of must be done before they areusedin the program
Syntax : ;
Eg:int count;int num,total;double ratio;
39
-
8/2/2019 C Intro Class1
40/108
KGISL ITECH 40
auto local variable declared within a function
static local variable which exist and retain value evenafter control is transferred to calling function
extern Global variable known to all function
register Local Variable which is stored in the register
static and external variables are automatically initializedto zero
Automatic (auto) variables contains undefined valuescalled garbage unless they are initialized.
40
-
8/2/2019 C Intro Class1
41/108
KGISL ITECH 41
Initialization Syntax: =
Eg:int num = 5;
Values can be assigned using theassignment operator = Eg:int num;
num = 5;
41
-
8/2/2019 C Intro Class1
42/108
KGISL ITECH 42
An operator is a symbol(+,-,*,/) thatdirects the computer to perform certainmathematical or logical manipulations and
is usually used to manipulate data andvariables.
Ex: a + b
42
-
8/2/2019 C Intro Class1
43/108
KGISL ITECH 43
1. Arithmetic operators2. Relational operators
3. Logical operators
4. Assignment operators5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
43
-
8/2/2019 C Intro Class1
44/108
KGISL ITECH 44
C operation Algebraic C
Addition(+) f+7 f+7
Subtraction (-) p-c p-c
Multiplication(*) bm b*m
Division(/) x/y, x , x y x/y
Modulus(%) r mod s r%s
44
-
8/2/2019 C Intro Class1
45/108
Operators in expressions contained within pairs ofparentheses are evaluated first. Parentheses
are said to be at the "highest level ofprecedence."
((a + b) + c) Multiplication, division and modulus operations are
applied next. If an expression contains several
multiplication, division and modulus operations,operators are applied from left to right.Multiplication, division and modulus are said to beon the same level of precedence.
Addition and subtraction operations are appliedlast. If an expression contains several addition andsubtraction operations, operators are applied fromleft to right. Addition and subtraction also have thesame level of precedence.
a * b + ( c d) / f45KGISL ITECH
-
8/2/2019 C Intro Class1
46/108
KGISL ITECH 46
Highest to lowest
()
*, /, %
+, -
46
-
8/2/2019 C Intro Class1
47/108
KGISL ITECH 47
Algebra:a(b+c)+ c(d+e)
C:
a * ( b + c ) + c * ( d + e ) ;
Precedence:
3 1 5 4 2
47
-
8/2/2019 C Intro Class1
48/108
KGISL ITECH 48
/* Program using Arithmetic operator */
#include
#include
void main()
{
int a=5,b=6; // Initializing variablesint c;
clrscr(); // To clear the screen
c=a+b; // + operator
printf(The result is : %d,c);
c=b%a; // % operator
printf(The result is : %d,c);
}
48
-
8/2/2019 C Intro Class1
49/108
A Relational Operator compares twovalues.
Values can be any built-in C data
type.The comparison involves suchrelationship as equal to, less than
and greater than.The result of the comparison is eithertrue or false.
49KGISL ITECH
-
8/2/2019 C Intro Class1
50/108
KGISL ITECH 50
Operator Meaning
< Is less than
Is greater than
>= Is greater than or equal to
== Equal to
!= Not equal to
50
-
8/2/2019 C Intro Class1
51/108
KGISL ITECH 51
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Logical expressions are a compound relational expressions
-An expression that combines two or morerelational expressions
Ex: if (a==b && b==c)
51
-
8/2/2019 C Intro Class1
52/108
The AND operator ( && ) requiresboth relationships to be true in orderfor the overall result to be true.
If either or both of the relationshipsare false, the entire result is false.
52KGISL ITECH
-
8/2/2019 C Intro Class1
53/108
Value of the expression
a b a && b
0 0 0
0 1 0
1 0 0
1 1 1
53KGISL ITECH
-
8/2/2019 C Intro Class1
54/108
The OR Operator ( || ) takes two logicalexpressions and combines them. Ifeither or bothare true, the result is true. Both values must befalse for the result to be false.
a b a|| b
0 0 0
0 1 1
1 0 1
1 1 154KGISL ITECH
-
8/2/2019 C Intro Class1
55/108
The ( && ) and ( II ) operators alwaysappears between two expressions. Alsoknown as binary operators.
The NOT operator ( ! ) is a unary operator. It precedes a single logical expression and
gives its opposite as a result. Example
if !( marks > 50)is same as
if (marks
-
8/2/2019 C Intro Class1
56/108
KGISL ITECH 56
Syntax:
v op = exp;
Where v = variable,
op = shorthand assignment operator
exp = expression
Ex:
x=x+3x+=3
56
-
8/2/2019 C Intro Class1
57/108
KGISL ITECH 57
Simple assignment operator Shorthand operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
57
-
8/2/2019 C Intro Class1
58/108
KGISL ITECH 58
1. Increment ++
2. Decrement --
The ++ operator adds a value 1 to the operand
The operatorsubtracts 1 from the operand
Eg:
++a or a++
--a or a--
58
-
8/2/2019 C Intro Class1
59/108
KGISL ITECH 59
Let the value of a =5 and b=++a then
a = b =6
Let the value of a = 5 and b=a++ then
a =5 but b=6
i.e.:
1. a prefix operator first adds 1 to the operand and then the
result is assignedto the variable on the left
2. a postfix operator first assigns the value to the variableon left and thenincrements the operand.
59
-
8/2/2019 C Intro Class1
60/108
When variable is not in an expression then Pre-incrementing and post-incrementing have thesame effectExampleint c = 5;
++c;printf(%d,c); // c = 6
andc++;
printf(%d,c); // c= 6
are the same
60KGISL ITECH
-
8/2/2019 C Intro Class1
61/108
intc = 5;
printf(%d,++c);
c is changed to 6
Then prints out 6
printf(%d,c++);
Prints out 5 (printf is executed before theincrement)
c then becomes 6
61KGISL ITECH
-
8/2/2019 C Intro Class1
62/108
KGISL ITECH 62
/* Program using Increment operator */
#include
#include
void main()
{
int a=5,b; // Initializing variablesclrscr(); // To clear the screen
b = a++; // Post Increment operator
printf(The result is : %d,b);
b = ++a; // Pre Increment operator
printf(The res ult is : %d,b);
}
62
-
8/2/2019 C Intro Class1
63/108
KGISL ITECH 63
main()
{ int c;c = 5;
printf(%d\n, c);printf(%d\n, c++);
printf(%d\n\n, c);c = 5;
printf(%d\n, c);printf(%d\n, ++c);printf(%d\n, c);
return 0;}
5
5
6
5
6
6
63
-
8/2/2019 C Intro Class1
64/108
KGISL ITECH 64
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions
Working of the ? : Operator:Exp1 is evaluated first, if it is nonzero(1/true) then theexpression2 is evaluated and this becomes the value of theexpression,
If exp1 is false(0/zero) exp3 is evaluated and its value
becomes the value of the expressionEx: m=2;
n=3;
r=(m>n) ? m : n;
64
-
8/2/2019 C Intro Class1
65/108
KGISL ITECH 65
/* Program using Conditional operator */
#include
#include
void main()
{int a,b,c; // Initializing variables
a=5; // Assigning values
b=6;
clrscr(); // To clear the screenc=(a>b)?a:b; // Conditional operator
printf(The result is : %d,c);
}
65
-
8/2/2019 C Intro Class1
66/108
The conditional operators can be nestedas shown below
void main()
{int k, num = 30;k=(num>5 ? (num
-
8/2/2019 C Intro Class1
67/108
KGISL ITECH 67
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
> Shift right
67
-
8/2/2019 C Intro Class1
68/108
void main(){
clrscr();int a=5,b=6; // a = 0101 , b = 0110int c;c=a&b;printf(a&&b:%d,c); // c = 0100 = 4c=a|b;
printf(a||b:%d,c); // c = 0111 = 7c=a^b;printf(a^b:%d,c); // c = 0011 = 3
int d;d=c1:%d,c); // d = 0001 = 1
}
68KGISL ITECH
-
8/2/2019 C Intro Class1
69/108
KGISL ITECH 69
1. Comma operator ( ,)
2. sizeof operator sizeof( )
3. Pointer operators ( & and *)
4. Member selection operators ( . and ->)
69
-
8/2/2019 C Intro Class1
70/108
KGISL ITECH 70
One of the essential operations performed ina C language programs is to provide inputvalues to the program and output the dataproduced by the program to a standard
output device. scanf, which can be used to read data from
a keyboard. printf, which sends results out to a terminal.
70
-
8/2/2019 C Intro Class1
71/108
KGISL ITECH 71
Each program that uses standard input / output function must contain the statement.
#include at the beginning.
Single character input output: getchar()
putchar()
String input and output: gets
puts
71
-
8/2/2019 C Intro Class1
72/108
KGISL ITECH 72
#include#include
int main()
{
clrscr();
printf (Welcome to KGISL);
return 0;
}
-
8/2/2019 C Intro Class1
73/108
KGISL ITECH 73
#include#includeint main(){
int num;printf(Enter the value of num:);scanf(%d,&num);printf(The value of num is:%d,num);
getch();}
-
8/2/2019 C Intro Class1
74/108
KGISL ITECH 74
#include#include
void main()
{
char c;
c=getchar();
putchar( c );
getch();}
-
8/2/2019 C Intro Class1
75/108
KGISL ITECH 75
#include#include
void main()
{
char name[10];puts(Enter your name);
gets(name);
puts(Your name is:);
puts(name);getch();
}
-
8/2/2019 C Intro Class1
76/108
KGISL ITECH 76
%c- Reads a single character
%f- Reads a floating point value
%s- Reads a string
%h- Reads a hexadecimal integer
%o- Reads an octal integer
%i- Reads an integer value same as %d
76
-
8/2/2019 C Intro Class1
77/108
KGISL ITECH 77
-
8/2/2019 C Intro Class1
78/108
KGISL ITECH 78
C program is a set of statements, which arenormally sequentially executed in the orderin which they appear.
Conditional statements
Looping statements.
Unconditional statements
-
8/2/2019 C Intro Class1
79/108
KGISL ITECH 79
One of the most important parts of anylanguage is the ability to choose toexecute statements based on the value ofa particular variable.
To execute a single statement or group ofstatement.
-
8/2/2019 C Intro Class1
80/108
KGISL ITECH 80
There are four types of conditionalstatements.
Simple if statement
Ifelse statement
Nested if statement
Switch case
-
8/2/2019 C Intro Class1
81/108
KGISL ITECH 81
If Statement
ifstatement is a basic control flow structure ofC programming language.
ifstatement is used when a unit of code needto be executed by a condition true or false.
If the condition is true, the code in ifblock willexecute otherwise it does nothing.
Only performs an action if the condition istrue.
-
8/2/2019 C Intro Class1
82/108
KGISL ITECH 82
Syntax
if (Condition){
statements;
}
If the condition, whatever it is, is true, thenthe statement is executed.
/* D t ti f if t t t */
-
8/2/2019 C Intro Class1
83/108
KGISL ITECH 83
/* Demonstration of if statement */
void main( )
{int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num
-
8/2/2019 C Intro Class1
84/108
KGISL ITECH 84
Ifelse StatementTo check more than one condition.
Specifies an action to be performed both whenthe condition is true and when it is false
Syntax:
if (condition)
{true block of statements;
}
else
{
false block of statements;
}
#include
-
8/2/2019 C Intro Class1
85/108
KGISL ITECH 85
void main()
{
int a,b;
clrscr();printf(enter values of a and b:\n);
scanf(%d%d,&a,&b);
if(a>b)
{
printf(a is greater:%d,a);
}
else
{
printf(b is greater:%d,b);
}
getch();
}
else-ifladderF h ki th diti
-
8/2/2019 C Intro Class1
86/108
KGISL ITECH 86
For checking more than one conditionsSyntax:
if(condition-1){
/* code block if condition-1 is true */}else if (condition-2){
/* code block if condition-2 is true */}else if (condition-3){
/* code block if condition-3 is true */}
else{/* code block all conditions above are false */
}
#include else
-
8/2/2019 C Intro Class1
87/108
KGISL ITECH 87
#include
void main()
{
int a,b,c;clrscr();
printf(enter values ofa,b,c:);
scanf(%d%d%d,&a,&b,&c);
if(a>b)&&(a>c)
{
printf(a is greater);
}else if(b>a)&&(b>c)
{
printf(b is greater);
}
else
{
printf(c is greater);
}getch();
}
S t
Nested ift t t
-
8/2/2019 C Intro Class1
88/108
KGISL ITECH 88
Syntax:if(condition){
if(condition){if statement;
}else{
else stmt;}
}else if{
else if stmt;}
else{
else stmt;}
statement
void main()
{else if (b > c)
-
8/2/2019 C Intro Class1
89/108
KGISL ITECH 89
{
int a,b,c;
clrscr();
printf("Enter values ofa,b,c:");
scanf("%d%d%d",&a,&b,&c);
if (a > b)
{
if (a > c){
printf("A is greater");
}
else
{printf("C is greater");
}
}
{
printf("b is greater");
}
else
{
printf("c is greater");
}
getch();}
-
8/2/2019 C Intro Class1
90/108
Loops Cause a section of your program tobe repeated a certain number of times.
The repetitioncontinues while a conditionis true. When condition becomes false, the
loops ends and control passes to thestatements following the loop.
Types of Loops
for loop while loop
do while loop
90KGISL ITECH
-
8/2/2019 C Intro Class1
91/108
The for allows us to specify three things about aloop in a single line.
a)Setting a loop counter to an initial value.
b)Testing the loop counter to determine whether itsvalue has reached the number of repetitionsdesired.
c)Increasing the value of loop counter each time
the program segment within the loop has beenexecuted.
91KGISL ITECH
-
8/2/2019 C Intro Class1
92/108
The general form offor statement is as under.
for (initialization expression; test expression; incrementexpression)
{statement 1;
statement 2;
.
.statement n;
}
92KGISL ITECH
-
8/2/2019 C Intro Class1
93/108
void main ( ){
int j;
for ( j=0; j < 15; j++ )
{
printf(%d\n,j);
}
getch( );}
93KGISL ITECH
-
8/2/2019 C Intro Class1
94/108
Loops can also be nested. The placing of one loop inside the body of
another loop is called nesting.
Syntax:
for( intialization; test _condition; Increment;)
{
for( intialization; test_condition;increment;)
{
statemet;
}
statement;
}
94KGISL ITECH
-
8/2/2019 C Intro Class1
95/108
#includevoid main ( ){int i, j;clrscr ( );for (j=1; j
-
8/2/2019 C Intro Class1
96/108
Loops can also be nested like if statements.void main ( ){
int r, c, sum;for ( r = 1; r
-
8/2/2019 C Intro Class1
97/108
The while loop allows programs to repeat astatement or series of statements, over andover, as long as a certain test condition istrue.
The while loop is an entry-condition loop
Syntax:while (test-condition)
{
statement
}
97KGISL ITECH
-
8/2/2019 C Intro Class1
98/108
#includevoid main(){int i=0;clrscr();
while(i
-
8/2/2019 C Intro Class1
99/108
The do-while loop is similar to the while loop,except that the test condition occurs at the endof the loop.
The do-while loop is an exit-condition loop.
Syntax
do
{
statement
} while (condition);
99KGISL ITECH
-
8/2/2019 C Intro Class1
100/108
#includevoid main()
{
int i=0;
do{
i++;
printf(%d,i);
}while(i
-
8/2/2019 C Intro Class1
101/108
KGISL ITECH
Unconditional statement
This statement will be executed without anycondition.
Goto
Break Continue
KGISL ITECH
-
8/2/2019 C Intro Class1
102/108
The goto statement is a jump statement whichjumps from one point to another point within afunction.
Syntax
goto ;
KGISL ITECH
-
8/2/2019 C Intro Class1
103/108
KGISL ITECH
Example: goto statement
#include #include int main()
{ int a = 0; n: //Label
printf("\n %d",a); a++; if(a
-
8/2/2019 C Intro Class1
104/108
break statement Causes immediate exit from the loop
Used in while, for, dowhile or switchstatements
continue statement Skips remaining statements in loop body and start
from the beginning of loopUsed in for loop, while , dowhile loops
KGISL ITECH
-
8/2/2019 C Intro Class1
105/108
void main ( ){int k;for ( k= -5; k < 25; k= k+5){
printf(%d,k);printf(Good day\n);;
}}
KGISL ITECH
-
8/2/2019 C Intro Class1
106/108
void main ( ){
int k;for ( k= -5; k < 25; k= k+5)
{printf(%d\n, k);break;printf(Good day);
}}
KGISL ITECH
-
8/2/2019 C Intro Class1
107/108
void main ( ){int k;for ( k= -5; k < 25; k= k+5)
{printf(%d, k);conitnue;printf(Good Day);
}}
KGISL ITECH
Example: break & continue statement
#include
-
8/2/2019 C Intro Class1
108/108
#includevoid main()
{ int i;for(i=0;i