SlideShare une entreprise Scribd logo
1  sur  15
BY REHAN IJAZ
07 - Basic C Operators
Operator
Operators are symbolswhichtake one ormore operandsor expressionsandperformarithmeticorlogical
computations.
Types ofoperators available inC are as follows:
1. Arithmeticoperators
2. Assignmentoperators
3. Equalitiesand relational operators
4. Logical/relational
5. Conditional operators
1. Arithmeticoperators
Arithmeticoperators take numerical valuesastheiroperandsandreturna single numerical value.Assume
variable A = 10 and variable B= 20
Operator Description Example
+ Addstwooperands. A + B = 30
− Subtractssecondoperandfromthe first. A − B = 10
∗ Multipliesbothoperands. A ∗ B = 200
∕ Dividesnumeratorbyde-numerator. B ∕ A = 2
% ModulusOperatorand remainderof afteranintegerdivision. B % A = 0
++ Incrementoperatorincreasesthe integervaluebyone. A++ = 11
-- Decrementoperatordecreasesthe integervalue byone. A-- = 9
Example
Try the following example to understand all the arithmetic operators available in C −
#include <stdio.h>
main()
{
int a = 21, int b = 10 , int c ;
c = a + b;
printf("Line 1 - Value of c is %dn", c );
c = a - b;
printf("Line 2 - Value of c is %dn", c );
c = a * b;
BY REHAN IJAZ
printf("Line 3 - Value of c is %dn", c );
c = a / b;
printf("Line 4 - Value of c is %dn", c );
c = a % b;
printf("Line 5 - Value of c is %dn", c );
c = a++;
printf("Line 6 - Value of c is %dn", c );
c = a--;
printf("Line 7 - Value of c is %dn", c );
}
Output
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
2. Assignmentoperators
The followingtable liststhe assignmentoperatorssupportedbythe Clanguage
Operator Description Example
= Simple assignmentoperator.Assignsvaluesfromright
side operandstoleftside operand
C = A + B will assignthe value of A + B
to C
+= AddAND assignmentoperator.Itaddsthe rightoperand
to the leftoperandand assignthe resulttothe left
operand.
C += A isequivalenttoC = C + A
-= Subtract ANDassignmentoperator.Itsubtractsthe right
operandfromthe leftoperandandassignsthe resultto
the leftoperand.
C -= A isequivalenttoC= C - A
*= Multiply ANDassignmentoperator.Itmultipliesthe right
operandwiththe leftoperandandassignsthe resultto
the leftoperand.
C *= A isequivalenttoC = C * A
/= Divide ANDassignmentoperator.Itdividesthe left
operandwiththe rightoperandandassigns the resultto
the leftoperand.
C /= A is equivalenttoC= C / A
%= ModulusANDassignmentoperator.Ittakesmodulus
usingtwooperandsandassignsthe resultto the left
operand.
C %= A isequivalenttoC= C % A
<<= LeftshiftANDassignment operator. C <<= 2 is same as C = C << 2
>>= RightshiftANDassignmentoperator. C >>= 2 is same as C = C >> 2
&= Bitwise ANDassignmentoperator. C &= 2 issame as C = C & 2
^= Bitwise exclusive ORandassignmentoperator. C ^= 2 issame as C = C ^ 2
BY REHAN IJAZ
|= Bitwise inclusiveORandassignmentoperator. C |= 2 is same as C = C | 2
Example
#include <stdio.h>
main() {
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %dn", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %dn", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %dn", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %dn", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %dn", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %dn", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %dn", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %dn", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %dn", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %dn", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %dn", c );
}
Output
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2
BY REHAN IJAZ
3. Equalitiesandrelational operators
The binary relational and equality operators compare their first operand to their second operand to test the
validity of the specified relationship. The result of a relational expression is 1 if the tested relationship is true
and 0 if it is false. The type of the result is int.
The relational and equality operators test the following relationships:
Operator Description
< First operand less than second operand
> First operand greater than second operand
<= First operand less than or equal to second operand
>= First operand greater than or equal to second operand
== First operand equal to second operand
!= First operand not equal to second operand
Example
Try the following example to understand all the relational operators available in C −
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
printf("Line 1 - a is equal to bn" );
}
else {
printf("Line 1 - a is not equal to bn" );
}
if ( a < b ) {
printf("Line 2 - a is less than bn" );
}
else {
printf("Line 2 - a is not less than bn" );
}
if ( a > b ) {
printf("Line 3 - a is greater than bn" );
}
else {
printf("Line 3 - a is not greater than bn" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b ) {
printf("Line 4 - a is either less than or equal to bn" );
}
if ( b >= a ) {
printf("Line 5 - b is either greater than or equal to bn" );
BY REHAN IJAZ
}
}
When you compile and execute the above program, it produces the following result −
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b
4. Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then −
Operator Description Example
&& CalledLogical ANDoperator.If boththe operands are non-zero,
thenthe conditionbecomestrue.
(A && B) is
false.
|| CalledLogical OROperator.If any of the twooperandsisnon-
zero,thenthe conditionbecomestrue.
(A || B) is
true.
! CalledLogical NOTOperator.Itis usedtoreverse the logical
state of itsoperand.If a conditionistrue,thenLogical NOT
operatorwill make itfalse.
Example
#include <stdio.h>
main() {
int a = 5, int b = 20, int c ;
if ( a && b ) {
printf("Line 1 - Condition is truen" );
}
if ( a || b ) {
printf("Line 2 - Condition is truen" );
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is truen" );
}
else {
printf("Line 3 - Condition is not truen" );
}
if ( !(a && b) ) {
printf("Line 4 - Condition is truen" );
}
BY REHAN IJAZ
}
Output
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
5. Conditional Operators
Syntax : expression1 ? expression2 : expression3
1. Expression1isnothingbutBooleanConditioni.eitresultsintoeither TRUEor FALSE
2. If resultof expression1isTRUE thenexpression2isExecuted
3. Expression1issaidtobe TRUE if itsresultisNON-ZERO
4. If resultof expression1isFALSEthenexpression3isExecuted
5. Expression1issaidtobe FALSEif its resultisZERO
Example : Even- Odd
#include<stdio.h>
int main()
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even"):printf("Odd");
}
BY REHAN IJAZ
C - Type Casting
Type casting is a way to convert a variable from one data type to another data type. For example, if you
want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the
values from one type to another explicitly using the cast operator as follows –
Syntax: (type_name) expression
Example 1
#include <stdio.h>
main() {
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %fn", mean );
}
Output
Value of mean : 3.400000
It should be noted here that the cast operator has precedence over division, so the value of sum is first
converted to type double and finally it gets divided by count yielding a double value.
Example 2
#include <stdio.h>
main() {
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;
sum = i + c;
printf("Value of sum : %dn", sum );
}
Output
Value of sum : 116
Example 3
#include <stdio.h>
main() {
int i = 17;
char c = 'c'; /* ascii value is 99 */
float sum;
sum = i + c;
printf("Value of sum : %fn", sum );
}
Output
Value of sum : 116.000000
BY REHAN IJAZ
DecisionControl structure using if
Decisionmakingstructuresrequire thatthe programmerspecifiesone or more conditions to be evaluated
or tested by the program, along with a statement or statements to be executed if the condition is
determinedtobe true,and optionally, other statements to be executed if the condition is determined to
be false.
C - if statement
The if statement allows you to control if a program enters a section of code or not based on whether a
given condition is true or false. One of the important functions of the if statement is that it allows the
program to select an action based upon the user's input. For example, by using an if statement to check a
user-entered password, your program can decide whether a user is allowed access to the program.
Example
#include <stdio.h>
int main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20n" );
}
printf("value of a is : %dn", a);
return 0;
}
Output
a is less than 20;
value of a is : 10
BY REHAN IJAZ
C - nested if statements
#include <stdio.h>
int main () {
int a = 100;
int b = 200;
if( a == 100 ) {
if( b == 200 ) {
printf("Value of a is 100 and b is 200n" );
}
}
printf("Exact value of a is : %dn", a );
printf("Exact value of b is : %dn", b );
return 0;
}
Output
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
C - if...else statement
An if statementcanbe followedbyanoptional elsestatement,whichexecuteswhenthe Boolean
expressionisfalse.
Example
#include <stdio.h>
int main ()
{
int a = 100;
if( a < 20 )
{
printf("a is less than 20n" );
}
else
BY REHAN IJAZ
{
printf("a is not less than 20n" );
}
printf("value of a is : %dn", a);
return 0;
}
Output
a is not less than 20;
value of a is : 100
C nested if else statement
The nested if...else statement has more than one test expression. If the first test expression is true, it
executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the
secondtestexpression.If the secondtestexpressionistrue,itexecutesthe statement/sinside the braces{ }
justbelowit.Thisprocesscontinues.If all the test expression are false, code/s inside else is executed and
the control of program jumps below the nested if...else
The ANSI standard specifies that 15 levels of nesting may be continued.
Example
#i nclude <stdio.h>
i nt mai n(){
i nt numb1, numb2;
pri ntf("Enter two i ntegers to checkn");
scanf("%d %d",&numb1,&numb2);
i f(numb1==numb2)
pri ntf("Result: %d = %d",numb1,numb2);
el se
i f(numb1>numb2)
pri ntf("Result: %d > %d",numb1,numb2);
el se
pri ntf("Result: %d > %d",numb2,numb1);
return 0;
}
Output
Enter two integers to check.
5
3
Result: 5 > 3
BY REHAN IJAZ
If...else if...else Statement
An if statementcanbe followed byanoptional elseif...elsestatement,whichisveryuseful totestvarious
conditionsusingsingle if...else if statement.
Syntax
if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
}
else {
/* executes when the none of the above condition is true */
}
Example
#include <stdio.h>
int main () {
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10n" );
}
else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20n" );
}
else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30n" );
}
else {
/* if none of the conditions is true */
printf("None of the values is matchingn" );
}
printf("Exact value of a is: %dn", a );
return 0;
}
Output
None of the values is matching
Exact value of a is: 100
BY REHAN IJAZ
C - switchstatement
A switch statementallowsavariable tobe testedforequality against a list of values. Each value is called a
case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows −
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
Example
#include <stdio.h>
int main () {
/* local variable definition */
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!n" );
break;
case 'B' :
case 'C' :
printf("Well donen" );
BY REHAN IJAZ
break;
case 'D' :
printf("You passedn" );
break;
case 'F' :
printf("Better try againn" );
break;
default :
printf("Invalid graden" );
}
printf("Your grade is %cn", grade );
return 0;
}
Output
Well done
Your grade is B
Benefits of Switch Statement
In some languages and programming environments, the use of a switch-case statement is considered
superior to an equivalent series of if else if statements because it is:
 Easier to debug
 Easier to read
 Easier to understand, and therefore
 Easier to maintain
 Fixed depth: a sequence of "if else if" statements yields deep nesting, making compilation more
