Psuc Unit II
Psuc Unit II
C Tokens -
Identifiers, Keywords, Constants, Variables and Operators
Expressions –
Arithmetic expressions, Precedence and Associativity, evaluating
Expressions
Decision control structures –
if, Two-way selection- if else, nested if, dangling else, Multi way
selection- else if ladder and switch
Repetitive control structures –
Pre-test and post-test loops – intialization and updation, do while,
while and for loop and nested loops.
Unconditional statements –
break, continue and goto statements with example
Definition- variable which stores the value in the memory location, that value varies
during program execution
variables in the above program are a b and c .
Percent Legal
y2x5__fg7h Legal
annual_profit Legal
_1990_tax Legal but not advised
savings#account Illegal: Contains the illegal character #
double Illegal: Is a C keyword
9winter Illegal: First character is a digit
int a;
Variable declaration and definition
Variable name
a
23456 Garbage value
1000
Address of the variable
K.Vigneswara Reddy, Dept.of I.T, SNIST
Variable initialization
• While declaring the variable assign
initial value is called variable
initialization.
• Data type identifier= initial value;
Mantissa e exponent
double 0. 0.0
double 0.0 .0
Defined constants:
By using the preprocessor command you can create a
constant.
Example: #define pi 3.14
Memory constants:
Memory constants use a C type qualifier, const, to indicate
that the data can not be changed.
Its format is: const type identifier = value;
Example: const float PI = 3.14159;
Arithmetic Meaning
operators c= a+b;
+ Addition or unary operator z= x-y;
- Subtraction or unary
operator
* Multiplication
Relational Meaning
operators
< Is less than The value of a relational
Expression is either one or
> Is greater than zero. It is one if the
Specified relation is true and
<= Less than or equal to zero if the relation is false .
Logical meaning
operator
&& Logical AND
variable = expression;
• Increment operator- ++
• Decrement operator - --
• ++- add one(1) value to the operand
• -- - subtracts one value to the operand.
Bitwise Meaning
Operators
& Bitwise AND
| Bitwise OR
^ Exclusive OR
<< Shift left
>> Shift right
~ One‘s compliment
K.Vigneswara Reddy, Dept.of I.T, SNIST
Examples:
& Bitwise AND 0110 & 0011 0010
| Bitwise OR 0110 | 0011 0111
^ Bitwise XOR 0110 ^ 0011 0101
~ One's complement ~0011 1100
Don't confuse bitwise & | with logical && ||
ex- a++; a- -;
expression
ex- ++a;
1
Operand must be integral data type( int, float)
Ex- a+b
a-c
void main()
{
int i=1;
printf(“ i= %d\t i=%d”,++i,i++);
}
Output
i=3 i=1
Example: int x;
x=(int)7.5;
Types of Statements
K.Vigneswara Reddy, Dept.of I.T, SNIST
Compound statements are used to group the statements
into a single executable unit.
It consists of one or more individual statements
enclosed within the braces { }
Compound Statement
K.Vigneswara Reddy, Dept.of I.T, SNIST
Decision Control structures
Control statements embody the decision logic that tells the
executing program what action to be carried out depending on
the values of certain variable or expressions.
Control statements includes selection, iteration and jump
statements
Selection or conditional statement
- allows us to decide which statement to execute next.
- decision is based on the condition or test expression that
evaluates to either true or false.( 0 or 1)
- the resultant of the expression determines which statement is
executed next.
Enter
if ( condition )
statement;
Test
If the condition is true, the
statement is executed.
If it is false, the statement is
Body of the IF Statement
skipped.
Exit
if (condition\expression){
The expression evaluated to true,
Statement1}
statement1 is executed.
else {
If false, statement2 is executed.
statement2 }
Both cases statement-x is executed.
statement-x
K.Vigneswara Reddy, Dept.of I.T, SNIST
The if-else Statement
An else clause can be added to an if statement to make an if-else
statement
Enter
Flowchart for if-else
Test
Exit
If the condition is true, statement1 is executed; if the condition is
false, statement2 is executed
One or the other will be executed, but not both
if( x>0)
if( x>0)
{
if(a>b)
if(a>b)
z=a;
z=a;
else
}
z=b;
else
z=b;
K.Vigneswara Reddy, Dept.of I.T, SNIST
Nested if else- within the if else we can
include other if-else either in if block or
else block
F T
F T
Dangling else
K.Vigneswara Reddy, Dept.of I.T, SNIST
Dangling else (contd…)
To avoid dangling else problem place the inner if statement
with in the curly braces.
Post-test Loop
In each iteration, the loop action(s) are executed.
Then the control expression is tested. If it is true, a
new iteration is started; otherwise, the loop
terminates.
Loop Initialization
Loop Update
3. for statement
while is a
Key word If the condition is true, the
statement is executed.
while ( condition ) Then the condition is
statement; evaluated again.
float a=1.0;
while(a<=1.5)
{
printf(―%f‖,a);
a=a+0.1;}
Syntax
do {
statement;
statement;
} while (condition);
statement;
#include <stdio.h>
void main()
{
int i = 1, n=5;
do
{
printf(― %d * %d = %d ―, n, i, n*i);
i = i + 1;
} while ( i<= 5);
}
Examples
Vary the control variable from 1 to 100 in increments of 1
for (i = 1; i <= 100; i++)
Vary the control variable from 100 to 1 in decrements of -1
for (i = 100; i >= 1; i--)
Vary the control variable from 5 to 55 in increments of 5
for (i = 5; i <= 55; i+=5)
K.Vigneswara Reddy, Dept.of I.T, SNIST
Write a c program to find the sum of the first n natural
While example numbers
do-while example For example
#include <stdio.h> #include <stdio.h> #include <stdio.h>
void main(void) void main(void) void main(void)
{ { {
int n,i = 1, sum = 0; int n,i = 1, sum = 0; int n,i,sum = 0;
printf(“\nenter n value”); printf(“\nenter n value”); printf(“\nenter n value”);
scanf(“%d”,&n); scanf(“%d”,&n); scanf(“%d”,&n);
while ( i<= n) { do{ for(i=1;i<=n;i++)
sum = sum + i; sum = sum + i; sum=sum+i;
i = i + 1; i = i + 1; printf(“Sum of %d natural nos=
}//while } while ( i<= n) ; %d\n”,n, sum
printf(“Sum of %d natural printf(“Sum of %d natural nos=
nos= %d\n”,n, sum); %d\n”,n, sum);
getch(); getch();
}//main }//main
General form
Most compilers
for (loop1_exprs)
allow 15
{
nesting levels –
loop_body_1
DON‘T DO IT!!
for (loop2_exprs)// inner for loop
{
loop_body_2
}// inner loop
loop_body_1
}//outer loop K.Vigneswara Reddy, Dept.of I.T, SNIST
Nested for loops
int i,j,sum;
for(i=1,i<=3;i++)
{
for(j=0;j<2;j++)
{
sum=i+j;
printf(―i=%d,j=%d,sum=%d‖,i,j,sum);
}//inner for loop
}//outer for loop
#include<stdio.h>
void main()
{
int num,i;
printf(―\n enter the number‖);
scanf(―%d‖,&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf(―not a prime number‖);
break;
}//if
i++;
}//while
if(i= = num)
printf(―\n it‘s a prime number‖);
}//main
Algorithm
1. read n
2. while(n>0)
2.1 y=n%10
2.2 n=n/10
2.3 sum=sum+y
3. print sum