Professional Documents
Culture Documents
IN
‘C’
Arithmetic operator are used for mathematical
calculation these operator are binary operator that work
with integer, floating point number and double.
:
Operator Meaning
< Is less than
<= Is less than equal to
> Is greater than
>= Is greater than equal to
== Equal to
!= is not equal to
main()
{
int a=10,b=20,c=30,d,e;
d=a>b;
e=b<=c;
printf(“%d %d”,d,e);
getch();
}
Output:-
01
Are used to combine (compare) two or more condition.
Logical Operator are:-
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Types of operator:-
Logical AND compare two operands and return 1if both
condition are true else return 0 (false)
Expression
a = 8
Assignment
operator
Unary operator is also called Increments & decrement
operator. The increment operator (++) adder on to the
variable and decrement (- -) subtract one from the variable.
There are following unary operator
Operator Meaning
++x Pre increment
- -x Pre decrement
x++ Post increment
X- - Post decrement
The conditional operator is ternary operator,
which operates On the three operands.
Example:-
main()
{
int a=10,b=5,big;
big=a>b ?a:b;
printf(“Big is %d”,big);
getch();
}
Output is:-
10
Bitwise Operator:-
These operator are used for designing bit or
shifting them either right to left, left to right.
Operator Meaning
Example:-
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift
Equality operator:-
Equality operator is used for compression between two
operands these operator.
Example:-
Operator Meaning
== Equal to
!= Not equal to
Control statements
Conditional
/Decision
statements
Control Looping/
Statements Iteration
statements
Control
Transfer
statements
Conditional statements:-
Conditional statements are used to execute a set of
statements based on some condition(s).
1. If
statement
Decision
2. If else 3. Switch
statement control statement
statement
4. else if
statement
• It is used to execute a set of statements based on the condition.
-The condition must be of Boolean expression.
FALSE
Condition
TRUE
Body Statements
Immediate statement
STOP
if (conditional expression)
{
Body statements;
}
Immediate Statement;
Note:
Never place a semicolon after the expression in an if statement.
main()
{
int n;
clrscr();
printf(“enter value of n:”);
scanf(“%d”,&n);
if(n>25)
{
printf(“\nnumber is greater than 25”);
}
printf(“\n bye”); getch();
FALSE
condition
TRUE
True Block Else Block
Next statement
STOP
The IF else statement
Syntax:
if (conditional expression)
{
True Block statements;
}
else
{
False Block statements;
}
• If the conditional expression evaluates to TRUE(i.e., 1), then the true
block statements will be executed.
}
Example of Else if Statement:
main()
{
int n;
printf(“\n enter value of n:”);
scanf(“%d”,&n);
if(n>25)
printf(“\n number is greater than 25”);
else if (n>0&&n<25)
printf(“\n number is less than 25”);
else if (n<0)
printf(“\n number is negative”);
else
printf ("\n Number is Zero");
getch();
}
Syntax:
switch (variable or an integer expression)
{
case constant1:
//C Statements ;
break;
case constant2:
//C Statements ;
break;
.
.
case constantN:
//C Statements ;
break;
default:
//C Statements ;
}
Important Point for the switch case statement
•Cases doesn’t always need to have order 1, 2, 3 and so on. They can be specifyied
in any order as per the need of the program.
•You can also use characters in switch case.
•Nesting of switch statements are allowed.
•The expression provided in the switch should result in a constant value.
For example:
Valid expressions for switch –
switch(1+2+23)
switch(1*2+3%4)
switch case control flow diagram
C Program to match alphabets using switch case statement
#include <stdio.h>
int main()
{
char ch='b';
switch (ch)
{
case 'd':
printf("Case D ");
break;
case 'b':
printf("Case B");
break;
case 'c':
printf("Case C");
break;
case 'z':
printf("Case Z ");
break;
default:
printf("Default ");
}
getch();
}
• C language provides three constructs for
perfoming loop operations
• While statement
• Do statements
• For statements
While(test condition)
{
do
{
For(intialization;testcondition;icrement)
{
body of the loop
}
• Intialization of control variabledone
first using assignment statement
• The value of control variable tested
using test condition,ie reletional
expression such as i>10,that determine
when the loop will exit
• If the condition is true ,the body of
loop executed,otherwise terminated
int_sum=0;
for(int_n=1;int_n<=10;int_n++)
{
int_sum=int_sum+int_n;
}
printf(“sum=%d\n”,int_sum);
Nesting of for loop
For(i=0;i<n;i++)
{
……………………………
…For(j=0;j<n-1;j++)
{
………………………………
}
}
Jumping out of aloop
Syntax :-
switch(expression)
{
case constant : statement;
break;
default : statement;
}
main()
{
char choice;
printf(“enter any alphabet”);
scanf(“%d”,& choice);
switch(choice)
{
case ‘a’:
printf(“this is a vowel \n”);
break;
case ‘e’ :
printf(“this is a vowel \n”);
break;
case ‘i’ :
printf(“this is a vowel \n”);
break;
case ‘o’ :
printf(“this is a vowel \n”);
break;
case ‘u’ :
printf(“this is a vowel \n”);
break;
default :
printf(“this is not avowel”);
getch();
}
}
Go Tostatement
A GO TO statement can cause program control to end up anywhere in the
program unconditionally.
Example :-
main()
{
int i=1;
up : printf(“Hello ToC”)
i++;
If (i<=5)
goto up
getch();
}