Post on 16-Jan-2016
description
transcript
Introducing ' C ' LanguageIntroducing ' C ' Language VINAY KUMAR GUPTA VINAY KUMAR GUPTA 11
Introducing the
‘c’
Programming language
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 22
What is ‘C’ Programming Language ?
‘C’ is a general purpose procedural programming language.It is developed at AT & T’s Bell Laboratories of USA in 1972.It was designed and written by a man named Denis Ritche.‘C’ is a Mid Level Language.
Introducing ' C ' LanguageIntroducing ' C ' Language VINAY KUMAR GUPTAVINAY KUMAR GUPTA 33
Getting started with ‘C’ language. Steps in learning English
language :-Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. However, there is a close analogy between learning English language and learning ‘C’ language.The method of learning English and learning ‘c’ language are given below :-
Steps in learning ‘ C ’ language :-
Character set
Word
Sentence
Paragraph
Alphabets ,Digits, Special symbols
Constants, Variables, Keywords
Instructions
Programs
Introducing ' C ' LanguageIntroducing ' C ' Language VINAY KUMAR GUPTA VINAY KUMAR GUPTA 44
The character set :-
Alphabets :- a to z , A to ZDigits :- 0,1,2,3,4,5,6,7,8,9
Special Symbols :- =,+,-,*,/,%,>,<, ~, ‘,!,@,#,^,&,( ),|,?,:, [ ] , { } , ; , ” ,etc.
Constants :-A constant is a quantity that doesn’t change.
e.g:- 10,20,30 etc.Variables:-
A quantity(constant) which can be stored at a locations in the memory of the computer. A variable can be considered as a name given to the location in memory where this constant is stored. e.g :-
x=10;
y=10; Here x and y is the variable.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 55
WORD
RESERVED WORD USER DEFINED WORD
Some specific meaning in its dictionary.Such as :- +,-,*,/, int , float , char , etc.It is defined by the
user.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 66
TOKENS : -
It is the smallest unit of the sentence or
statement.TOKENS(WORD)
KEYWORD IDENTIFIER SEPERATOR LITERALS OPERATOR
Keywords are reserved words who have special meaning in the ‘C’ library. e.g :- int,float,char etc.
It is always written in small letter. The total number of the keywords is 32.It is the memory location name where we can
store a value at a time.e.g :- x = 5 , Here x is a identifier.
Literals(constant):- literal are fixed value of the identifer. Such as :- x = 10 or nm =
“ram”.Here 10 and ram are literals.
“ ;” ,” { }” and “ , “ are seperators. It is used for
seperates the tokens.
Operators are special symbol who have an special meaning in arithmetical or logical expression. Such as :- + , * , - , / , % , ! , !
= , = = , > , < , < = , > = etc.{
s = x + y ;P = x * y ;
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 77
WORD
RESERVED WORD USER DEFINED WORD
IDENTIFIER LITERALSKEYWORD SEPERATOR OPERATOR
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 88
RULES FOR NAMING IDENTIFIER : -1. It can have maximum of 32 characters, but first eight character should be unique. Such as :- Student_no and student_name will be treated as same name because first character is not unique.
2 . First character should be an alphabet or under score (Hypen with sift). e.g : - s1 , _s etc.
Don’t write such as :-
1s , -s etc.
3 . No special symbols are used in identifier except underscore.
e.g : - roll_no , r_home etc.
Don’t write like this :- roll-no , roll.no etc.
4 . Identifier can’t be a keyword. 5 . Lower case and Upper case alphabet are significant.
e.g : - x , y are different from X , Y .
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 99
PROGRAM
TOP - DOWN BOTTOM - UP
‘ C ‘ IS A PROCEDURAL PROGRAMMING LANGUAGE.
[ BASIC , COBOL , FOXPRO ETC. ] IN THIS METHOD FIRST
MAINAND THEN
U D F
[ C ++ , C # , JAVA ETC. ][OOPS]
OBJECT ORIENTED PROGRAMMING LANGUAGE.
IN THIS METHOD FIRSTU D F
AND THENMAIN
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1010
STRUCTURE OF THE ‘ C ‘ PROGRAM
DOCUMENTATION
ATTACHED HEADER FILE
MACRO DEFINITION
MAIN FUNCTION
USER DEFINED FUNCTION
IT IS AN OPTIONAL ENTRY. FOR THIS WE USE COMMENTS [/*
STATEMENT */]IT IS AN OPTIONAL ENTRY. SUCH AS :-
#include <stdio.h>#include <conio.h> etc.IT IS AN OPTIONAL ENTRY. FOR
THIS , WE USE “ && FOR AND “ , “ NULL FOR 0 “ , “ || FOR OR
“ ,ETC.
IT IS MANDATORY ENTRY. IT IS AN OPTIONAL
ENTRY. IF YOU WANT TO MAKE
PROCESS.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1111
STRUCTURE OF MAIN FUNCTION
main ( )
{
Body of main function
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1212
BODY OF MAIN FUNCTION
DECLERATION ASSIGNMENT PROCESS PRINT
VARIABLE DECLERATION . SUCH AS :- int x;
float y; etc.
ASSIGN VALUES OR PUT VALUES OF THE VARIABLE. SUCH AS :- int x = 10;
Float y = 30.45; etc.PROCESS (LOGICAL OR
ARTHIMETICAL). SUCH AS :-S = X * Y;
if( x > y) etc.
PRINT IT MEANS OUTPUT OF THE PROGRAM.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1313
CONSTANT
NUMERIC( 0 T0 9 DIGITS)
NON - NUMERIC
INTEGER REAL CHARACTER
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1414
DATA TYPE
INT FLOAT DOUBLE CHAR
FORM IN INTEGER. SIZE BASICALLY 2 BYTES, DATA CAN BE STORED TILL 16 BITS.(FIRST BIT RESERVES FOR SIGN BIT). VALUE
VARY FROM -32767 TO +32768
DATA IS STORED IN REAL FORM. SIZE IS BASICALLY 4 BYTES . DATA CAN BE STORED TILL 32 BITS.
DATA IS STORED IN REAL OR INTEGER FORM BOTH. SIZE BASICALLY 8 BYTES. DATA CAN BE
STORED TILL 64 BITS.
DATA IS STORED IN CHARACTER(ENGLISH ALPHABET) FORM. SIZE BASICALLY 1 BYTE.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1515
INT
INT LONG INT
SIZE IS 2 BYTES. DATA CAN BE STORED TILL 15 BITS AND ONE
BIT IS FOR SIGN ( + OR -). IN SIGNED INT ,VALUE OF THE
DATA WILL VARY FROM -32767 TO +32768.(i.e. 215 ) AND
UNSIGNED INT THE SIZE OF THE DATA IS 216.
SIZE IS BASICALLY FOUR BYTES. IN SIGNED LONG INT , DATA CAN BE STORED TILL 0 TO 231 VALUE
AND IN UNSIGNED LONG INT , DATA CAN BE STORED TILL 0 TO
232 VALUE.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1616
FLOAT
FLOAT LONG FLOAT
SIZE IS BASICALLY 4 BYTES. IN SIGNED FLOAT ,DATA CAN BE STORED TILL 231VALUE AND IN UNSIGNED
FLOAT , DATA CAN BE STORED TILL 232 VALUE.
SIZE IS BASICALLY 8 BYTES . IN SIGNED LONG FLOAT , DATA CAN BE STORED TILL 263
VALUE AND IN UNSIGNED LONG FLOAT , DATA CAN BE STORED TILL 264 VALUE.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1717
DOUBLE
DOUBLE LONG DOUBLE
SIZE IS BASICALLY 8 BYTES( 64 BITS). IN SIGNED DOUBLE , DATA CAN BE STORED TILL 263 VALUE AND IN
UNSIGNED DOUBLE , DATA CAN BE STORED TILL 264
VALUE.
SIZE IS BASICALLY 10 BYTES (80 BITS). IN SIGNED LONG DOUBLE , DATA CAN BE STORED
TILL 279 VALUE AND IN UNSIGNED LONG DOUBLE , DATA CAN BE STORED TILL 280
VALUE.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1818
DATA TYPES AND THEIR KEYWORDS :-
DATA TYPE KEYWORD EQUIVALENT
Character char
Unsigned character unsigned char
Signed character signed char
Signed integer signed int (or int)
Unsigned integer unsigned int
Signed short integer signed short int ( or short int)
Signed long integer signed long int ( or long int)
Unsigned short integer unsigned short int
Unsigned long integer unsigned long int
Floating point float
Double precision floating point double
Extended Double precision
floating point long double
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 1919
DECLERATION
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2020
HOW TO DECLARE A VARIABLE
SYNTAX :-
DATA TYPE VARIABLENAME ;
IT IS USED FOR ONE VARIABLE DECLERATION OF ONLY ONE DATA
TYPE.EXAMPLE:- int x ;
int y; etc.
DATA TYPE VARNM1,VARNM2,VARNM3,……… ;
IT IS USED FOR DECLERATION OF MANY VARIABLES OF SAME DATA TYPE.
EXAMPLE :- int x,y;float s,p; etc.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2121
ASSIGNMENT
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2222
HOW TO ASSIGN A VALUE
COMPILE TIME INITIALIZATION RUN TIME INITILIZATION
IT MEANS YOU CAN ASSIGN A VALUE WITH VARIABLE DECLERATION ( SUCH AS :- int x = 10;)
OR AFTER THE VARIABLE DECLERATION(SUCH AS:- int x;
X = 10; IT IS ALSO CALLED COPY A VALUE. AFTER THE DECLERATION , BY DEFAULT VALUE OF THE VARIABLE IS
GARBAGE.)
IT MEANS , YOU CAN ASSIGN A VALUE OF A VARIABLE AFTER RUNNING THE PROGRAM. FOR THIS
INITIALIZATION, WE USE scanf(“ “ ); KEYWORD.THE SYNTAX OF THE SCANF( ) KEYWORD :-
Scanf(“type specifier”,&variablename);int x ;
&x|----|----|-----|----|----|----|------||----|----|-----|----|----|----|------|
^0 1 2 3 4 5 6 NOTE :-
“ & “ IS GENERALLY USED FOR PUTTING THE VALUE OF A VARIABLE IN CERTAIN PLACE.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2323
COMMONLY USED scanf FORMAT CODES :-
CODE MEANING %c read a single character %d read a decimal number %e read a floating point value %f read a floating point value %g read a floating point value %h read a short integer %i read a decimal, hexadecimal
of octal number %o read an octal number %s read a string %u read an unsigned decimal
number %x read a hexadcimal integer %[..] read a string of word(s)
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2424
TYPE SPECIFIER :-
DATA TYPE TYPE SPECIFIER
integer ------------- int %d
long int %ld
float ------------- float %f
long float %lf
double ---------- double %ld
or, %lf
character --------- char %c
string ----------- char %s
PRINTF ( ) KEYWORD :-
IT IS USED FOR PRINTING SOME TEXT OR MAKING GOOD
OUTPUT OF THE PROGRAM.
int x ; [ -------- decleration ]
printf(“ enter the no.”);
scanf(“%d”,&x); [-------- run time initialization ]
SYNTAX OF printf ( ) KEYWORD :-
printf (“ statement “);
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2525
PROCESS
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2626
PROCESS (ARITHEMETICAL AND LOGICAL) :-
IT MEANS, YOU CAN PERFORMS THE LOGICAL OR ARITHEMETICAL EXPRESSION OR OPERATION.
ARITHEMETICAL SUCH AS :-
+ , - , * , / , % ETC.
LOGICAL SUCH AS :-
&& , || , != , < , > , < = , > = ETC.
EXAMPLE :-
int s,x,y; [ Decleration]
s = x + y ; [ Process]
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2727
OUTPUT
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2828
OUTPUT :-IT MEANS , YOU CAN SEE THE OUTPUT OF THE
PROGRAM. FOR THIS WE USE printf KEYWORD.
SYNTAX :-
printf (“CONTROL STRING”,LIST OF VARIABLES);
EXAMPLE :-
int x,y,s; [DECLERATION]
printf(“enter the first no:”);
scanf(“%d”,&x); [INITIALIZATION]
printf(“enter the second no:”);
scanf(“%d”,&y); [INITIALIZATION]
s = x + y;
printf(“sum of two nos=%d”,s); [OUTPUT]
[or printf(“%d”,s); ] [OUTPUT]
CONTROL STRING
STRING CONSTANT
(STATEMENT)
OPTIONAL ENTRY
TYPE SPECIFIER
(MANDATORY)
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 2929
A SIMPLE PROGRAM IN ‘C’ LANGUAGE :-
main ( )
{
printf(“this is my first program.”);
printf(“Developed by RAKESH ANAND”);
}
A PROGRAM TO PRINT THE SUM OF TWO NOS. THROUGH COMPILE TIME INITIALIZATION:-
main( )
{
int x,y,s;
x = 10;
y = 25 ;
s = x + y;
printf(“SUM OF TWO NOS.=%d”,s);
}
A PROGRAM TO PRINT THE SUM OF TWO NOS. THROUGH RUN TIME INITIALIZATION:-
main( ){
int x,y,s;printf(“enter the 1st nos.:”);scanf(“%d”,&x);printf(“enter the 2nd nos,:”);scanf(“%d”,&y);s = x + y;printf(“SUM OF TWO NOS.=%d”,s);
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3030
HOW TO COMPILE A PROGRAM :-
FOR THIS , WE USE [Alt + c] OR [Alt + F9] OR [F9].
COMPILING IS GENERALLY USED FOR SEEING THE ERROR OF THE PROGRAM.
HOW TO RUN A PROGRAM :-
FOR RUNNING THE PROGRAM , WE USE
[Alt + r ] or [Ctrl + F9]HOW TO SEE OR VIEW THE OUTPUT OF A PROGRAM :-
FOR THIS, WE USE [Alt + F5]HOW TO SAVE A PROGRAM :-
FIRSTLY, WE USE [Alt + f] AND THEN SELECT SAVE SUB MENU AND PRESS ENTER KEY AND THEN GIVING THE NAME WITH DESIRED PLACE AND THEN PRESS ENTER KEY.
OR
PRESS F2 AND THEN GIVING THE NAME WITH DESIRED PLACE AND THEN PRESS ENTER KEY.
HOW TO CLEAR THE SCREEN :-
FOR CLEARING THE SCREEN , WE USE
clrscr( );
IT IS USED AFTER THE DECLERATION OF THE VARIABLE. SUCH AS :-
int x,y,z; [Decleration ]
clrscr( );
HOW TO GET THE FULL SCREEN :-
FOR FULL SCREEN , WE USE
[ Alt + enter ] KEY.HOW TO BREAK THE EXECUTION OF THE PROGRAM ON THE SCREEN :-
FOR THIS , WE USE getch( );
BUT IT IS GENERALLY USED FOR INPUTTING THE VALUE OF THE VARIABLE OR SOMETHING.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3131
Firstly press Alt + f key and then
select New option for
creating new ‘C’ file and then press enter key.
Write the code or program in
this area.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3232
For saving the
program, firstly press Alt + f key and then select the
save option and then
press enter key .( or press F2 function
key).
After perssing the
enter key and it gives a box
for writing the file name with desired path and then
press enter key.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3333
Firstly press Alt + c key and then press
enter key for compiling the
program.
After pressing enter , a dialogue box appears and it shows many things and
then press enter key. If any error occurs firstly
correct the error(s). And then run the program.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3434
For running the program , firstly press Alt + r key and then
press enter key.
After pressing
enter key ,it will show
and wait for inputing.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3535
Input the required value of
the variable and then
press enter key.
After pressing the enter key, it will show the main
screen . And then press
Alt + F5 key for seeing
the output of the program.
After pressing
these key it will show
the accurate ouput.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3636
ESCAPE SEQUENCE :-
\a ----------- Audible alert (Bell)
\b ---------- Back space
\f ---------- Form feed
\r ---------- carriage return
\n ----------- New line
\t ----------- Horizontal Tab
\v ----------- Vertical Ttab
\’ ----------- Single quote
\” ----------- Double quotes
\? ----------- Question mark
\\ ----------- Back slash
\0 ----------- Null
‘ C ’ support some special backslash character constants that are used in output function. It is combination of backslash and one caharacter. These character combination are called ESCAPE SEQUENCE.
HOW TO ESCAPE FROM THE ‘ C ’ COMPILER :-
FOR OUTGOING FROM ‘C’ COMPILER, WE PRESS [Alt + x ] key.
SOME NOTES :-
‘0’ IS USED FOR FALSE STATEMENT OR CONDITION. (-VE)
‘1’ IS USED FOR TRUE STATEMENT OR CONDITION. (+VE)
COMMENTS :-
IT IS USED FOR DOCUMENTATION AND YOU CAN TAKE IT ANYWHERE IN THE PROGRAM.
SYNTAX :-
/* --------- STATEMENT----------- */
IT DOESN’T PARTICIPATES IN THE PROGRAM EXECUTION.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3737
OPERATOR
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3838
ARITHMETICOPERATOR
ARITHMETICOPERATOR
ASSIGNMENTOPERATOR
ASSIGNMENTOPERATOR
UNARYOPERATOR
UNARYOPERATOR
TERNARY(CONDITIONAL)
OPERATOR
TERNARY(CONDITIONAL)
OPERATOR
LOGICALOPERATOR
LOGICALOPERATOR
RELATIONALOPERATOR
RELATIONALOPERATOR
OPERATOROPERATOR
‘ + ’ , ‘ – ’ , ‘ * ’ , ‘ / ’ ,
‘ % ’
Modular(remainder)
multiplication > , < , > = , < = , = = (comparision) ,! =(not equa
to) etc.
= (c = 0 ) , + = ( c = c +a or c+=a),
-= (c = c – a or c-=a),*= (c = c * a or (c*=a),/ = (c = c / a or c/=a ) ,
% = (c = c % a or c%=a) etc.
It is used for compound relation.
&& (and) ,|| (or ) etc.
++ (increment ) and -- (decrement) .
(C = C + 1 OR C+=1 OR ++C)AND
(C = C – 1 OR C-=1 OR --C)
( ? : ) SYNTAX :-
(COND) ? TRUE STATEMENT : FALSE STATEMENT ;EXAMPLE :-
Write a program to fine the biggest no. among two nos.
Soln :- main( ) { int x,y,b;
clrscr( );Printf(“ enter the two nos. ”);Scanf(“ %d %d ”,&x , &y);
b = ( x > y ) ? x : y ;printf(“Biggest no = %d ”,b);
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 3939
OPERATOR PRECEDENCE AND ASSOCIATIVITY :-OPERATOR DESCRIPTION ASSOCIATIVITY RANK-------------------------------------------------------------------------------------------------------- ( ) Function call Left to Right 1 [ ] Array element reference -------------------------------------------------------------------------------------------------------- + Unary plus - Unary minus + + Increment - - Decrement ! Logical negation Right to left 2 ~ One’s complement * Pointer reference & Address sizeof Size of an object (type) Type cast (conversion)-------------------------------------------------------------------------------------------------------- * Multiplication / Division Left to right 3 % Modulus--------------------------------------------------------------------------------------------------------
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4040
OPERATOR PRECEDENCE AND ASSOCIATIVITY :-
OPERATOR DESCRIPTION ASSOCIATIVITY RANK-------------------------------------------------------------------------------------------------------- + Addition Left to Right 4 - Substraction -------------------------------------------------------------------------------------------------------- << Left shift Left to right 5 >> Right shift-------------------------------------------------------------------------------------------------------- < Less than Left to right 6 < = Less than or equal to > Greater than > = Greater than or equal to-------------------------------------------------------------------------------------------------------- = = Equality Left to right 7 ! = Inequality--------------------------------------------------------------------------------------------------------- & Bitwise AND Left to right 8--------------------------------------------------------------------------------------------------------- ^ Bitwise XOR Left to right 9---------------------------------------------------------------------------------------------------------
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4141
OPERATOR PRECEDENCE AND ASSOCIATIVITY :-
OPERATOR DESCRIPTION ASSOCIATIVITY RANK-------------------------------------------------------------------------------------------------------- | Bitwise OR Left to Right 10 -------------------------------------------------------------------------------------------------------- && Logical AND Left to right 11-------------------------------------------------------------------------------------------------------- || Logical OR Left to right 12-------------------------------------------------------------------------------------------------------- ? : Condition expression Right to left 13------------------------------------------------------------------------------------------------------- = Assignment operators Right to left 14 * = , / = , % = + = , - =, & = ^ = , | = << = , >> =--------------------------------------------------------------------------------------------------------- , Comma operator Left to right 15
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4242
STATEMENT
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4343
STATEMENT
EXPRESSIONSTATEMENT
LABELSTATEMENT
CONTROLSTATEMENT
s = x + y ;p = x * y;r = n % 2;
Etc.
SUM : s = x + y ;p = x * y ;
For this purpose, we use goto keyword.
It is a label name.
It will be discussed in next slide.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4444
CONTROL STATEMENT
SELECTION STATEMENT
JUMPING STATEMENT
ITERATIONSTATEMENT
IT IS USED FOR MAKING SINGLE OR MULTIPLE DECISION. IN TRUE OR FALSE CONDITION. FOR THIS , WE USE
CONDITIONAL OPERATOR OR IF – ELSE STATEMENT.
IT IS USED FOR REPEATING( OR MAKING LOOP) THE SAME STATEMENT OR PROCESS. FOR THIS , WE USE DO
– WHILE , WHILE AND FOR LOOP.
IT IS USED FOR JUMPING THE STATEMENT FOR ONE PLACE TO
DESIRED PLACE . FOR THIS , WE USE GOTO,BREAK AND CONTINUE
STATEMENT.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4545
SELECTION STATEMENT(BRANCHING)
DIAGRAM : -
START
PROCESS 1
CONDITION
PROCESS 3 PROCESS 2
YES
(TRUE)
NO
(FALSE)
PROCESS 4
END
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4646
p = x * y ;s = x + y;
int x ,y , s , p ;
x > yN y
Print result
EXAMPLE ON SELECTION
STATEMENT (BRANCHING)
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4747
JUMPINGSTATEMENT
DIAGRAM :-
START
PROCESS 1
PROCESS 2 COND
PROCESS 3
PROCESS 4
PROCESS 5
Y
N
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4848
ITERATION (LOOP)STATEMENT
DIAGRAM :-
START
PROCESS 1
PROCESS 2
PROCESS 3 COND
Y
PROCESS 4 END
N
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 4949
DECISION MAKING AND
BRANCHING
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5050
HOW TO MAKE A SELECTION :-
1. USING TERNARY OPERATOR OR CONDITION OPERATOR
2. USING “ IF STATEMENT ”
“ if ” is a keyword which makes a
selection.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5151
IF STATEMENT
DIAGRAM AND SYNTAX :-
ONE WAY DECISION DIAGRAM :- SYNTAX :-
PROCESS 1
START
COND
Y
PROCESS 2
PROCESS 3 END
If (cond ) Process;
if (cond){
process1;Process2;Process3;………………
}
Single statement
Multiplestatement
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5252
IF STATEMENT
DIAGRAM AND SYNTAX :-
SYNTAX :-
PROCESS 1
START
COND Y
PROCESS 2PROCESS 3
END
If (cond ) process;else process;
Single statement
Multiplestatement
if (cond){ process1; process2; ………} else{ process1; process2; ……….}
TWO WAY DECISION DIAGRAM :-
N
PROCESS 4
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5353
IT IS COMM-ENTS LINE.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5454
ASSIGNMENT 1
1. Ramesh’s basic salary is input through keyboard. His dearness allowance is
40% of basic salary, and house rant allowance is 20% of basic salary. Write
a program to calculate his gross salary.
2. The distance between two cities(in km.) is input through the keyboard.
Write a program to convert and print this distance in meters,feet, inches
and centimeters.
3. The marks obtained by a student in five different subjects are input
through the keyboard, find out the aggregate marks and percentage marks
obtained by the student. Assume that the maximum marks that can be
obtained by a student in each subject is 100.
4. Temperature of a city in farenheit degrees is input through the keyboard.
Write a program to convert this temperature into centigrade degrees.
5. The length and brath of a rectangle and radius of a circle are input through
the keyboard. Write a program to calculate the area and perimeter of the
rectangle, and the area and circumference of the circle.
6. Two numbers are input through the keyboard into two localtions C and D.
Write a program to interchange the contents of C and D.
7. If a five digit number is input through keyboard, Write a program to
calculate the sum of its digits. (Hint :- Use % (modular) operator)
8. If a five digit number is input through the keyboard, Write a program to
reverse the number.
9. If a four digits number is input through the keyboard , Write a program
to obtain the sum of the first and last digit of this number.
10. Write a program to check the year is leap or not.
11. Write a program to check the number is prime or not.
12. Take quantity and rate as an input and Write a program to calculate the
amount discount and net amount on this condition :-
if amount is more than 500 then discount will be 20% of amount.
13. Take no. of units consume as an input. Write a program to calculate bill
amount on following condition :-
No. of units charge
Up to 100 Rs. 2/unit
For next unit Rs. 3/unit
14. Take no. of hours as an input.Write a c progran to calculate labours
weighes on following condition:-
No. of hours charge
up to 8 hours Rs. 55.00
for nest hours Rs. 25/hour
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5555
NESTED IF – ELSE STATEMENTWhen a series of
decisions are involved, we may have to
use more than one if…
else statement.
DIAGRAM AND SYNTAX :-
DIAGRAM :- SYNTAX :-
START
PROCESS 1
COND1 TRUE COND2 TRUE PROCESS 2
FALSE
FALSE
PROCESS 4PROCESS 3
PROCESS 5 END
if (condition 1){ if ( condition 2 ) { process 1; } else { process 2; }}else{ process 3;}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5656
Q. Take sex and balance as an input and Write a program to calculate the net balance On following condition :- I ) . If sex is female and balance is greater than 5000 then bonus will be 5% on balance amount. II ) . If sex is female and balance is not greater than 5000 then bonus will be 2% on balance amount. III ) . Otherwise bonus will be 2% on balance amount.
( Hint :- net balance = balance + bonus )
Solution :- main( ){
char sex;float balance,bonus,netbalance;clrscr( );printf(“Enter the values of sex and balance:”);scanf(“ %c %f ”,&sex,&balance);if( sex = = ‘f ’)
{if(balance > 5000){ bonus = 0.05 * balance;}else{ bonus = 0.02 * balance;}
}else{ bonus = 0.02 * balance;
}netbalance = balance + bonus;printf(“Net balance = %f\nBonus = %f\n ”,netbalance,bonus);
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5757
ELSE IF LADDER STATEMENTThere is
another way of putting ifs
together when
multipath decisions are involved. A decision is a chain of ifs in which the
statement associated with each
else is an if.
DIAGRAM :-
DIAGRAM :-
START
PROCESS 1
COND1
PROCESS 2
COND2
PROCESS 3
COND3 COND n
DEFAULTSTATEMENT
PROCESS 4 PROCESS 5
PROCESS 6 END
FALSE FALSEFALSE FALSE
TRUE TRUE TRUE TRUE
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5858
ELSE IF LADDER STATEMENT
SYNATAX :-
if ( condition 1) process 1; else if (condition 2 ) process 2; else if (condition 3 ) process 3); else if (condition n) process n; else default process; process;
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 5959
Q. Take marks of two subjects as an input and write a program to calculate total marks and percentage and print remarks whether the student is grading 1st , 2nd , 3rd division of fail.
Soln :- main ( ){ float m1,m2,tm,avgm; clrscr( ); printf(“Enter the marks m1 and m2 :”); scanf(“%f %f ”,&m1,&m2); tm = m1 + m2 ; avgm = tm / 2 ;
if ( avgm >= 60 && avgm < 100 ) printf(“ FIRST DEVISION ”); else if ( avgm >= 45 && avgm < 60 )
printf(“ SECOND DEVISION ”); else if ( avgm >= 30 && avgm < 45 )
printf(“ THIRD DEVISION ”); else
printf(“ FAIL ”);}
Marks of subject 1
Marks of subject 2
Total marks
Average marks
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6060
FORMS IF STATEMENT
if ( condition ) process ;
If ( condition ){
process 1;process 2;
}
if ( condition )process;
elseprocess;
if ( condition) { process 1;
process 2; }else {
process 1;process 2;
}
if ( condition )process;
else{
if (condition )process 1;
elseprocess 2;
}
if ( condition ){
if (condition )process 1;
else process 2;
}else{
process 3;}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6161
CONDITIONAL OPERATOR
DIAGRAM AND SYNTAX :-
DIAGRAM :-
START
PROCESS 1 COND PROCESS 2
PROCESS 3
TRUE
FALSE
PROCESS 4
END
SYNTAX :-
( CONDITION ) ? TRUE : FALSE ;
FOR THIS , WE USE ? : OPERATOR
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6262
Q. Write a program to check a given no. whether it is even or odd ?
#include <stdio.h>#include <conio.h>main( ){
int n;clrscr( );printf(“ENTER THE NO :”);scanf(“ %d ”, &n);( n % 2 = = 0 ) ? printf ( “ EVEN ”) : printf ( “ ODD ”);
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6363
NESTED CONDITIONAL OPERATOR
DIAGRAM AND SYNTAX :-
DIAGRAM :-
START
PROCESS 1
COND1 COND2
PROCESS 3
PROCESS 2PROCESS 4 PROCESS 5 END
TRUE TRUE
FALSE
FALSE
SYNTAX :-
(( COND 1) ? ( ( COND 2) ? TRUE : FALSE ) : FALSE);
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6464
Q. Take sex and balance as an input and Write a program to calculate the net balance On following condition :- I ) . If sex is female and balance is greater than 5000 then bonus will be 5% on balance amount. II ) . If sex is female and balance is not greater than 5000 then bonus will be 2% on balance amount. III ) . Otherwise bonus will be 2% on balance amount.
( Hint :- net balance = balance + bonus )Solution :-
main( ){
char sex;float balance,bonus,netbalance;clrscr( );printf(“Enter the values of sex and balance:”);scanf(“ %c %f ”,&sex,&balance); bonus = (( sex = = ‘f ’) ? ((balance > 5000) ? (0.05 * balance) : (0.02 * balance)) :
(0.02 * balance)) ;netbalance = balance + bonus;printf(“Net balance = %f\nBonus = %f\n ”,netbalance,bonus);
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6565
FORMS OF CONDITIONAL OPERATOR
( CONDITION ) ? TRUE : FALSE ;( CONDITION ) ? (( CONDITION) ? TRUE : FALSE ) : FALSE ;( CONDITION ) ? TRUE : (( CONDITION ) ? TRUE : FALSE );( CONDITION ) ? (( CONDITION ) ? (( CONDITION ) ? TRUE : FALSE ) : FALSE ) : FALSE ;
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6666
SWITCH STATEMENTThe Control statement which
allows us to make a decision from the number of choices is called a switch , or more correctly a
switch – case – default .
DIAGRAM :-
START
PROCESS 1
COND1
PROCESS 2break;
COND2
PROCESS 3break;
COND3 COND n
DEFAULTSTATEMENT
PROCESS 4break;
PROCESS 5break;
PROCESS 6 END
FALSE FALSEFALSE FALSE
TRUE TRUE TRUE TRUE
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6767
SWITCH STATEMENT
SYNTAX :-
switch ( integer expression ){
case constant 1 : process ;break;
case constant 2 : process;break;
case constant 3 : process;break;
case constant n : process;break;
default : default process; }
Break is used forTerminating the
Execution of block.
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6868
Q. Take day number as an input and print the corresponding day .
main ( ){
int dn;clrscr( );printf(“ Enter the day no. : ”);scanf(“ %d ”, &dn);switch ( n ){
case 1 : printf ( “ SUNDAY ”); break;case 2 : printf (“ MONDAY ”); break;case 3 : printf (“ TUESDAY ”); break;case 4 : printf (“ WEDNESDAY ”); break;case 5 : printf (“ THURSDAY ”); break;case 6 : printf (“ FRIDAY ”); break;case 7 : printf (“ SATURDAY ”); break;default : printf (“ INVALID DAY NO. ”);
}}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 6969
JUMPING STATEMENT
BREAK CONTINUE GOTO
We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test.
The keyword break allows us to do this. When the keyword break encountered inside any C loop, control automatically passes to the
first statement after the loop.A break is usually associated with an if.
In some programming situations we want to take the control to the beginnig of the loop, by passing the statements inside the
loop which have not yet been executed. The keyword continue allows us to do this. When the keyword continue encounteredinside any C loop control automatically passes to the beginnig
of the loop.A continue is usually associated with an if.
In some programming situation we want to transfer the control to the specific location . The keyword goto allows us to do this.
A goto keyword usually associated with if .
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7070
GOTO STATEMENT
DIAGRAM AND SYNTAX :-
DIAGRAM
START
LABEL 1 :
PROCESS 1
COND TRUE
G
O
T
O
PROCESS 2 ENDFALSE
SYNTAX :-
LABEL 1 : process 1;
process 2; if ( cond )
goto LABEL 1;
LABELNAME
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7171
Q. Write a program to print the sum of the two positive nos. ?
main ( ){
int x,y,s;clrscr( );printf(“ Enter the two nos. : ”);scanf(“ %d %d ”, &x, &y);input1 : printf (“ Enter the 1st no. :”);
scanf (“ %d ”, &x);if ( x < 0 ) goto input1;
input2 : printf (“ Enter the 2nd no. :”); scanf( “ %d ”, &y);
if ( y < 0 ) goto input2;
s = x + y;printf(“ SUM OF TWO NOS.=%d ”,s);
}
LABEL NAME
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7272
BREAK STATEMENT
DIAGRAM AND SYNTAX :-
DIAGRAM
START
PROCESS 1
PROCESS 2
COND
PROCESS 3
TRUE
PROCESS 4FALSE
PROCESS 5
BREAK
END
SYNTAX :-
if ( condition ){
process1;process2;break;
}
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7373
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7474
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7575
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7676
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7777
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7878
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 7979
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8080
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8181
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8282
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8383
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8484
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8585
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8686
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8787
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8888
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 8989
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9090
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9191
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9292
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9393
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9494
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9595
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9696
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9797
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9898
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 9999
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 100100
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 101101
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 102102
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 103103
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 104104
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 105105
Introducing ' C ' LanguageIntroducing ' C ' Language RAKESH ANAND RAKESH ANAND 106106