difficult
 Faster execution potential
Limitations of Switch Statement
Switch statement is a powerful statement used to handle many alternatives and provides good
presentation for C program. But there are some limitations with switch statement which are given below:
 Logical operators cannot be used with switch statement. For example case k>= 20; is not
allowed.
 Switch case variables can have only int and char data type. So float or no data type is allowed.
BY REHAN IJAZ
break statement in C
The break statement in C programming has the following two usages −
 When a break statement is encountered inside a loop, the loop is immediately terminated and the
program control resumes at the next statement following the loop.
 It can be used to terminate a case in the switch statement (covered in the next chapter).
If you are using nested loops, the break statement will stop the execution of the innermost loop and start
executing the next line of code after the block.
Syntax
break;
Example
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 ) {
printf("value of a: %dn", a);
a++;
if( a > 15) {
/* terminate the loop using break statement */
break;
}
}
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
BY REHAN IJAZ
value of a: 14
value of a: 15

Contenu connexe

Tendances

Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailgourav kottawar
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++Praveen M Jigajinni
 
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operatorsdishti7
 
C operators
C operatorsC operators
C operatorsGPERI
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C languageAbdul Rehman
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Himanshu Kaushik
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators pptViraj Shah
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++Pranav Ghildiyal
 
Conditional operators
Conditional operatorsConditional operators
Conditional operatorsBU
 
Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expressionshubham_jangid
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++zeeshan turi
 

