INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Module-2 Syllabus
Operators: Introduction to Operators, Arithmetic Operators, Relational Operators, Logical
Operators, Assignment Operators, Increment and Decrement Operators, Conditional Operators,
Precedence of Arithmetic Operators.
Decision Making, Branching, Looping: Introduction, Decision Making with IF Statement,
Simple IF Statement, The IF..ELSE Statement, Nesting of IF..ELSE Statements, The ELSE IF
Ladder, The Switch Statement, The ?: Operator, The GOTO Statement, WHILE, DO, FOR,
Jumps in LOOPS.
Operators
Operator: An operator is a symbol (or a token) that specifies the operation to be performed on various
types of operands.
Operand: A constant or a variable or a function which returns a value is an operand.
Expression: A sequence of operands and operators that reduces to a single value is known as expression.
The operators available in C are shown below.
Dept. of CSE, AJIET, Mangaluru. Page 1 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Operators Classification: The operators in C language are classified based on
a. The number of operands an operator has and
b. The type of operation being performed
Classification based on the number of operands:
The operators are classified into three major categories based on the number of operands as shown
below:
1. Unary Operators
2. Binary Operators
3. Ternary Operators
1. Unary Operators: An operator that operates on one operand to produce a result is called unary operator.
Here operator precedes the operand. Example: -10, ++x, y-- etc. are all the expressions with unary operators.
Dept. of CSE, AJIET, Mangaluru. Page 2 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
2. BinaryOperator: An operator that operates on two operands in an expression to produce a result is called
a binary operator. In an expression involving a binary operator, the operator is in between two operands. Example
:a+b, a*b , 10/5 ,etc. are all expressions consisting of binary operators.
3. Ternary operators: An operator that operates on three operands to produce a result is called as ternary
operator. Ternary operator ? : is also called the conditional operator. Example: a ? b : c
Classification based on the type of the operation:
The operators are classified into following eight types based on the operation they perform on operands:
1. Arithmeticoperators
2. Assignmentoperators
3. Relationaloperators
4. Logicaloperators
5. Bit wiseoperators
6. Conditional operators (ternaryoperators)
7. Increment/decrementoperators
8. Specialoperators
Arithmetic Operators
The operators that are used to perform arithmetic operation such as addition, subtraction, multiplication, division
and modulus operation are called arithmetic operators. These operators perform operations on two operands and
hence they are all binary operators. The meaning of all the operators along with examples is shown in table
below:
Dept. of CSE, AJIET, Mangaluru. Page 3 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Exercise: Write a C program to illustrate the arithmetic operators.
Assignment operators in C
The = operator is also called as the assignment operator. It is used to assign a constant value to the variable.
Syntax: variable = constant ; Example: a=100;
Or variable = another_variable ; Example: a=b;
Or variable = expression; Example: a=1*b
Short hand assignment operators
The syntax of short hand assignment statement is :variable op= expression;
Here the op may be operators such as +,-,/,*,%,<<,>>, !,^ etc. Some of the short hand assignment operators are
+=,-=,*= ,/= and %= .Example: x + = 1 is same as x = x + 1
Multiple Assignment statement
A statement using which a value or a set of values are assigned to different variables is called
multiple assignment statement.
The assignment operator ‘=’ can be used to assign a single value to more than one variable.
Example :i=j=k=10;
Increment and Decrement Operators:
a) Increment operator:
The ++(double plus) operator is also called as incrementation operator. This operator is used to increment the
value of a variable by 1.
Dept. of CSE, AJIET, Mangaluru. Page 4 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
post and pre increment operators:
If the ++ operator is placed before the operand then it is called as pre increment operator. If the ++ operator is
placed after the operand then it is called as post increment operator.
Example 1:
m = 5;
y = ++m; (prefix)
In this case the value m will be incremented to 6 first and then it will be assigned to y. Therefore at the end of
the execution, the value of y and m will be 6.
Example 2:
m = 5;
y = m++; (postfix)
In this case the values of m will gets assigned first to y and then it will gets incremented. Therefore at the end of
the execution, the value of y will be 5 and m will be 6.
b) Decrement operator:
The --(double minus) operator is also called as decrementation operator. This operator is used to decrement the
value of a variable by 1.
post and pre decrement operators:
If the -- operator is placed before the operand then it is called as pre decrement operator. If the -- operator is
placed after the operand then it is called as post decrement operator.
Example 1:
m = 5;
y = --m; (prefix)
In this case the value m will be decremented to 4 first and then it will be assigned to y. Therefore at the end of
the execution, the value of y and m will be 4.
Example 2:
m = 5;
y = m--; (postfix)
In this case the values of m will gets assigned first to y and then it will gets decremented. Therefore at the end
of the execution, the value of y will be 5 and m will be 4.
Dept. of CSE, AJIET, Mangaluru. Page 5 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Example: Program to illustrate the use of increment operators. // Write similarly for decrement
#include<stdio.h>
main()
{
int x=10,y = 10, z ;
z= x++;
printf(“ z=%d x= %d\n”, z, x);
z= ++y;
printf(“ z=%d y= %d”, z, y);
}
Output: z=10 x=11 z=11 y=11
Relational Operators
Definition: The operators that are used to find the relationship between two operands are called relational
operators. The two operands may be constants, variables or expressions. The relationship between the two
operand values results in true or false values. All non zero values (i.e. 1, -1, 2, -2) will be treated as true. While
zero value (i.e. 0 ) will be treated as false.
A simple relational expression contains only one relational operator and takes the following form.
exp1 relational_operator exp2
There are four relational operators and two equality operators which are closely associated to relational
operators as given in Table.
Logical Operators:
Definition: The operators which are used to combine two or more relational expression are known as logical
Dept. of CSE, AJIET, Mangaluru. Page 6 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
operators.
Syntax: relational expression connector relational expression;
The different types of logical operators are shown below.
i) Logical AND (&&): This operator is used to evaluate 2 conditions or expressions with relational operators
simultaneously.
If both the expressions to the left and to the right of the logical operator is true then the whole compound
expression is true. Example: Consider the expression (a> b) &&(x = = 10). Here the expression to the left is a
> b and that on the right is x == 10, the whole expression is true only if both expressions are true i.e., if a is
greater than b and x is equal to10.
ii) Logical OR (| |): The logical OR is used to combine 2 expressions or the condition evaluates to true if any
one of the 2 expressions is true.
Example: Consider the expression (a<m)||( a <n). Here the expression evaluates to true if any one of them is
true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m
and n.
Logical NOT (!): The logical not operator takes single expression and evaluates to true if the expression is
false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.
Operator Meaning Example
&& Logical AND Ifc=5andd=2then((c==5)&&(d>5))returns false.
|| Logical OR If c=5 and d=2 then ((c==5) || (d>5)) returns true.
! Logical NOT If c=5 then !(c==5) returns false
For example: In the expression ! (x >= y) the NOT expression evaluates to true only if the value of x is
neither greater than nor equal to y.
Dept. of CSE, AJIET, Mangaluru. Page 7 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
/* Program to illustrate the use of both relational & Logical operators.*/
#include<stdio.h> void main()
{ Output:
printf(“5>3 && 5<10 : %d \n”, 5>3 && 5<10); 5>3 && 5<10 : 1
printf(“ 8<5 || 5= =5 : % d \n”, 8<5 || 5==5); 8<5 || 5= =5 : 1
printf(“!(8 = =8) : %d ”, !(8==8)) ; !(8 = =8) : 0
}
Bit wise operators in C:
Bitwise operators are operators which act on the individual bits of the data stored in the memory location. The
operators that are used to manipulate the bits of given data are called bitwise operators.
There are 6 bitwise operators: as shown in the previous table
These operators are used to perform logical operation (and, or, not) on individual bits of a binary number.
Ex for ~ (bitwise complement) Ex for & (bitwise AND)
a = 13 0000 1101 a = 13 0000 1101
~a= 11110010 b=6 0000 0110
a&b 0000 0100
• Ex for || (bitwise OR) Ex for ^ (bitwise xor)
a = 13 0000 1101 a = 13 0000 1101
b=6 0000 0110 b=6 0000 0110
a|b 0000 1111 a^b 0000 1011
The operator that is used to shift the data by a specified number of bit positions towards left or right is called
shift operator.
• The syntax is shown below for << The syntax is shown below for >>
b=a << num; b=a >> num;
Dept. of CSE, AJIET, Mangaluru. Page 8 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
where a is value to be shifted
num is number of bits to be shifted
Ex for <<(left shift): Ex for >>(right shift):
a = 13 0000 1101 a = 13 0000 1101
b=a<<1 0001 1010 b=a<<1 0000 0110
Conditional Operators
Conditional operator which is also known as ternary operator, operates on three operands. Conditional operators
return one value if condition is true and returns another value if condition is false.
The syntax is shown below:
(exp1)? exp2: exp3;
where exp1 is an expression evaluated to true or false;
If exp1 is evaluated to true, exp2 is executed;
If exp1 is evaluated to false, exp3 is executed.
Example: a=10; b=15;
x= (a>b) ? a :b ;
Example: Program to find largest of 2 numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b, big ;
printf(“ enter 2 distinct numbers \n”);
scanf(“%d %d”, &a, &b);
big=(a>b)? a :b;
printf(“ largest number =%d ”, big);
}
Output:
enter 2 distinct numbers
34
largest number = 4
Precedence and Associativity Rules
1. Precedence rules decides the order in which different operators are applied.
2. Associativity rule decides the order in which multiple occurrences of the same level operator are
applied.
The list of operators supported in C language along with their precedence and priority is illustrated in Table.
Dept. of CSE, AJIET, Mangaluru. Page 9 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Evaluation of Expression: In order to evaluate any given C Expression one must use the rules
described below:
Rules for Evaluating Expressions
a. Parentheses Rule: All expressions in parentheses must be evaluated separately. Nested
parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated
first.
b. Operator precedence Rule : Operators in the same expression are evaluated in the following
order:
1. Unary Operator [Link] Operators [Link] Shift and Right Shift Operators [Link]
Operators [Link] and In Equality Operators [Link] Operators [Link] Operators [Link]
Operators [Link] Operators and [Link].
Dept. of CSE, AJIET, Mangaluru. Page 10 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Associativity Rule: Whenever two or more operators having the same precedence appear in the same
expression then they must be evaluated on the basis of Left to Right Associativity and Right to Left
associativity.
Unary operators, Ternary operators and Assignment operators are evaluated on the basis of Right to Left
Associativity. Whereas the remaining all category of operators are evaluated on the basis of Right to Left
Associativity.
Evaluation of expression
a=3*(4%5)/2 // parenthesized sub expression from left to right
a=3*4/2 // multiplication and division from left to right
a=12/2
a= 6
Z= 9 – (12 / (3 + 3) * 2) – 1;
Z= 9 – (12/ 6 * 2) – 1;
Z= 9 – (2 * 2) – 1;
Z= 9 – 4– 1;
Z= 5– 1;
Z= 4;
Decision Making, Branching, Looping
C SUPPORTS MAINLY THREE TYPES OF CONTROL STATEMENTS
1) Decision making statements
i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder or cascaded if-else
v) switch statement
2) Loop control statements
i) while loop
ii) for loop
Dept. of CSE, AJIET, Mangaluru. Page 11 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
iii) do-while loop
3) Unconditional control statements / Jump statements
i) break statement
ii) continue statement
iii) goto statement
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.
• If the answer is true, one or more action statements are executed. If the answer is false, then a
different action or set of actions is executed. Regardless of which set of action is executed, the program
continues with the next statement after the selection.
• For ex, we want to print a remark about a student based on secured marks and following is the
situation:
1. Assume given marks are x for a student
2. If given marks are more than 95 then
3. Student is brilliant
4. If given marks are less than 30 then
5. Student is poor
6. If given marks are less than 95 and more than 30 then
7. Student is average
Now, question is how to write programming code to handle such situation. C language provides
conditional i.e., decision making statements which work based on the following flow diagram:
Dept. of CSE, AJIET, Mangaluru. Page 12 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
There are 5 types of decision statements:
i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder
v) switch statement .
The two way selection could be done using:
i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder or cascaded if
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)
{
Statement1/s;
}
• Firstly, the expression is evaluated to true or false.
Dept. of CSE, AJIET, Mangaluru. Page 13 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
If the expression is evaluated to true, then statement1/s is executed.
If the expression is evaluated to false, then statement1/s is skipped.
• The flow diagram is shown below:
/* Program to illustrate the use of if statement */
#include<stdio.h>main()
{
int n;
printf(“Enter any non zero integer: \n”); scanf(“%d”, &n);
if(n>0)
printf(“Number is positive number ”);
if(n<0)
printf(“Number is negative number ”);
}
Output:
Enter any non zero integer: 7
Dept. of CSE, AJIET, Mangaluru. Page 14 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
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)
{
Statement1/s;
}
else
{
Statement2/s;
}
• Firstly, the expression is evaluated to true or false.
If the expression is evaluated to true, then statement1/s is executed.
If the expression is evaluated to false, then statement2/s is executed.
• The flow diagram is shown below:
/* Program to illustrate the use of if else statement. */
Dept. of CSE, AJIET, Mangaluru. Page 15 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
#include<stdio.h>
main()
{
int n;
printf(“Enter any non-zero integer: \n”) ; scanf(“%d”, &n);
if(n>0)
printf(“Number is positive number”);
else
printf(“Number is negative number”);
}
Output:
Enter any non-zero integer: 7
Number is positive number
THE nested if STATEMENT
• An if-else statement within another if-else statement is called nested if statement.
• This is used when an action has to be performed based on many decisions. Hence, it is called as
multi-way decision statement.
• The syntax is shown below:
if(expr1)
{
if(expr2)
{ statement1/s }
else
{ statement2/s }
}
else
Dept. of CSE, AJIET, Mangaluru. Page 16 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
{
if(expr3)
{ statement3/s }
else
{ statement4/s }
}
• Here, firstly expr1 is evaluated to true or false.
If the expr1 is evaluated to true, then expr2 is evaluated to true or false.
If the expr2 is evaluated to true, then statement1/s is executed.
If the expr2 is evaluated to false, then statement2/s is executed.
If the expr1 is evaluated to false, then expr3 is evaluated to true or false.
If the expr3 is evaluated to true, then statement3/s is executed.
If the expr3 is evaluated to false, then statement4/s is executed.
• The flow diagram is shown below:
Dept. of CSE, AJIET, Mangaluru. Page 17 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
/* Program to select and print the largest of the 3 numbers using nested “if-else” statements . */
#include<stdio.h>main()
{
int a,b,c;
printf(“Enter Three Values: \n”);
scanf(“%d %d %d ”, &a, &b,&c);
printf(“Largest Value is: ”) ;
if(a>b)
{
if(a>c)
printf(“ %d ”, a);
else
Dept. of CSE, AJIET, Mangaluru. Page 18 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
printf(“ %d ”, c);
}
else
{
if(b>c)
printf(“ %d”, b);
else
printf(“ %d”, c);
}
}
Output:
Enter Three Values: 7 8 6
Largest Value is: 8
THE else if ladder or cascaded if STATEMENT
• This is basically a “multi-way” decision statement.
• This is used when we must choose among many alternatives.
• The syntax is shown below:
if(expression1)
{ statement1/s; }
else if(expression2)
{ statement2/s; }
else if(expression3)
{ statement3/s }
Dept. of CSE, AJIET, Mangaluru. Page 19 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
else if(expression4)
{ statement4/s }
else
{ statement5/s }
• The expressions are evaluated in order (i.e. top to bottom).
• If an expression is evaluated to true, then
→ statement associated with the expression is executed &
→ control comes out of the entire else if ladder
• For ex, if exprression1 is evaluated to true, then statement1/s is executed.
If all the expressions are evaluated to false, the last statement4/s (default case) is executed.
The Ternary Operator:
The Ternary (? : ) operator can be used as a shortcut for an if…else statement. It is typically used as part
of a larger expression where an if…else statement would be awkward.
Dept. of CSE, AJIET, Mangaluru. Page 20 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Syntax: condition ? value_if_true : value_if_false;
OR variable = condition ? value_if_true : value_if_false;
The above entire expression returns value_if_true if condition is true, but value_if_false otherwise.
Example: if we wish to implement some C code to store the greater value to an variable,
we may use:
int result = (a > b) ? a : b;
instead of:
int result;
if(a>b)
result=a;
else
result=b;
THE switch STATEMENT
• This is basically a “multi-way” decision statement.
• This is used when we must choose among many alternatives.
• The syntax & flow diagram is shown below:
Dept. of CSE, AJIET, Mangaluru. Page 21 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
• Here, choice can be either any integer value or a character.
• Based on this integer value, the control is transferred to a particular case-value where necessary
statements are executed.
• During executing, if break statement is encountered, then the control comes out of the switch
block.
• If the value of the choice does not match with any of the case values (i.e. value1, value2, value3)
then control goes to default label.
• All case-values must be different.
/* Program to illustrate the use of switch statement. */
#include<stdio.h>
main()
{
char grade; // local variable definition printf(“enter grade A to D: \n”); scanf(“%c”,&grade);
switch(grade)
{
case 'A': printf("Excellent! \n ");
break;
case 'B': printf("Well done \n");
break;
case 'C': printf("You passed \n");
break;
case 'D': printf("Better try again\n ");
break;
default: printf("Invalid grade \n ");
return;
}
printf("Your grade is %c", grade);
Dept. of CSE, AJIET, Mangaluru. Page 22 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
BASIC CONCEPTS OF LOOP
• Let's consider a situation when you want to write a message “Welcome to C language” five times.
Here is a simple C program to do the same:
#include<stdio.h>
main()
{
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
• When the program is executed, it produces the above result.
• It was simple, but again let's consider another situation when you want to write the same message
thousand times, what you will do in such situation?
• Are we going to write printf() statement thousand times? No, not at all.
• C language provides a concept called loop, which helps in executing one or more statements up
to desired number of times.
• Loops are used to execute one or more statements repeatedly.
Dept. of CSE, AJIET, Mangaluru. Page 23 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
• The flow diagram :
• There are 3 types of loops in C programming:
1) while loop
2) for loop
3) do while loop
Pretest and Post-Test loops
• Programming languages allow us to check the loop control expressions either before or after each
iteration of the loop. In other words, we can have either a pre- or a posttest terminating condition.
• In a pretest loop, the condition is checked before we start and at the beginning of each iteration
after the first. If the test condition is true, we execute the code; if the test condition is false, we terminate
the loop.
Examples: for loop, while loop.
• In a post-test loop, we always execute the code at least once. At the completion of the loop code,
the loop control expression is tested. If the expression is true, the loop repeats; if the expression is false,
the loop terminates. The flowcharts in Fig show these two loop types.
Example: do…while
Dept. of CSE, AJIET, Mangaluru. Page 24 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Figure: Control Flow diagram
THE while LOOP
• A while loop statement can be used to execute a set of statements repeatedly as long as a given
condition is true.
• The syntax is shown below:
while(expression)
{
statement1/s;
}
• Firstly, the expression is evaluated to true or false.
• If the expression is evaluated to false, the control comes out of the loop without executing the
body of the loop.
• If the expression is evaluated to true, the body of the loop (i.e. statement1/s) is executed.
• After executing the body of the loop, control goes back to the beginning of the while statement
and expression is again evaluated to true or false. This cycle continues until expression becomes false.
• The flow diagram is shown below:
Dept. of CSE, AJIET, Mangaluru. Page 25 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
/*Program to calculate the sum of the first 10 numbers */
#include<stdio.h>
main()
{
inti=1,sum=0; while(i<=10)
{
sum=sum+i;
i=i+1;
}
printf(“\nSum = %d”,sum);
}
Output: Sum=55
/* Program to display a message 5 times using while statement. */
#include<stdio.h>
main()
{
int i=1;
while(i<=5)
{
printf(“Welcome to C language \n”);
i=i+1;
}
}
Output:
Welcome to C language
Welcome to C language
Dept. of CSE, AJIET, Mangaluru. Page 26 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Welcome to C language
Welcome to C language
Welcome to C language
THE for LOOP
• A for loop statement can be used to execute s set of statements repeatedly as long as a given
condition is true.
• The syntax is shown below:
for(expr1;expr2;expr3)
{
statement1/s;
}
• Here, expr1 contains initialization statement expr2 contains limit test expression expr3 contains
updating expression
• Firstly, expr1 is evaluated. It is executed only once.
• Then, expr2 is evaluated to true or false.
• If expr2 is evaluated to false, the control comes out of the loop without executing the body of the
loop.
• If expr2 is evaluated to true, the body of the loop (i.e. statement1/s) is executed.
• After executing the body of the loop, expr3 is evaluated.
• Then expr2 is again evaluated to true or false. This cycle continues until expression becomes
false.
The flow diagram is shown below:
Dept. of CSE, AJIET, Mangaluru. Page 27 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
/*Program to print numbers from m to n using for loops*/
#include<stdio.h>
main()
{
int m,n,i;
printf(“Enter the value of m:\n”);
scanf(“%d”,&m);
printf(“Enter the value of n:\n”);
scanf(“%d”,&n);
for(i=m; i<=n; i++)
{
printf(“\t%d”,i);
}
}
/* Program to display a message 5 times using for statement . */
#include<stdio.h> void main()
{
int i;
for(i=1; i<=5; i++)
{
printf(“Welcome to C language \n”);
}
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Dept. of CSE, AJIET, Mangaluru. Page 28 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Welcome to C language
Welcome to C language
THE do while STATEMENT
• When we do not know exactly how many times a set of statements have to be repeated, do-wile
statement can be used.
• The syntax is shown below:
do
{
statement1/s;
}while(expression); // note that while is terminated with semicolon (;) in do-while.
• Firstly, the body of the loop is executed. i.e. the body of the loop is executed at least once.
• Then, the expression is evaluated to true or false.
• If the expression is evaluated to true, the body of the loop (i.e. statement1/s) is executed
• After executing the body of the loop, the expression is again evaluated to true or false. This cycle
continues until expression becomes false.
The flow diagram is shown below:
/*Program to print the average of first n natural numbers using for loop*/
#include<stdio.h>
main()
{
int n,i,sum=0;
Dept. of CSE, AJIET, Mangaluru. Page 29 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
float avg=0.0;
printf(“Enter the value of m:\n”);
scanf(“%d”,&n);
i=1;
do
{
sum = sum+i;
i++;
}while(i<=n);
avg=sum/n;
printf(“\nThe sum of first n natural numbers = %d\n”,sum);
printf(“\nThe average of first n natural numbers = %f\n”,avg);
}
/* Program to display a message 5 times using do while statement. */
#include<stdio.h>
main()
{
int i=1;
do
{
printf(“Welcome to C language \n”); i=i+1;
}while(i<=5);
}
Output:
Welcome to C language
Welcome to C language
Dept. of CSE, AJIET, Mangaluru. Page 30 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
Welcome to C language
Welcome to C language
Welcome to C language
Comparision:
for loop while loop do…while loop
1.A for loop is used to execute A while loop is used to execute A do…while loop is used
a
and repeat a statement/s block and repeat a statement block to execute and repeat statement
depending on a condition at the depending on a condition which block depending on a condition
beginning of the loop. is evaluated at the beginning of which is evaluated at the end of
Example the loop. the loop.
for(i=1; i<=10; i++) Example Example
{ i=1; i=1;
s=s+i; while(i<=10) do
p=p*i; { {
} s=s+i; s=s+i;
p=p*i; p=p*i;
i++; i++;
} } while(i<=10);
2. A variable value is initialized A variable value is initialized at A variable value is initialized
at the beginning of the loop and
is the beginning or before the loop before the loop or assigned inside
used in the condition. and is used in the condition. the loop and is used in the
condition.
3. A statement to change the A statement to change the value A statement to change the value
value of of
of the condition or to increment the condition or to increment the the condition or to increment the
Dept. of CSE, AJIET, Mangaluru. Page 31 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
the value of the variable is given value of the variable is given at value of the variable is given at
at the beginning of the loop. the inside of the loop. the inside of the loop.
[Link] statement block will not be The statement block will not be The statement block will not be
executed when the value of the executed when the value of the executed when the value of the
condition is false. condition is false. condition is false, but the block
Is executed at least
once
irrespective of the value of the
condition.
5.A for loop is commonly A while loop is also widely A do…while loop is used in
used by many programmers. used by many programmers. some cases where the condition
need to be checked at the end of
the loop.
Jump statements:
Jump statements are the statements that transfer control from one part of the program to another. The
jump statements supported by C are follows:
i. break statements
ii. continue statements
iii. goto statements
THE break STATEMENT:
• The break statement is jump statement which can be used in switch statement and loops.
• The break statement works as shown below:
1) If break is executed in a switch block, the control comes out of the switch block and the statement
following the switch block will be executed.
2) If break is executed in a loop, the control comes out of the loop and the statement following the
loop will be executed.
Dept. of CSE, AJIET, Mangaluru. Page 32 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
The syntax is shown below:
/* program which uses break statement */
#include<stdio.h>
main()
{
int day;
printf(\nEnter any number from 1 to 7:”);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“\n SUNDAY”);
break;
case 2: printf(“\n MONDAY”);
break;
case 3: printf(“\n TUESDAY”);
Dept. of CSE, AJIET, Mangaluru. Page 33 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
break;
case 4: printf(“\n WEDNESDAY”); break;
case 5: printf(“\n THURSDAY”); break;
case 6: printf(“\n FRIDAY”); break;
case 7: printf(“\n SATURDAY”); break;
default: printf(“Invalid number has been entered”);
}
}
THE continue STATEMENT
• During execution of a loop, it may be necessary to skip a part of the loop based on some condition.
In such cases, we use continue statement.
• The continue statement is used only in the loop to terminate the current iteration.
The syntax is shown below:
Dept. of CSE, AJIET, Mangaluru. Page 34 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
* Program to read and add only positive numbers using continue statement. */
#include<stdio.h> void main()
{
int i=1, num, sum =0;
for(i=0; i< 5; i ++)
{
printf(“ Enter an integer:”); scanf( “%d”, &num);
if(num< 0)
{
printf(“you have entered a negative number \n”);
continue ; // skip the remaining part of loop
}
sum= sum+num;
}
printf(“The sum of the Positive Integers Entered = %d”, sum);
}
Output:
Enter an integer: 10
Enter an integer: -15
You have entered a negative number
Enter an integer: 15
Enter an integer: -100
You have entered a negative number
Enter an integer: 30
The sum of the positive integers entered = 55
Dept. of CSE, AJIET, Mangaluru. Page 35 of 36
INTRODUCTION TO C PROGRAMMING [1BPLC205E/105E] | Module 1
THE goto and label:
• The goto statement is used to transfer control to a specified label.
• The label is an identifier that specifies the place where the branch is to be made.
• Label can be any valid variable name that is followed by a colon ( : ).
• The label is placed immediately before the statement where the control is to be transferred.
The syntax is shown below:
goto label;
-------
-------
label :
/* Program to detect the entered number as to whether it is even or odd. Use goto statement. */
#include<stdio.h> void main()
{
int x;
printf(“Enter a Number: \n”); scanf(“%d”, &x);
if(x % 2 = = 0)
goto even;
else
goto odd;
even: printf(“%d is EvenNumber”);return;
odd : printf(“ %d is Odd Number”);
}
Output:
Enter a Number: 5 5 is Odd Number.
********************
Dept. of CSE, AJIET, Mangaluru. Page 36 of 36