C UNIT2
C UNIT2
exp(4.0) is 54.598150
abs(x) Absolute value of a number
abs(5)=5
abs(-8)=8
log(x) natural logarithm of x (base e) log(2.0) is 0.693147
log(4.0) is 1.386294
log10(x) logarithm of x (base 10) log10(10.0) is 1.0
log10(100.0) is 2.0
fabs(x) absolute value of x fabs(2.0) is 2.0
fabs(-2.0) is 2.0
ceil(x) rounds x to smallest integer not less
than x
ceil(9.2) is 10.0
ceil(-9.2) is -9.0
floor(x) rounds x to largest integer not greater
than x
floor(9.2) is 9.0
floor(-9.2) is -10.0
pow(x,y) x raised to power y (x ) pow(2,2) is 4.0
y
case 2: printf(“Monday”);
break;
case 3: printf(“Tues day”);
break;
case 4: printf(“Wednesday”);
break;
case 5: printf(“Thursday”);
break;
case 6: printf(“Fri day”);
break;
case 7: printf(“Saturday”);
break;
default: printf(“ Invalid Day number”);
}
}
The (Ternary) (?: ) Operator
The ternary operator has three operands, the first of which is Boolean
expression,and the second and third are expressions that compute to a
value:
Looping
Loops in 'C' cause a set of statements in a program to be executed
repeatedly whilean expression is true(Repeated execution of set of
statements in a program is called looping). When the expression becomes
false, the loop terminates and the control passes to the statement
following the loop. A loop in a program therefore consists of two
segments, one known as the body of the loop and the other known as the
control statement. The control statement tests certain conditions and then
directs the repeated execution of the statements contained in the body of
the loop. There are three kinds of loops in 'C'
i) while
ii) do - while
iii) for
while statement
This is called “pre-tested” looping structure. For this statement, two
different things must be specified, the statements to be repeated and the
terminating condition. In this structure, the checking of condition is done
at the beginning. The condition must be satisfied before the execution of
the statements. The set of statements in the block is executed again and
again until the test condition is true.
If the test condition becomes false, the control is transferred out of the
block.
Syntax: while(test condition)
{
statements;
}
The execution of this statement works as follows:
1. The test condition if first evaluated.
2. If the test condition is false, while statement is terminated and the
control goes out of the structure.
3. If test condition is true, then the statements in the structure will be
executed and the control returns to the test condition.
for statement
for statement is also called the fixed execution looping structure. This
structure is used when we know exactly how many times a particular set
of statements is to be repeated.
Syntax: for(Expression1; Expression 2; Expression 3)
{
statements;
}
Where
1. Expression 1 represents the initialization expression
2. Expression 2 represents the final condition
3. Expression 3 represents the increment or decrement expression.
The execution of for loop is as follows:
1. Expression 1 is evaluated and the counter variable is assigned initial
value.
2. Expression 2 is then executed, i.e, the value of the counter variable is
checked to see whether it has exceeded the final value, if not the
statements in the structure are executed once.
3. Control is sent back to the beginning of the structure and Expression 3
is evaluated, i.e., the value of the counter variable is either increased or
decreased depending on the statement used.
4. Step 2 is repeated again and again until the counter variable exceeds
the final value.
Example: Program to display the numbers from 0 to 10 */
#include <stdio.h>
37
void main( )
{ inti;
for (i=0;i <=10;i++)
printf ("%d\n",i);
}
Nested Loops
If one looping statement is enclosed in another looping statement, then
such a sequence of statements is called as nested loops. Whenever
nested loops are used,the inner loop must be completely enclosed by the
outer loop.
Example:
for(row=1;row<=4;row++)
{
for(col=1;col<=row;col++)
printf(“ * ”);
printf(“\n”);
}
OUTPUT: *
**
***
****
Infinite Loops
If we do not use any expressions in a for statement, then the loop will run
for an infinite amount of time. The loops which do not terminate are called
as infinite loops.
Example:
for(; ;)
printf(“C Programming ”);
break Statement
The break statement is used to terminate loops. It can be used within a
while loop do while loop or for loop. When a break is encountered within a
loop, control automatically passes to the first statement after the loop.
Example:
void main()
{
int i;
for(i=0;i<=10;i++)
{
if(i==5)
break;
printf(“%d ”,i);}
}
}
Output will be 0 1 2 3 4
continue Statement
When a continue is encountered inside any loop, control automatically
passes to the beginning of the loop, by skipping rest of the looping
statements.
Example:
void main()
{ int i;
for(i=0;i<=10;i++)
{
if(i==5 || i==7)
continue;
printf(“%d ”,i);
}
}
Output will be 0 1 2 3 4 6 8 9 10
exit() Statement
• exit() is a standard library function that is used to immediately
terminate the program.
• When exit() is encountered in the program, the program is immediately
terminated and the control is transferred back to the operating system.
• The general form of an exit() function is,exit(int return_code);
where return_code is optional.
• Generally zero is used as return_code to indicate normal program
termination.
• Other nonzero value for return_code indicates the occurrence of an error
within the programs.
• The use of exit() function requires the inclusion of the header file
<stdlib.h>
*******************END OF UNIT II *******************