Tendances (19)

Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
C operators
C operatorsC operators
C operators
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 
Operators
OperatorsOperators
Operators
 
Operator of C language
Operator of C languageOperator of C language
Operator of C language
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expression
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 

En vedette

How to make presentation effective assignment
How to make presentation effective assignmentHow to make presentation effective assignment
How to make presentation effective assignmentREHAN IJAZ
 
Trevel presentation
Trevel presentationTrevel presentation
Trevel presentationindra Kishor
 
importance of Communication in business
importance of Communication in businessimportance of Communication in business
importance of Communication in businessREHAN IJAZ
 
Programming Fundamentals lecture 3
Programming Fundamentals lecture 3Programming Fundamentals lecture 3
Programming Fundamentals lecture 3REHAN IJAZ
 
Career development interviews
Career development interviewsCareer development interviews
Career development interviewsREHAN IJAZ
 
Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6REHAN IJAZ
 
Programming Fundamentals lecture 4
Programming Fundamentals lecture 4Programming Fundamentals lecture 4
Programming Fundamentals lecture 4REHAN IJAZ
 
Cmp2412 programming principles
Cmp2412 programming principlesCmp2412 programming principles
Cmp2412 programming principlesNIKANOR THOMAS
 
Project code for Project on Student information management system
Project code for Project on Student information management systemProject code for Project on Student information management system
Project code for Project on Student information management systemREHAN IJAZ
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1REHAN IJAZ
 
