Unit 2 Notes Soft
Unit 2 Notes Soft
If
If else
nested if
Switch statement
The if Statement
Syntax:
if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
Output:
if (condition)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
printf("You are eligible for voting");
else
printf("You are not eligible for voting");
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the no ");
scanf("%d",&a);
if(a%2==0)
printf("no.is even");
else
printf("no. is odd");
getch();
}
#include<stdio.h>
main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is a maximum number\n",a);
else
printf("%d is a maximum number\n",b);
getch();
}
Output:
Enter two numbers:
25
40
40 is a maximum number
3: Nested if else or else if later - this is another way of putting if’s together when multiple
decision involved. The conditions are evaluated from top to down. as soon as a true condition is
found, statement associated with it is executed and the control is transferred to the next statement
.
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
Example of nested if..else
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
Output:
Syntax:
if(condition1)
{
// code to be executed if condition1 is true
}
else if(condition2)
{
// code to be executed if condition2 is true
}
else if(condition3)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if all the conditions are false
}
Flowchart:
Example:
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 !=var2)
{
printf("var1 is not equal to var2\n");
}
else if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else if (var2 > var1)
{
printf("var2 is greater than var1\n");
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
Output:
4: Nested if or if else : if there are more than two alternatives to select then nested if statement
are used. and closing if within another if is called nested if statement.
Switch statement :
Switch statement is a multiway decision or multiple branch selection statement which test the
value of an expression Against the list of on stent integer values .
A switch statement is used where there is a choice as to which code is executed, depending on
the value of constant expression.
Different cases are presented and checked one by one to see if one case matches the value of an
constraint expression . If the case matches, it's block of code is executed . if none of the case
matches then default code is executed.
Advantages of switch:
4:easy to understand
5:easy to debug
Syntax:
switch ( integer )
{
case 1 :
{
statement ;
break;
}
case 2 :
{
statement ;
break;
}
default :
{
statement ;
break;
}
Flow Chart:
Example:
main( )
{
int i = 22 ;
switch ( i )
{
case 121 :
printf ( "I am in case 121 \n" ) ;
break ;
case 7 :
printf ( "I am in case 7 \n" ) ;
break ;
case 22 :
printf ( "I am in case 22 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
The output of this program would be:
I am in case 22
Example: WAP to display the month of the year
/* C Program to display month name according to the month number using Switch Statement */
#include<stdio.h>
int main()
{
int n;
printf("Month No : ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 3:
printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
case 6:
printf("June");
break;
case 7:
printf("July");
break;
case 8:
printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
default:
printf("Invalid Month number\nPlease try again ....\n");
break;
}
return 0;
}
2. WAP to simulate calculator.
int main() {
char operator;
int n1, n2;
switch(operator)
{
case '+':
printf("%d + %d = %d",n1, n2, n1+n2);
break;
case '-':
printf("%d + %d = %d",n1, n2, n1-n2);
break;
case '*':
printf("%d + %d = %d",n1, n2, n1*n2);
break;
case '/':
printf("%d + %d = %d",n1, n2, n1/n2);
break;
return 0;
}
Output
C programs are written using the tokens and the syntax of the language
1. Keywords
2. Identifiers
3. Constants
4. strings
5. operators
6. special symbol
Identifiers :
It refers to the name of the variables, functions and arrays created by the programmer.
It can be written in lowercase or uppercase but usually prefer lowercase .
The first character must be an alphabet or underscore. Example add().
Example : int a;
where int is a keyword and a is a identifier.
OPERATORS
Unary operator : Operator with only one operand is called unary operator . Example a =-b
Here minus is a unary operator as it has only one operand b.
Example: c= a+b
Example if a=3,b=4
C=((a>b)?a:b)
C=4
1: Arithmetic operators
2: Relational operator
3: Logical operator
4: Assignment operators
6: Conditional operator
7: Bitwise operator
8: Special operators
1. Arithmetic Operator
There are following arithmetic operators supported by C language.
2: Relational operator :
<= Left hand operand is less than or equal to right hand operand
>= Left hand operand is greater than or equal to right hand operand
3.Logical Operator
&& AND
|| OR
! NOT
They are used to combine two or more conditions/constraints or to complement the evaluation of
the original condition under consideration. They are described below:
1. Logical AND operator: The ‘&&’ operator returns true when both the conditions under
consideration are satisfied. Otherwise it returns false. For example, a && b returns true
when both a and b are true (i.e. non-zero).
2. Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions
under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if
one of a or b or both are true (i.e. non-zero). Of course, it returns true when both a and b are
true.
3. Logical NOT operator: The ‘!’ operator returns true the condition in consideration is not
satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
4. Assignment Operator :
+= The value of right hand operand is added to the value of left hand
operand. Here x=x+4
x+=4
Increments the number by 1 and then assigns the value to the left
Pre Increment ++
hand operand
Assigns the value to the left hand operand and then increments
Post Increment ++
the number by 1
Decrements the number by 1 and then assigns the value to the left
Pre Decrement --
hand operand
Assigns the value to the left hand operand and then decrements
Post Decrement --
the number by 1
Example:
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
6.Conditional Operator
Conditional Operators are represented by “?” and “:”These operators are also termed as
“ternary operators”.
(condition)? Statement1:statement2
7. Bitwise Operator
~ One’s complement
| Bitwise OR
^ Bitwise Exclusive OR
Bitwise operator works on bits and perform bit by bit operation.Assume if A = 60; and B
= 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
^ Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
(~A ) will give -60 which is 1100 0011
<< Binary Left Shift Operator. The left operands value is moved left by the
number of bits specified by the right operand.
A << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the
number of bits specified by the right operand.
A >> 2 will give 15 which is 00001111
7. Special Operator:
a) The sizeof operator is the most common operator in C. It is a compile-time unary
operator and used to compute the size of its operand. It returns the size of a variable.
It can be applied to any data type, float type, pointer type variables.
Eg main()
{ int x,y,z;
float g;
y= sizeof(x);
z= sizeof(g);
printf(“%d %d”, y,z); }
output : 2 4
b) Comma operator : comma operator is used to link the related expression together. A
comma operator are evaluated from right to left and the value of right most is the
value of combined expression.
Operator Precedence in C
Operator precedence determines which operator is evaluated first when an expression has more
than one operators. For example 100-2*30 would yield 40, because it is evaluated as 100 –
(2*30) and not (100-2)*30. The reason is that multiplication * has higher precedence than
subtraction(-).
Associativity in C
Associativity is used when there are two or more operators of same precedence is present in an
expression. For example multiplication and division arithmetic operators have same precedence,
lets say we have an expression 5*2/10, this expression would be evaluated as (5*2)/10 because
the associativity is left to right for these operators. Similarly 20/2*5 would be calculated as
(20*2)/5.
() 1
Operator
Primary
Left to
Right
[] 1
. 1
-> 2
s
i++ 2
i-- 2
* 2
& 2
+ 2
- 2
Unary operators
Right to Left
! 2
~ 2
++i 2
--i 2
(type) 2
sizeof 2
* 3
/ 3
% 3
+ 4
Arithmetic Operators
- 4
Left to Right
<< 5
>> 5
< 6
> 6
<= 6
Relational Operator
>= 6
== 7
!= 7
& 8
^ 9
Operator
Bitwise
| 10
Logical && 11
Operator
|| 12
Ternary ?: 13
operator
= 14
+= 14
-= 14
Right to Left
Assignment Operator
*= 14
/= 14
%= 14
<<= 14
>>= 14
&= 14
^= 14
|= 14
Sequence , 15
operator
Type conversion :
We may often want to change the data type of the variable. When we declare some
variable as int, the desired output may be float or vice-versa. In such situation, we
change the nature of the data stored in the variable . This process is known as data
type conversion or type casting. The general form of type casting is (data
type)variable
There are two types of type conversion:
1. Implict conversion : It does not require any operator. They are automatically
perform when a value is copied to a compatiable type .It happens when the type
of the variable on LHS of the assignment operator does not match with the
expression on RHS.
In such situation value of expression is demoted or promoted depending
of the type of the variable on LHS of is equal to (assignment operator).
Eg
main()
{
int i;
float j;
i=3.4;
j=4;
}
So the value of I will be 3 and j will be 4.0.
2. Explicit Typecasting : If we are required to force the user to explicitly convert
the value of an expression to a datatype, then this conversion is called explicit
conversion.
In this method, we use the operator. This consist of putting a pair of parenthesis
around the name of the datatype.
Eg :
main()
{
int x=3, y=2;
float z;
z = (float)x/y;
printf(“%f”, z);
}
So the value of z will be 1.5.