2 Unit
2 Unit
Variable name is a valid C that has been described as char data type
When its statement is encounted complier waits until a key is pressed and then assigns a
charcter as a value to getchar function
EX
Char course;
Course =getchar ();
Writng a charctrer :
Putchar() functions is used for writing one charcter at a time to the terminal
Syntax : putchar (variables name);
When variables name is atype char variable containg a charcter
Ex
Ans=’y’;
Putchar( ans );
2 formatted input
It refer to an input data that has been arranged in a particular format
Syntax :
Scanf (“control strings” ,&arg1,&arg2…………..,&argn);
Controls string specifies the field format in which the data is to be entered
Argunments arg1, arg2….argn specify the address of location where the data is stored
Control strings and argunments are seprated by commas
Controls srring may include
*field specification consisting of conversion charcters % a data type charcter (or)type
specified and an optional number , specifying field with
* blanks tabs ot newline
Blanks and new lines are ignored
The field width specified is optional
Data; 100
SYNTAX
If (test expression)
{
Statement_blocks:
}
Statement _x ;
Statement_ block may be a single statement or a group of statement
If the test expression is true the statement blocks will be executed
If the test expression is false the statement block will be skipped and the execution
jump into to the statement x
When the condition is true both the statement blocks and the statement—x are
executed is sequence
The program tests the type of category of the student if the student belongs to the
sports category then additional bonus marks are added to the marks
IF..ELSE STATEMENT
Syntax
If test (expression)
{
True-block statement
}
Else
{
False-block statement
}
Statement -x;
If the test expression is true ,then the statement are executed
Otherwise,false blocks statements are expressed
In either case,either true -blocks or false -blocks will be executed not both
In both cases the control is transferred subsequently to statement -x
Ex:
Counting the number of girls and boys in the class
For boys code=1
For girls code=2
If (code==1)
Boy=boy+1;
Else
Girl =girl+1;
Statement -x ;
If the code is equal to 1,the statement boy=boy+1 is executed and the control is
transferred to statement -x after skipping the else part
If the code is not equal to 1 the statement boy= boy+1 is skipped and the else part
girls =girls in 1+1 is executed before the control reaches the statement
NESTED IF-ELSE STATEMENT
When a series of decisions are involved more than one if else statement is used
If the condition 1 is false the statement -3 will be executed ;otherwise it continues to
perform the second test
If the condtion-2 is true the statement-1 will be evaluated otherwise the statement -2
will be evaluated and then the control is transferred to the statement x
EX :
If (gender==”f”
{
If (bal>5000)
Bonus=0.05*sal;
Else
Bonus =0.02*sal;
}
Else
{
Bonus =0.02*sal;
}
Sal=sal =bonus :
ELSE IF LADDER
C also permits decisions statements a multiple decisions is a chain of its in
which the statement associated with each else is an if
SYNTAX
If (condition-1)
Statement -1;
Else if (condition-2)
Statement -2
Else if (condition 3)
Statement-3;
Else if (condition )
Statement -n;
Else
Default statement
Statement-x;
This is known as else if ladder the condition are evaluated from the top to down if say one
conditions is true the statement asscicated with its executed and then the control is transferred
to the statement x remaining statement is skipped when all the condition becomes false then
the final else containing default statement will be executed
SWITCH STATEMENT
C has a built in multi way decision statement known as switch
Switch statement tests the value of a given variable against a list of case values and
when a match is found a block of statement associcated with that case is executed
Syntax
Switch (expression)
{
Case value -1;
Block-1;
Break ;
Case value -2 ;
Block -2;
Break;
Default
Default- block
Break;
}
GOTO STATEMENT
C support the goto statement to branch unconditionally from one point tp another in
the pogram
The goto requires a label in order to identify the place where the branch is to be made
A label in any valid variable name and must followed by colon
The label is placed immediartely before the statement where the control is to be
transferred
The label can be placed anywhere in the program either before or after the goto label
statement
Two types if goto statement
*forward jump
* backward jump
Syntax for forward jump
goto label;
----
-----
Label:
Statement ;
Ex
Main()
{
Double x,y;
Read;
Scanf(“%f”,&x);
If (x<0)goto read;
Y=sqrt(x);
Printf (“%f%f”,x,y);
Goto read;
}
This program uses two goto statement
The(2) goto statement after printing the x to transfer the control back to the input
statement and the other to skip any futjer compound when the number is negative
Due to unconditional goto the statement at the end the control is always transferred
back to the input statement
ITERATION STATEMENT
Iteration statement are also known an loops allows a set of instruction to be performed
until a certain condition becomes false
The condition may be predefined or open ended
The loop strcture avaible in ‘C’one
.. the while loop
.. the do.. while loop
.. the for loop
In looping a sequences of statement are executed until sources condition for
terminiation of the loop are stasified
A program loop consist of two segements one known as the body of the loop and the
other known ad the control statement
The control statement tests certain condition and then directs the repeated executions
of the statement contained in the body of the loop
Depending upon the position of the control statement in the loop a control strcture
may be classified as the entry- controllec loop or exit – controlled loop
WHILE STATEMENT
While statement is used to carry out looping operations in which a group is statement
is executed repeadtly until some condition has been stasfied
Syntax
While (test condtion)
{
Body of the loop
}
While is an entry – controlled loop statement
The process of repeated executions of the body contuines until the test = condition
finally becomes false and the control is transferred of the loop
On exit the program continue with the statement immedieatly after the body of the
loop
The body of the loop may have one or more statement
Braces are needed only if the body contains two or more statement
Ex
Main()
{
Int digit =0;
While digit =0;
While (digit <=9)
{
Printf (“%d\n”.digit );
++ digit :
}
}
Output
0
1
2
3
4
5
6
7
8
9
Syntax
For (initialization – condition increment )
{
Body of fhe loop
}
The execution of for statement is as follows
Intiallization of the control variables is done first using assignment statement
A values of the control variable is tested using the text condition the test condition is a
relational expressions determines when the loop will exit
If the condition is true the body of the loop is executed otherwise the loop is
terminated and the execution continues with the statement that the immediuatetly
follows the loop
When the body of the loop is executed the control is transferred back to the foe
statement after the evaluating the last statement in the loop if the conition
Is stasified the body of the loop is again executed this process continues till the value
of the control variable fails to satisfy the test condition
Ex:1(incrementing)
For (x=0<=9 ;x-x+1 )
{
Printf (“%d”,x);
}
This loop allows negative increments this above loop will be executed for 10 times
and prints value 9to 0 in one line
The nesting may contains up to 15 levels many compliers allow more than 15
Ex
M=5 :
For(i=1;i<=16;i++)
{
Sum=i*m;
Printf (“%d%d%d”I”*”m”=”sum);
}
UNCONDITIONAL TRANSFER STATEMENT
C performs as unconditional branch
Goto
Break
Continue
Unconditional branching means “transfer control from the point where it is specified
statement
Go to can be used anywhere in the program where break and continue statements are
conjunctions with any of the loop statement
BREAK STATEMENT
The break statement can be used to terminate a case in the switch statement and to
foree immediate of loop bypassing normal loop condtional test
When the break statement is encourtred inside a loop the loop immediately terminated
and the program control passive to the statement following the loop
Syntax
Break
Ex
For (i=1<=5;i++)
{
----
----
Break ;
}
CONTINUE STATEMENT :
The contitune statement causes the next iteration of the enclosing loop to begin
When this statement is encourted in the program the remaining statement in thebody
of the loop are skipped and the control is passed to the re – initialization stop
Continue directs the loop to increments portion of the loop and then condtional test
Syntax
Continue
EXIT () FUNCTIONS
Used to terminates a program just break statement is used to terminate a loop a return
statement is used to terminates a function
Exit () function is used to terminate the program and return control to the operating
Exit function is of two types
Exit (0)
Exit(1)