Everything about Object Oriented Programming
Everything about Object Oriented ProgrammingEverything about Object Oriented Programming
Everything about Object Oriented ProgrammingAbdul Rahman Sherzad
 
Trevel presentation
Trevel presentationTrevel presentation
Trevel presentationindra Kishor
 
General Programming Concept
General Programming ConceptGeneral Programming Concept
General Programming ConceptHaris Bin Zahid
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Pritom Chaki
 
Introduction to artificial intelligence lecture 1
Introduction to artificial intelligence lecture 1Introduction to artificial intelligence lecture 1
Introduction to artificial intelligence lecture 1REHAN IJAZ
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentEduardo Bergavera
 
Porposal on Student information management system
Porposal on Student information management systemPorposal on Student information management system
Porposal on Student information management systemREHAN IJAZ
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Michael Redlich
 
Introduction to programimg
Introduction to programimgIntroduction to programimg
Introduction to programimgStn PT
 

En vedette (20)

How to make presentation effective assignment
How to make presentation effective assignmentHow to make presentation effective assignment
How to make presentation effective assignment
 
Trevel presentation
Trevel presentationTrevel presentation
Trevel presentation
 
importance of Communication in business
importance of Communication in businessimportance of Communication in business
importance of Communication in business
 
Programming Fundamentals lecture 3
Programming Fundamentals lecture 3Programming Fundamentals lecture 3
Programming Fundamentals lecture 3
 
Career development interviews
Career development interviewsCareer development interviews
Career development interviews
 
Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6
 
Programming Fundamentals lecture 4
Programming Fundamentals lecture 4Programming Fundamentals lecture 4
Programming Fundamentals lecture 4
 
Cmp2412 programming principles
Cmp2412 programming principlesCmp2412 programming principles
Cmp2412 programming principles
 
Project code for Project on Student information management system
Project code for Project on Student information management systemProject code for Project on Student information management system
Project code for Project on Student information management system
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1
 
Everything about Object Oriented Programming
Everything about Object Oriented ProgrammingEverything about Object Oriented Programming
Everything about Object Oriented Programming
 
Trevel presentation
Trevel presentationTrevel presentation
Trevel presentation
 
General Programming Concept
General Programming ConceptGeneral Programming Concept
General Programming Concept
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \
 
Introduction to artificial intelligence lecture 1
Introduction to artificial intelligence lecture 1Introduction to artificial intelligence lecture 1
Introduction to artificial intelligence lecture 1
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software Development
 
Porposal on Student information management system
Porposal on Student information management systemPorposal on Student information management system
Porposal on Student information management system
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
 
Introduction to programimg
Introduction to programimgIntroduction to programimg
Introduction to programimg
 

Similaire à Programming Fundamentals lecture 7

C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsAmrinder Sidhu
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptxramanathan2006
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxNithya K
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression Ashim Lamichhane
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdSyed Mustafa
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CSyed Mustafa
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 

Similaire à Programming Fundamentals lecture 7 (20)

Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
Operators
OperatorsOperators
Operators
 
C operators
C operatorsC operators
C operators
 
cprogrammingoperator.ppt
cprogrammingoperator.pptcprogrammingoperator.ppt
cprogrammingoperator.ppt
 
C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Operators in c by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
 
C sharp chap3
C sharp chap3C sharp chap3
C sharp chap3
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 

