0% found this document useful (0 votes)
19 views26 pages

Unit 2 Notes Soft

Uploaded by

sanyamkumar75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views26 pages

Unit 2 Notes Soft

Uploaded by

sanyamkumar75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit 2

Conditional Control Statements

Conditional Control Statement : In some Situation, it is necessary to check the condition to


make the decision which involves performing a logical test. This test result in either true or false.

Depending on the truthiness or falsity of the condition ,statement to be executed is determined.


This involves both decision making an branching.

Following are the conditional control statements :

If

If else

nested if

nested if else or elseif ladder

Switch statement

1: if - if the statement is used to execute a statement or set of statement conditionally.

The if Statement

Syntax:

if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}

Flow Diagram of if statement


Example of if statement

#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
Output:

Variable x is less than y


2: if else - if statement is used to execute one action. If there are two statements to be executed
then if else statement is used . It is also called two way branching .

if (condition)
{
True block of statements
}
Else
{
False block of statements
}
Statements;

1. WAP to print person is eligible for voting or not.

#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;
}

2. WAP to check given no is even and odd.

#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();
}

3. WAP to find greatest between two numbers.

#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

4. WAP to check whether two numbers are equal or not.


include<stdio.h>
main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
if(a==b)
printf("both nos are equal”);
else
printf (“both nos are not equal”);
getch();
}

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:

Input the value of var1:12


Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1
C – else..if statement

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:

Input the value of var1:12


Input the value of var2:21
var1 is not equal to var2

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:

1: switch statement is very useful while writing menu driven programs .


2:multiple statement does not Need to enclosed in a curly braces .

3: switch work faster than if else as compiler generate jump table

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.

// Program to create a simple calculator


#include <stdio.h>

int main() {
char operator;
int n1, n2;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%d %d",&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;

// operator doesn't match any case constant +, -, *, /


default:
printf("Error! operator is not correct");
}

return 0;
}

Output

Enter an operator (+, -, *,): -


Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
C Tokens

In a C program, smallest individual unit are known as C Tokens.

C programs are written using the tokens and the syntax of the language

C has six types of Tokens .

1. Keywords
2. Identifiers
3. Constants
4. strings
5. operators
6. special symbol

Keywords and Identifier:

Keywords : In C there are certain reserved characters called keywords.


They have predefined meaning.
A keyword is a reserved word of C whose meaning has already been explained to the C
compiler.
All C keyword must be written in lowercase letters .Example void, if, else, int etc.

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

Operators are the tools that manipulate the data .


It is a symbol which represents some particular operation to be performed on a data
value.
Operator are classified into three categories:
1. Unary operator
2. Binary operator
3. Ternary operator

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.

Binary operator : Operator with two operands is called binary operator.

Example: c= a+b

Here +is a binary operator as it is operating on two operands a and b.

Ternary operator : Operator with three operands is called ternary operator .

Syntax: (Condition ?Statement 1:Statement 2 )

Example if a=3,b=4

C=((a>b)?a:b)

C=4

here ? and : are ternary operator as it has three operand.

Operators are classified into several categories :

1: Arithmetic operators

2: Relational operator

3: Logical operator

4: Assignment operators

5: Increment decrement operators

6: Conditional operator

7: Bitwise operator

8: Special operators

a) comma operator b)sizeof() operator.

1. Arithmetic Operator
There are following arithmetic operators supported by C language.

Airthmetic Operator Function

+ Perform addition of two or more numbers.


- Perform subtraction of two or more numbers

* Perform multiplication of two or more numbers

/ Perform division of two or more numbers

Calculate the remainder which is obtained by dividing two or


%
more integers

Example: Assume variable A holds 10 and variable B holds 20 then:


Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by denumerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0

2: Relational operator :

Relational Operator Function

< Left hand operand is less than right hand operand

<= Left hand operand is less than or equal to right hand operand

> Left hand operand is greater than right hand operand

>= Left hand operand is greater than or equal to right hand operand

== Left hand operand is equal to right hand operand

!= Left hand operand is not equal to right hand operand


Relational operators are used for comparison of two values to understand the type of relationship
a pair of number shares. For example, less than, greater than, equal to etc. Let’s see them one by
one
1. Equal to operator: Represented as ‘==’, the equal to operator checks whether the two
given operands are equal or not. If so, it returns true. Otherwise it returns false. For
example, 5==5 will return true.
2. Not equal to operator: Represented as ‘!=’, the not equal to operator checks whether the
two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is
the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
3. Greater than operator: Represented as ‘>’, the greater than operator checks whether the
first operand is greater than the second operand or not. If so, it returns true. Otherwise it
returns false. For example, 6>5 will return true.
4. Less than operator: Represented as ‘<‘, the less than operator checks whether the first
operand is lesser than the second operand. If so, it returns true. Otherwise it returns false.
For example, 6<5 will return false.
5. Greater than or equal to operator: Represented as ‘>=’, the greater than or equal to
operator checks whether the first operand is greater than or equal to the second operand. If
so, it returns true else it returns false. For example, 5>=5 will return true.
6. Less than or equal to operator: Represented as ‘<=’, the less than or equal to operator
checks whether the first operand is less than or equal to the second operand. If so, it returns
true else false. For example, 5<=5 will also return true.

3.Logical Operator

Logical Operator Function

&& 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 :

Assignment Operator Function

= Assign the value of right hand operator to left hand operand

+= The value of right hand operand is added to the value of left hand
operand. Here x=x+4
x+=4

-= The value of right hand operand is subtracted to the value of left


hand operand. Here x=x-4
x-=4

*= The value of right hand operand is multiplied to the value of left


hand operand. Here x=x*4
X*=4

/= The value of right hand operand is divided to the value of left


hand operand. Here x=x/4
x/=4

%= The value of remainder obtained after dividing two integer values


is stored in left hand operand. Here x=x%4
x%=4

5. Increment decrement operators :

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:

// Working of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

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

If the condition is true then statement1 is executed otherwise statement2 is executed.


Ex. If a=5 and b=-3 and c=2
(a>b?b=1:(b=3))
b will be 1.

7. Bitwise Operator

Bitwise Operator Function

~ One’s complement

>> Right Shift

<< Left shift

& Bitwise AND

| 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

A|B = 0011 1101

A^B = 0011 0001


~A = 1100 0011

There are following Bitwise operators supported by C language Operator Description


Example
& Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) will give 12 which is 0000 1100

| Binary OR Operator copies a bit if it exists in either operand.


(A | B) will give 61 which is 0011 1101

^ 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.

Example c=(a=20, b=30, a+b)


C=50

Precedence and Associativity of Operators

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.

Type Operato Priority Associativity


r

() 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy