0% found this document useful (0 votes)
6 views

bpops-203-module-2

The document provides an overview of operators in C programming, including their classifications based on the number of operands (unary, binary, ternary) and types of operations (arithmetic, relational, logical, etc.). It explains various operators with examples, such as arithmetic expressions, type conversion, and operator precedence. Additionally, it covers increment/decrement operators, conditional operators, and bitwise operators, detailing their functionality and usage in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

bpops-203-module-2

The document provides an overview of operators in C programming, including their classifications based on the number of operands (unary, binary, ternary) and types of operations (arithmetic, relational, logical, etc.). It explains various operators with examples, such as arithmetic expressions, type conversion, and operator precedence. Additionally, it covers increment/decrement operators, conditional operators, and bitwise operators, detailing their functionality and usage in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

lOMoARcPSD|55733806

Bpops 203 Module 2

system programing (Visvesvaraya Technological University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Zaheer Gamers ([email protected])
lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Module -2
Operators In C

EXPRESSIONS
OPERATOR:
It is a symbol or a token that specifies the operation to be performed on various types of data.
Ex: +, -, *

OPERAND:
A constant or a variable or a function which returns a value is an operand. An operator may
have one or two o three operands.

EXPRESSION:
A sequence of operands and operators that reduces it to a single value on solving it is an
expression. Eg: a+b-c*a/2

CLASSIFICATION OF OPERATORS
It is classified based on:
1. The number of operand on operator has.
2. The type of operation being performed.

A.. BASED ON NUMBER OF OPERANDS


OPERATOR TYPES:
Operators are classified into three main categories
1. Unary operators: ++, -- etc
2. Binary operators: +, -, *, / etc
3. Ternary operators: ? and :
4. Special operators: comma(,), sizeof() etc

a. UNARY OPERATOR:
An operator which acts on only one operand to produce the result. Eg: -10, -a, *b, a++ etc.

b. BINARY OPERATOR:
An operator which acts on two operands to produce the result. Eg: a+b, a*b.

c. TERNARY OPERATOR:
An operator which acts on three operands to produce a result. Eg: a?b:c

Department of ISE, SJBIT 1

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

B. BASED ON TYPE OF OPERATION


TYPES OF OPERATORS:
1. Arithmetic operators. Eg: +, *, - etc
2. Assignment operators. Eg: =, += etc
3. Increment/Decrement operators. Eg: ++, --
4. Relational operators. Eg: <, <= etc
5. Logical operators. Eg: &&, || etc
6. Conditional operators. Eg: ?:
7. Bitwise operators. Eg: &, ^ etc
8. Special operators. Eg: , ,[] etc

1. ARITHMETIC OPERATORS:
The operators that are used to perform arithmetic operations such as addition, subtraction,
multiplication, division and modulus operations are called arithmetic operators. These
operators perform operation on two operands and hence are binary operators.

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus

ARITHMETIC EXPRESSIONS:
An expression is a sequence of operands and operators which when evaluated results in a single
value. Two types:

1. Additive expressions. Eg: a+b, a-b


2. Multiplicative expressions. Eg: a*b, a/b, a%b

TYPE CONVERSION:
In C language, a programmer can instruct the compiler to convert the data from one data type
to another data type. This process is called type conversion.

1. Implicit conversion. Eg: ½.0 gets converted to 1.0/2.0


2. Explicit convertion. Eg: 1/(float)2 gets converted to 1.0/2.0 Example program:
#include <stdio.h>
int main()
{
int m1=75, m2=85; float avg;
avg=(float)(m1+m2)/2;
return 0;
}

Department of ISE, SJBIT 2

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Modes of Arithmetic Operation


 Integer expressions
 Floating point expressions
 Mixed mode expressions

INTEGER EXPRESSIONS:

If all the operands in an expression are integers, the expression is called integer expression. Eg:
4/2=2, 2+5=7.

FLOATING POINT EXPRESSION:

If all the operands in an expression are floating point numbers or double values, it is floating
point expression. Eg: 4.0/2.0=2.0

MIXED MODE EXPRESSIONS:

An expression that has operands of different data types is called mixed mode expressions. Eg:
4.0/3=1.333

ARITHMETIC OPERATOR’S PRECEEDENCE:


BODMAS rule:

Highest to lowest:

ASSOCIATIVITY OF OPERATORS:
In programming languages and mathematical notation, the associativity (or fixity) of an
operator is a property that determines how operators of the same precedence are grouped in the
absence of parentheses.

1. Left associativity (left to right): operators are grouped from the left.
2. Right associativity (right to left): operators are grouped from the right

Department of ISE, SJBIT 3

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

2. RELATIONAL OPERATORS:
The operators that are used to find the relationship between two operands are called relational
operators.
i. Less than ( < )
ii. Less than or equal to ( <= )
iii. Greater ( > )
iv. Greater than or equal to ( >= )
v. Equal ( == )
vi. Not equal ( != )

Department of ISE, SJBIT 4

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Associativity for all above is left to right.


Relational Results output
expressions (True/False)
3>2 TRUE 1
2<3 TRUE 1
2>5 FALSE 0
6==6 TRUE 1
6+2>17 FALSE 0
8!=8 FALSE 0

3. LOGICAL OPERATORS:
If the data value is 0, it is considered false. Else it is true. The operators that are used to combine
two or more relational expressions are called logical operators. Logical operators are used to
combine two or more relational expressions. The output will be either true or false.

1. Unary operator ( ! )
2. Binary operator ( && , || ) Left to right associativity for both.

 Logical NOT ( ! ):

OPERAND !OPERAND
True (1) False (0)
False (0) True (1)

 Logical AND ( && ):


Operand1 AND Operand2 Result
true && true = True
true && False = False
False && True = False
false && false = False

 Logical OR ( || ):

Operand1 OR Operand2 result


false || False = False
false || True = True
True || False = True
true || true = true

Department of ISE, SJBIT 5

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

4. ASSIGNMENT OPERATORS:
An operator which is used to assign the data or result of an expression into a variable (also
called memory location) is called an assignment operator. It is denoted by ‘=’ sign. A statement
with assignment operator is called assignment statement or assignment expression.

There are three types:


 Simple assignment statement. eg:a=a+10
 Shorthand assignment statement. Eg: a+=10
 Multiple assignment statement. Eg: a=b=10

 SIMPLE ASSIGNMENT STATEMENT:

Syntax: Variable=expression (eg: a=a+10)

i. The expression can be a constant or a variable or any expression.


ii. The single symbol ‘=’ is an assignment operator.
iii. The expression on right side of assignment operator is evaluated and the result is
converted into type of variable on the left side of assignment operator.
iv. The converted data is copied into memory location which is identified by the variable
on the LHS of the assignment operator.

 SHORTHAND ASSIGNMENT OPERATOR ( COMPOUND ASSIGNMENT ):

Operators such as +=, -=, *=, /= and %=

The expression a+=10 means a=a+10. Similarly others.

 MULTIPLE ASSIGNMENT STATEMENT:


i=10 ; j=10; k=10
can be written as i=j=k=10;

5. INCREMENT AND DECREMENT OPERATORS


 INCREMENT OPERATOR:
Types:
i. Post increment. Eg: a++
ii. Pre increment. Eg: ++a

Both increments value of variable a by 1.

Department of ISE, SJBIT 6

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

i. POST INCREMENT
eg:
#include <stdio.h>
int main()
{
int a=5, b;
b=a++;
return 0;
}

The expression b=a++ is evaluated as b=a


a++ => a=a+1
i.e. value of b is 5. And a is 6.

ii. PRE INCREMENT


Eg:

#include <stdio.h>
int main()
{
int a=5, b;
b=++a;
return 0;
}

The expression b=++a is evaluated as


++a => a=a+1 b=a
i.e. value of b is 6 and a is also 6.

 DECREMENT OPERATOR:
Types:
i. Post decrement. Eg: a++
ii. Pre decrement. Eg: ++a

Both decrements value of variable a by 1.

i. POST DECREMENT eg:


#include <stdio.h>
int main()
{
int a=5, b;
b=a--;

Department of ISE, SJBIT 7

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

return 0;
}

The expression b=a-- is evaluated as b=a


a-- => a=a-1
i.e value of b is 5. And a is 4.

ii. PRE DECREMENT


Eg:

#include <stdio.h>
int main()
{
int a=5, b;
b=--a;
return 0;
}

The expression b=--a is evaluated as


--a=> a=a-1 b=a
i.e. value of b is 4 and a is also 4.

6. CONDITIONAL OPERATOR
It is also called ternary operator.

Syntax: (expression 1) ? (expression 2): (expression 3);

• Expression 1 will be evaluated to either true or false.


• If expression 1 is true, expression 2 is executed.
• If expression 1 is false, expression 3 is executed.

#include <stdio.h>
int main()
{
int a=7, b=8, big;
big=(a>b)?a:b;
printf(“%d”, big);
return 0;
}

output: 8

Department of ISE, SJBIT 8

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

7. BITWISE OPERATOR
The operators that are used to manipulate the bits of given data are called bitwise operators.
Types:
i. Bit-wise negative (~)
ii. Left shift (<<)
iii. Right shift (>>)
iv. Bit-wise AND (&)
v. Bit-wise OR (|)
vi. Bit-wise XOR (^)

i. BIT-WISE NEGATIVE/ ONE’S COMPLIMENT:


The operator that is used to change every bit from 0 to 1 and from 1 to 0 in the specified operand
is called one’s compliment operator.
Eg:

#include <stdio.h>
int main()
{
int a=10;
int b=~a;
return 0;
}

Here, a=10 i.e in binary representation 8 bits: 00001010

Its compliment is 11110101 i.e 245 in decimal which is assigned to b.

ii. LEFT SHIFT OPERATOR:


The operator that is used to shift the data by a specified number of bit positions towards left is
called left shift operator.

Syntax: b=a<<num;

Eg: b=5<<1;

 5 in binary is 00000101 which when shifted one bit left


 00001010 <= a zero is appended.
 Value is 10. Assigned to variable b.

iii. RIGHT SHIFT OPERATOR:


The operator that is used to shift the data by a specified number of bit positions towards left is
called left shift operator.

Department of ISE, SJBIT 9

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Syntax: b=a>>num;
Eg: b=5>>1;

 5 in binary is 00000101 which when shifted one bit left


 0000010 <= a zero is appended.
 Value is 2. Assigned to variable b.

iv. BIT-WISE AND:


If the corresponding bit positions in both the operands are 1, then AND operation results in 1.
Otherwise AND operation results in 0.

0 & 0=0, 0&1=0, 1&0=0, 1&1=1.


Eg: a=10,
b=6;
c=a & b;

then, a: 00001 0 10

b: 00000 110
c: 00000 010 i.e 2 in decimal.

v. BIT-WISE OR:
If the corresponding bit positions in both the operands are 0, then OR operation results in 0.
Otherwise OR operation results in 1.

0 & 0=0, 0&1=1, 1&0=1, 1&1=1. Eg: a=10, b=6;


c=a & b;

then, a: 0000 1010

b: 0000 0110

c: 000011 10 i.e 14 in decimal.

vi. BIT-WISE XOR:


If the corresponding bit positions in both the operands are 0 or 1, then XOR operation results
in 0. Otherwise XOR operation results in 1.

0 & 0=0, 0&1=1, 1&0=1, 1&1=0. Eg: a=10, b=6;


c=a & b;

then, a: 00001 010

Department of ISE, SJBIT 10

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

b: 00000 110

c: 00001 100 i.e 12 in decimal.

THE PRECEDENCE OF OPERATORS (HIERARCHY OF OPERATORS)

Department of ISE, SJBIT 11

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

8. SPECIAL OPERATORS
i. COMMA OPERATOR:
It has the least precedence among all the operators and is left associative. It is normally
used to:
 Separate items in the list. Eg: a=12,13,14; // 13 and 14 are discarded.
 Combine two or more statements into a single statement. Eg: a=10, b=20, c=30;

Eg:
printf(“%d%d”,a,b);

sizeof():
this operator is used to determine the number of bytes occupied by a variable or a
constant in the memory.

Eg: sizeof(char) gives 1 byte.

sizeof(int) gives 4 bytes in a 32 bit machine.

EXPRESSION FORMATS
Types of C expressions:
1. Primary
2. Unary
3. Binary
4. Ternary
5. Assignment
6. Comma

PRIMARY EXPRESSIONS:
An expression with only one operand but without any operator.
1. Names: variable or a function. Eg: const int MAX=10; int a,b;
2. Constants: a piece of data that cannot be changed during the execution of the program.
3. Parenthesized expressions: any expression or a value enclosed in parentheses must be
reducible to a single value.

UNARY EXPRESSIONS:
An expression with only one operand and one operator is called unary expression.
1. Unary minus expression. Eg: -5
2. Unary plus expression. Eg: +5
3. Prefix expression. Eg: ++i
4. Postfix expression. Eg: i++

Department of ISE, SJBIT 12

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

BINARY EXPRESSIONS:
An expression containing two operands and an operator.
1. Multiplicative expressions. Eg: 2*4, a/b
2. Additive expressions. Eg: a-b, a+b
3. Relational expressions. Eg: a>b, a<b
4. Logical expressions. Eg: a &&b
5. Bitwise expressions. Eg: a&b, a|b

TERNARY EXPRESSIONS:
An expression containing three operands and two operators. Eg: a?b:c

ASSIGNMENT EXPRESSIONS:
A statement with assignment operator. E: a=10, a=b*c

COMMA EXPRESSIONS:
A set of statements separated by comma are evaluated from left to right one after the other.
Eg: a=10, b=30;

Department of ISE, SJBIT 13

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

STATEMENTS, ALGORITHMS AND FLOWCHARTS

STATEMENTS
In a programming language, it is used to inform the computer to perform an action when a
program is executed. A statement can alter the value of a variable, it can accept the input, it
can manipulate the data and display the data.
1. Expression statement
2. Compound statement
3. Control statement

1. EXPRESSION STATEMENT:
An expression followed by a semicolon. An expression statement include:
 Statements with assignment operator. Eg: a=b*c; a=50;
 Statements consisting of only expressions. Eg: i++;
 Statements that invoke the functions. Eg: printf(“hello”); sum(n);

2. COMPOUND STATEMENT:
The sequence of statements enclosed within a pair of braces ‘{‘ and ‘}’ is called a compound
statement. Eg:
{
sum=a+b;
printf(“sum = %d “,sum);
}

3. CONTROL STATEMENT:
The order in which the statements are executed is called control flow.
Three types:
1. Sequential control statements
2. Branching statements
3. Loop statements

Department of ISE, SJBIT 14

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

ALGORITHMS AND FLOWCHARTS

ALGORITHM:
An algorithm is defined as unambiguous, step by step procedure to solve a given problem in
finite number of steps by accepting a set of inputs and producing desired result for the given
problem. After producing the result, algorithm should terminate.

FLOW CONTROL:
A algorithm or a program contains various actions to be executed in the form of statements.
Specifying the order in which various actions have to be executed is called flow control.

CHARACTERISTICS/PROPERTIES OF ALGORITHMS:
1. Each and every instruction should be precise and unambiguous i.e. each and every
instruction should be clear and should have only one meaning.
2. Each instruction should be performed in finite time.
3. One or more instructions should not be repeated infinitely. It means that the algorithm
must terminate ultimately.
4. After the instructions are executed, the user should get the required results.

ADVANTAGES AND DISADVANTAGES OF ALGORITHMS:


ADVANTAGES:
1. it is a step-by-step procedure of a solution to a given problem ,which is very easy to
understand.
2. it has got a definite procedure.
3. it easy to first develop an algorithm and then convert it into a flowchart and then into a
computer program.
4. it is independent of programming language.
5. it is easy to debug as every step is got its own logical sequence.

DISADVANTAGES:
It is time consuming & cumbersome as an algorithm is developed first which is converted into
flowchart and then into a computer program.

FLOWCHART:
A flowchart is a type of diagram that represents an algorithm or process, showing the steps as
boxes of various kinds, and their order by connecting them with arrows. This diagrammatic
representation illustrates a solution to a given problem. Process operations are represented in
these boxes, and arrows; rather, they are implied by the sequencing of operations. Flowcharts
are used in analyzing, designing, documenting or managing a process or program in various
fields.

Department of ISE, SJBIT 15

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Symbols used in flowchart

ADVANTAGES AND DISADVANTAGES OF FLOWCHARTS:


ADVANTAGES:
1. Communication: Flowcharts are better way of communicating the logic of a system to
all concerned.
2. Effective analysis: With the help of flowchart, problem can be analysed in more
effective way.
3. Proper documentation: Program flowcharts serve as a good program documentation,
which is needed for various purposes.
4. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis
and program development phase.
5. Proper Debugging: The flowchart helps in debugging process.
6. Efficient Program Maintenance: The maintenance of operating program becomes easy
with the help of flowchart. It helps the programmer to put efforts more efficiently on
that part

DISADVANTAGES:
 Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
 Alterations and Modifications: If alterations are required the flowchart may require re-
drawing completely.

Department of ISE, SJBIT 16

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

 Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart


becomes a problem. The essentials of what is done can easily be lost in the technical
details of how it is done.

FLOWCHART ALGORITHM

It is a graphical or pictorial representation along They are expressed in English language along with
with instructions mathematical expressions
For complex problems, flowcharts become They can be used for complex or simple problems
complex
Modification is difficult Can be modified easily

We can easily understand the logic Understanding may be slightly difficult

Department of ISE, SJBIT 17

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

TYPES OF ERRORS
1. Syntax errors
2. Logical errors
3. Runtime errors

SYNTAX ERRORS:
Each language has a set of rules that are to be followed while writing a program. During
compilation, the programming statements that violate these grammatical rules are identified by
the compiler and issue error messages so that programmer can locate and correct them.

Eg:
#include <stdio.h>
void main()
{
int a;
a=10 //error: missing ;
print(“a value= %d”,a); //error: print is undefined. It must be printf
}

LOGICAL ERRORS:
It is also called as semantic errors. When a program is compiled and executed successfully and
if the desired output not obtained, then logical errors are present.

Eg:
#include <stdio.h>
void main()
{
int a,b,sum;
printf(“enter 2 numbers: “);
scanf(“%d%d”,&a,&b);
sum=a-b; //we find the difference and not the sum! No error but answer is not
expected. printf(“\nsum= %d”,sum);
}

RUNTIME ERRORS:
It occurs when a program attempts to perform a task that is not allowed. The statements which
results in run time errors are syntactically and logically correct.
Eg: attempting to divide by a zero.
#include <stdio.h>
void main()
{

Department of ISE, SJBIT 18

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

float a=5, b=0, quo;


quo=a/b;
}

Department of ISE, SJBIT 19

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

DIFFERENCE BETWEEN SYNTAX AND LOGICAL ERRORS:

SYNTAX ERRORS LOGICAL ERRORS


When programmer is not following the syntax These errors are accidentally occurred due to
rules unintended mistakes
Easy to locate and correct Difficult to locate and correct
Compiler find them out Debuggers are used to locate them
It cannot be executed Can be executed
Programming knowledge of that language is Knowledge of programming logic is required
required

Department of ISE, SJBIT 20

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Decision Control and Looping Statements

CONTROL STATEMENT
The order in which the statements are executed is called control flow. The statements that are
used to control the flow of execution of the program are called control statements. Based on
the order in which the statements are executed, the various control statements are classified
into:
1. Sequential control statements
2. Branching statements: if else
3. Loop statements: for, while

SEQUENTIAL STATEMENTS:
The programming statements that are executed sequentially (one after the other) are called
sequential statements.
Eg:

void main()
{
int a,b,sum;
printf(“enter 2 numbers: “);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“\nsum= %d”,sum);
}

ADVANTAGES:
1. No separate control statements are required to execute the sequential statements
one after the other.
2. They are executed in the order they appear in the program.

DISADVANTAGES:
1. The sequence of execution of the program cannot be changed.

CONTROL STRUCTURES
 A program is nothing but the execution of sequence of one or more instructions.
 Quite often, it is desirable to change the order of execution of statements based on
certain conditions or
 This involves a kind of decision making to see whether a particular condition has
occurred or not and direct the computer to execute certain statements accordingly.
 Based on application, it is necessary / essential
 To alter the flow of a program
 Test the logical conditions

Department of ISE, SJBIT 21

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

 Control the flow of execution as per the selection these conditions can be placed in the
program using decision-making statements.

C SUPPORTS MAINLY FOUR TYPES OF CONTROL STATEMENTS


1. Decision making statements
i. if statement
i. if else statement
ii. nested if statement
iii. else if ladder
iv. switch statement

2. Loop control statements


i. while loop
ii. for loop
ii. do-while loop

3. Conditional control (jump) statements


i. break statement
ii. continue statement

 goto statement (unconditional jump)

BASIC CONCEPT OF DECISION STATEMENTS

 Decision making is critical to computer programming.


 There will be many situations when you will be given 2 or more options and you will
have to select an option based on the given conditions.

The following flow diagram shows how conditions work in C.

Department of ISE, SJBIT 22

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

Decision making statements


i. THE if STATEMENT
 This is basically a “one-way” decision statement.
 This is used when we have only one alternative.

The syntax is shown below:


if (expression)
}
Rest of the code;
 Firstly, the expression is evaluated to true or false. Only if the expression is true then
true part statements will be executed.

Program to illustrate the use of if statement.


#include<stdio.h> void main ()
{
int n;
printf ("Enter any non-zero integer: \n") ;
scanf ("%d", &n) ;
if (n>0)
print f ("Number is positive number ") ; if (n<0)
printf ("Number is negative number ") ;
}
Output:
Enter any non-zero integer:
7
Number is positive number

THE if-else STATEMENT


 This is basically a “two-way” decision statement.
 This is used when we must choose between two alternatives.

The syntax is shown below:


if(expression)

}
Rest of the code;

Department of ISE, SJBIT 23

Downloaded by Zaheer Gamers ([email protected])


lOMoARcPSD|55733806

Introduction to C Programming – 22ESC145

 Firstly, the expression is evaluated to true or false. Only if the expression is true then
true part statements will be executed otherwise false part statements are executed.

The flow diagram is shown below:

rest of the code

Department of ISE, SJBIT 24

Downloaded by Zaheer Gamers ([email protected])

You might also like