Dernier

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Dernier (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 

Programming Fundamentals lecture 7

  • 1. BY REHAN IJAZ 07 - Basic C Operators Operator Operators are symbolswhichtake one ormore operandsor expressionsandperformarithmeticorlogical computations. Types ofoperators available inC are as follows: 1. Arithmeticoperators 2. Assignmentoperators 3. Equalitiesand relational operators 4. Logical/relational 5. Conditional operators 1. Arithmeticoperators Arithmeticoperators take numerical valuesastheiroperandsandreturna single numerical value.Assume variable A = 10 and variable B= 20 Operator Description Example + Addstwooperands. A + B = 30 − Subtractssecondoperandfromthe first. A − B = 10 ∗ Multipliesbothoperands. A ∗ B = 200 ∕ Dividesnumeratorbyde-numerator. B ∕ A = 2 % ModulusOperatorand remainderof afteranintegerdivision. B % A = 0 ++ Incrementoperatorincreasesthe integervaluebyone. A++ = 11 -- Decrementoperatordecreasesthe integervalue byone. A-- = 9 Example Try the following example to understand all the arithmetic operators available in C − #include <stdio.h> main() { int a = 21, int b = 10 , int c ; c = a + b; printf("Line 1 - Value of c is %dn", c ); c = a - b; printf("Line 2 - Value of c is %dn", c ); c = a * b;
  • 2. BY REHAN IJAZ printf("Line 3 - Value of c is %dn", c ); c = a / b; printf("Line 4 - Value of c is %dn", c ); c = a % b; printf("Line 5 - Value of c is %dn", c ); c = a++; printf("Line 6 - Value of c is %dn", c ); c = a--; printf("Line 7 - Value of c is %dn", c ); } Output Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22 2. Assignmentoperators The followingtable liststhe assignmentoperatorssupportedbythe Clanguage Operator Description Example = Simple assignmentoperator.Assignsvaluesfromright side operandstoleftside operand C = A + B will assignthe value of A + B to C += AddAND assignmentoperator.Itaddsthe rightoperand to the leftoperandand assignthe resulttothe left operand. C += A isequivalenttoC = C + A -= Subtract ANDassignmentoperator.Itsubtractsthe right operandfromthe leftoperandandassignsthe resultto the leftoperand. C -= A isequivalenttoC= C - A *= Multiply ANDassignmentoperator.Itmultipliesthe right operandwiththe leftoperandandassignsthe resultto the leftoperand. C *= A isequivalenttoC = C * A /= Divide ANDassignmentoperator.Itdividesthe left operandwiththe rightoperandandassigns the resultto the leftoperand. C /= A is equivalenttoC= C / A %= ModulusANDassignmentoperator.Ittakesmodulus usingtwooperandsandassignsthe resultto the left operand. C %= A isequivalenttoC= C % A <<= LeftshiftANDassignment operator. C <<= 2 is same as C = C << 2 >>= RightshiftANDassignmentoperator. C >>= 2 is same as C = C >> 2 &= Bitwise ANDassignmentoperator. C &= 2 issame as C = C & 2 ^= Bitwise exclusive ORandassignmentoperator. C ^= 2 issame as C = C ^ 2
  • 3. BY REHAN IJAZ |= Bitwise inclusiveORandassignmentoperator. C |= 2 is same as C = C | 2 Example #include <stdio.h> main() { int a = 21; int c ; c = a; printf("Line 1 - = Operator Example, Value of c = %dn", c ); c += a; printf("Line 2 - += Operator Example, Value of c = %dn", c ); c -= a; printf("Line 3 - -= Operator Example, Value of c = %dn", c ); c *= a; printf("Line 4 - *= Operator Example, Value of c = %dn", c ); c /= a; printf("Line 5 - /= Operator Example, Value of c = %dn", c ); c = 200; c %= a; printf("Line 6 - %= Operator Example, Value of c = %dn", c ); c <<= 2; printf("Line 7 - <<= Operator Example, Value of c = %dn", c ); c >>= 2; printf("Line 8 - >>= Operator Example, Value of c = %dn", c ); c &= 2; printf("Line 9 - &= Operator Example, Value of c = %dn", c ); c ^= 2; printf("Line 10 - ^= Operator Example, Value of c = %dn", c ); c |= 2; printf("Line 11 - |= Operator Example, Value of c = %dn", c ); } Output Line 1 - = Operator Example, Value of c = 21 Line 2 - += Operator Example, Value of c = 42 Line 3 - -= Operator Example, Value of c = 21 Line 4 - *= Operator Example, Value of c = 441 Line 5 - /= Operator Example, Value of c = 21 Line 6 - %= Operator Example, Value of c = 11 Line 7 - <<= Operator Example, Value of c = 44 Line 8 - >>= Operator Example, Value of c = 11 Line 9 - &= Operator Example, Value of c = 2 Line 10 - ^= Operator Example, Value of c = 0 Line 11 - |= Operator Example, Value of c = 2
  • 4. BY REHAN IJAZ 3. Equalitiesandrelational operators The binary relational and equality operators compare their first operand to their second operand to test the validity of the specified relationship. The result of a relational expression is 1 if the tested relationship is true and 0 if it is false. The type of the result is int. The relational and equality operators test the following relationships: Operator Description < First operand less than second operand > First operand greater than second operand <= First operand less than or equal to second operand >= First operand greater than or equal to second operand == First operand equal to second operand != First operand not equal to second operand Example Try the following example to understand all the relational operators available in C − #include <stdio.h> main() { int a = 21; int b = 10; int c ; if( a == b ) { printf("Line 1 - a is equal to bn" ); } else { printf("Line 1 - a is not equal to bn" ); } if ( a < b ) { printf("Line 2 - a is less than bn" ); } else { printf("Line 2 - a is not less than bn" ); } if ( a > b ) { printf("Line 3 - a is greater than bn" ); } else { printf("Line 3 - a is not greater than bn" ); } /* Lets change value of a and b */ a = 5; b = 20; if ( a <= b ) { printf("Line 4 - a is either less than or equal to bn" ); } if ( b >= a ) { printf("Line 5 - b is either greater than or equal to bn" );
  • 5. BY REHAN IJAZ } } When you compile and execute the above program, it produces the following result − Line 1 - a is not equal to b Line 2 - a is not less than b Line 3 - a is greater than b Line 4 - a is either less than or equal to b Line 5 - b is either greater than or equal to b 4. Logical Operators Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then − Operator Description Example && CalledLogical ANDoperator.If boththe operands are non-zero, thenthe conditionbecomestrue. (A && B) is false. || CalledLogical OROperator.If any of the twooperandsisnon- zero,thenthe conditionbecomestrue. (A || B) is true. ! CalledLogical NOTOperator.Itis usedtoreverse the logical state of itsoperand.If a conditionistrue,thenLogical NOT operatorwill make itfalse. Example #include <stdio.h> main() { int a = 5, int b = 20, int c ; if ( a && b ) { printf("Line 1 - Condition is truen" ); } if ( a || b ) { printf("Line 2 - Condition is truen" ); } /* lets change the value of a and b */ a = 0; b = 10; if ( a && b ) { printf("Line 3 - Condition is truen" ); } else { printf("Line 3 - Condition is not truen" ); } if ( !(a && b) ) { printf("Line 4 - Condition is truen" ); }
  • 6. BY REHAN IJAZ } Output Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true 5. Conditional Operators Syntax : expression1 ? expression2 : expression3 1. Expression1isnothingbutBooleanConditioni.eitresultsintoeither TRUEor FALSE 2. If resultof expression1isTRUE thenexpression2isExecuted 3. Expression1issaidtobe TRUE if itsresultisNON-ZERO 4. If resultof expression1isFALSEthenexpression3isExecuted 5. Expression1issaidtobe FALSEif its resultisZERO Example : Even- Odd #include<stdio.h> int main() { int num; printf("Enter the Number : "); scanf("%d",&num); (num%2==0)?printf("Even"):printf("Odd"); }
  • 7. BY REHAN IJAZ C - Type Casting Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows – Syntax: (type_name) expression Example 1 #include <stdio.h> main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %fn", mean ); } Output Value of mean : 3.400000 It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value. Example 2 #include <stdio.h> main() { int i = 17; char c = 'c'; /* ascii value is 99 */ int sum; sum = i + c; printf("Value of sum : %dn", sum ); } Output Value of sum : 116 Example 3 #include <stdio.h> main() { int i = 17; char c = 'c'; /* ascii value is 99 */ float sum; sum = i + c; printf("Value of sum : %fn", sum ); } Output Value of sum : 116.000000
  • 8. BY REHAN IJAZ DecisionControl structure using if Decisionmakingstructuresrequire thatthe programmerspecifiesone or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determinedtobe true,and optionally, other statements to be executed if the condition is determined to be false. C - if statement The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user-entered password, your program can decide whether a user is allowed access to the program. Example #include <stdio.h> int main () { int a = 10; if( a < 20 ) { printf("a is less than 20n" ); } printf("value of a is : %dn", a); return 0; } Output a is less than 20; value of a is : 10
  • 9. BY REHAN IJAZ C - nested if statements #include <stdio.h> int main () { int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { printf("Value of a is 100 and b is 200n" ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); return 0; } Output Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200 C - if...else statement An if statementcanbe followedbyanoptional elsestatement,whichexecuteswhenthe Boolean expressionisfalse. Example #include <stdio.h> int main () { int a = 100; if( a < 20 ) { printf("a is less than 20n" ); } else
  • 10. BY REHAN IJAZ { printf("a is not less than 20n" ); } printf("value of a is : %dn", a); return 0; } Output a is not less than 20; value of a is : 100 C nested if else statement The nested if...else statement has more than one test expression. If the first test expression is true, it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the secondtestexpression.If the secondtestexpressionistrue,itexecutesthe statement/sinside the braces{ } justbelowit.Thisprocesscontinues.If all the test expression are false, code/s inside else is executed and the control of program jumps below the nested if...else The ANSI standard specifies that 15 levels of nesting may be continued. Example #i nclude <stdio.h> i nt mai n(){ i nt numb1, numb2; pri ntf("Enter two i ntegers to checkn"); scanf("%d %d",&numb1,&numb2); i f(numb1==numb2) pri ntf("Result: %d = %d",numb1,numb2); el se i f(numb1>numb2) pri ntf("Result: %d > %d",numb1,numb2); el se pri ntf("Result: %d > %d",numb2,numb1); return 0; } Output Enter two integers to check. 5 3 Result: 5 > 3
  • 11. BY REHAN IJAZ If...else if...else Statement An if statementcanbe followed byanoptional elseif...elsestatement,whichisveryuseful totestvarious conditionsusingsingle if...else if statement. Syntax if(boolean_expression 1) { /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ } Example #include <stdio.h> int main () { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a == 10 ) { /* if condition is true then print the following */ printf("Value of a is 10n" ); } else if( a == 20 ) { /* if else if condition is true */ printf("Value of a is 20n" ); } else if( a == 30 ) { /* if else if condition is true */ printf("Value of a is 30n" ); } else { /* if none of the conditions is true */ printf("None of the values is matchingn" ); } printf("Exact value of a is: %dn", a ); return 0; } Output None of the values is matching Exact value of a is: 100
  • 12. BY REHAN IJAZ C - switchstatement A switch statementallowsavariable tobe testedforequality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. Syntax The syntax for a switch statement in C programming language is as follows − switch(expression) { case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); } Example #include <stdio.h> int main () { /* local variable definition */ char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : case 'C' : printf("Well donen" );
  • 13. BY REHAN IJAZ break; case 'D' : printf("You passedn" ); break; case 'F' : printf("Better try againn" ); break; default : printf("Invalid graden" ); } printf("Your grade is %cn", grade ); return 0; } Output Well done Your grade is B Benefits of Switch Statement In some languages and programming environments, the use of a switch-case statement is considered superior to an equivalent series of if else if statements because it is:  Easier to debug  Easier to read  Easier to understand, and therefore  Easier to maintain  Fixed depth: a sequence of "if else if" statements yields deep nesting, making compilation more difficult  Faster execution potential Limitations of Switch Statement Switch statement is a powerful statement used to handle many alternatives and provides good presentation for C program. But there are some limitations with switch statement which are given below:  Logical operators cannot be used with switch statement. For example case k>= 20; is not allowed.  Switch case variables can have only int and char data type. So float or no data type is allowed.
  • 14. BY REHAN IJAZ break statement in C The break statement in C programming has the following two usages −  When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.  It can be used to terminate a case in the switch statement (covered in the next chapter). If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax break; Example #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } return 0; } Output value of a: 10 value of a: 11 value of a: 12 value of a: 13
  • 15. BY REHAN IJAZ value of a: 14 value of a: 15