0% found this document useful (0 votes)
43 views138 pages

CS Unit - 2

This document discusses various operators in C programming language including arithmetic, relational, logical, assignment, increment and decrement operators. It provides examples of using each operator type and explains the expected output. Key concepts covered are the different categories of operators in C, how they are used in expressions, truth tables for logical operators and the precedence of operators during expression evaluation. Examples of using each operator type in simple C code snippets are given to demonstrate their functionality.

Uploaded by

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

CS Unit - 2

This document discusses various operators in C programming language including arithmetic, relational, logical, assignment, increment and decrement operators. It provides examples of using each operator type and explains the expected output. Key concepts covered are the different categories of operators in C, how they are used in expressions, truth tables for logical operators and the precedence of operators during expression evaluation. Examples of using each operator type in simple C code snippets are given to demonstrate their functionality.

Uploaded by

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

M.S.

Ramaiah Institute of Technology


(Autonomous Institute, Affiliated to VTU)
Department of Computer Science and Engineering

Subject Name: Fundamental of Computing


Subject Code: CS16
Term: ODD SEMESTER 2020
UNIT-2

Faculty:
Darshana A Naik
Shilpa H
Sunitha R S

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Unit 2
Operators and Expressions: Arithmetic Operators, Relational
Operators, Logical Operators, Assignment Operators, Increment and
Decrement Operators, Conditional Operators, Arithmetic
Expressions, Evaluation of Expressions, Precedence of Arithmetic
Operators, Type Conversions in Expressions, Operator Precedence
and Associatively. Control Structures in C: Algorithm Development,
Decision Making and Branching: Simple IF statement, IF..Else
Statement, Nesting of IF...Else, The Else IF Ladder, The Switch
Statements. The GOTO Statement. Decision Making and Looping:
Introduction, The While Statement, The DO statement, The FOR
statement, Jumps in Loops.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Operators and Expressions
 C supports a rich set of operators. Operators are
used in programs to manipulate data and variables.
 They usually form a part of the mathematical of
logical expressions.

3
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Categories of Operators
 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
 Increment and Decrement operators
 Conditional operators
 Bitwise operators
 Special operators
4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ARITHMETIC OPERATORS
 The operators are
 + (Addition)
 - (Subtraction)

 * (Multiplication)

 / (Division)

 % (Modulo division)

 The Division operator produces the quotient.


 The modulo division produces the remainder of an integer division.
 The modulo division operator cannot be used on floating point
data.
 Note: C does not have any operator for exponentiation

5
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Integer Arithmetic
 When both the operands in a single arithmetic expression
are integers, the expression is called an integer expression
, and the operation is called integer arithmetic.
 If a=14 and b=4, then
 a-b=10
 a+b=18
 a*b=56
 a/b=3
 a%b=2
6
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Integer Arithmetic
 What is the results of the following?

 6/7=?
 3/7=?
 21/3=?

7
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Integer Arithmetic
 During modulo division the sign of the result is
always the sign of the first operand.
 -14 % 3 = -2
 -14 % -3 = -2
 14 % -3 = 2

8
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Real Arithmetic
 An arithmetic operation involving only real
operands is called real arithmetic.
 If x and y are floats then we will have
 1) x = 6.0 / 7.0 = 0.857143
 2) y = 1.0 / 3.0 = 0.333333

 The operator % cannot be used with real


operands.
9
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Mixed-mode Arithmetic
 When one of the operands is real and the other is
integer, the expression is called a mixedmode
arithmetic expression and its result is always a
real number.
 Eg: 1) 15 / 10.0 = 1.5

10
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
C Program for the arithmetic
operators
 What is the output of the following?

11
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
RELATIONAL OPERATORS
 Comparisons can be done with the help of
relational operators.
 The expression containing a relational operator is
termed as a relational expression.
 The value of a relational expression is either one
or zero.

12
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
RELATIONAL OPERATORS
 1) < (is less than)
 2) <= (is less than or equal to)
 3) > (is greater than)
 4) >= (is greater than or equal to)
 5) = = (is equal to)
 6) != (is not equal to)

13
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Relational Operators example in
C

14
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Relational Operators example in
C

15
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LOGICAL OPERATORS
 Following are three logical operators.
 && (logical AND)
 || (logical OR)
 ! (logical NOT)
 Eg:
 1) if(age>55 && sal<1000)
 2) if(number<0 || number>100)
16
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Truth Table

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


LOGICAL OPERATORS
 If c = 5 and d = 2 then, expression
 ((c == 5) && (d > 5)) equals to 0.
 If c = 5 and d = 2 then, expression
 ((c == 5) || (d > 5)) equals to 1.
 If c = 5 then, expression
 ! (c == 5) equals to 0.

18
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LOGICAL OPERATORS-
Example

19
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LOGICAL OPERATORS-
Example- Output

Output

20
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ASSIGNMENT OPERATORS
 The usual assignment operator is ‘=’.
 In addition, C has a set of ‘shorthand’ assignment
operators.
 Eg: x += y+1;
 This is same as the statement x=x+(y+1);

21
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ASSIGNMENT OPERATORS
 a + =1  a = a + 1
 a - = 1 a = a – 1
 a *= n + 1  a = a * (n+1)
 a /= n + 1  a = a / (n+1)
 a %= b  a = a % b

22
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND
DECREMENT OPERATORS
 These are the increment and decrement operator:
 ++ and --
 The operator ++ adds 1 to the operands while -- subtracts
1.
 It takes the following form:
 ++m; or m++
 --m; or m—

 x= 4++; // gives error, because 4 is constant

23
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=i++;
printf("x: %d",x);
printf("i: %d",i);
}

24
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;

x=i++;
Output:
printf("x: %d",x);
printf("i: %d",i);
10
}
11
25
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=++i;
printf("x: %d",x);
printf("i: %d",i);
}

26
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;

x=++i;
Output:
printf("x: %d",x);
printf("i: %d",i);
11
}
11
27
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
}

28
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;

x=i--;
Output:
printf("x: %d",x);
printf("i: %d",i);
10
}
9
29
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
}

30
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;

x=--i;
Output:
printf("x: %d",x);
printf("i: %d",i);
9
}
9
31
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,a,b,c; a = 2;
b = 4;
c = 5;
x = a-- + b++ -++c;
printf("x: %d",x);
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
94
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{

int x,a,b,c; a = 2;
b = 4;
c = 5;

x = a-- + b++ -++c;


Output: 0
printf("x: %d",x);
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


95
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{

int x,a,b,c; a = 2;
b = 4;
c = 5;

x = a-- + b++ -++c;


Output: 0
printf("x: %d",x);
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


34
Increment and decrement
operators
void main()
{
int a=5;
a=a+1=5+1=6
printf(“a: %d",++a); a=a=6
printf(“a: %d", a++);
}
a=a=6 //Displayed
a=a+1=7

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


35
Increment and decrement
operators
void main()
{
int a=5;
printf(“a: %d",++a); a:6
printf(“a: %d", a++);
printf(“a: %d", a); a:6
}

a:7

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


36
Exercise-What is the output?
#include <stdio.h> int main()
{
int a=5;
printf("a: %d\n",++a, a++); return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


37
Exercise-What is the output?
#include <stdio.h> int main()
{
int a=5;
printf("a: %d\n",++a, a++); return 0;

O/p: a:7
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
38
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("a: %d\n",a--);
printf("a: %d\n",--a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
39
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("a: %d\n",a--); a:5
printf("a: %d\n",--a); a:3

return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
40
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5,x;
x=a--;
printf("a: %d\n",x);
printf("a: %d\n",a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
41
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5,x;
x=a--;
printf("a: %d\n",x);
printf("a: %d\n",a); a:5
a:4
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
42
Exercise-What is the output?
#include <stdio.h> int
main()
{
int a=5,x;
x=a--;
printf("a: %d\n",a);
printf("a: %d\n",x);
x=--a;
printf("a: %d\n",a);
printf("a: %d\n",x);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
43
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5,x;
x=a--;
a:4
printf("a: %d\n",a); a:5
printf("a: %d\n",x); a:3
x=--a; a:3
printf("a: %d\n",a);
printf("a: %d\n",x);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
44
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d",a--,a++,--a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
45
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d",a--,a++,--a); return a:5
0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
46
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d%d",a--,a++,--a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
47
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d%d",a--,a++,--a);
return 0;
a:5, 4
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
48
Exercise-What are the values of n1,
n2, n3 and n4 at the end?
int n1,n2,n3,n4;
n1 = 1;
n2 = ++n1;
n3 = ++n1;
n4 = n1++;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


49
Exercise-What are the values of n1,
n2, n3 and n4 at the end?
int n1,n2,n3,n4;
n1 = 1;
n2 = ++n1;
n3 = ++n1;
n4 = n1++;

n1: 4 n2: 2 n3:3 n4: 3

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


50
TYPE CONVERSION IN EXPRESSIONS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


51
Automatic Type Casting
1. char and short operands are converted to int
2. Lower data types are converted to the higher data types and result
is of higher type. Hierarchy
3. The conversions between unsigned and signed Double float
types may not yield intuitive results. long Int
4. Example
Short and char
float f; double d; long l;
int i; short s;
d + f f will be converted to double
i / s s will be converted to int
l / i i is converted to long; long result

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


52
Assignment type casting
 If types of the operand in an assignment expression
is different, then the operand on the right-hand side
will be converted to the type of left-hand operand
according to the following rules.
 int i = 'z';
 i=122;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


53
Explicit Type Casting
 The general form of a type casting operator is
 (type-name) expression

 It is generally a good practice to use explicit casts than to rely on automatic

type conversions.
 Example

C = (float)9 / 5 * ( f – 32 )
 float to int conversion causes truncation of fractional part

 double to float conversion causes rounding of digits

 long int to int causes dropping of the higher order bits.

116
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Example on implicit type casting
#include<stdio.h> main ( )
{
int i=17;
char c=’c’; int sum; sum=i+c;
printf(“Value of sum:%d\n”,sum);
}

Output: Value of sum: 116

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


55
Example on explicit type casting
With explicit Without explicit
int sum=17,count=5; float mean; int sum=17,count=5; float mean;
mean=(float)sum/count; printf("Value of mean=sum/count; printf("Value of
mean:%f\n",mean); mean:%f\n",mean);

Output:
Value of mean: 3.400000 Output:
Value of mean: 3.000000

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


56
Operator Precedence and Associativity
Operator precedence: It dictates the order of evaluation of
operators in an expression.
Associativity: It defines the order in which operators of the
same precedence are evaluated in an expression.
Associativity can be either from left to right or right to left.
Consider the following example:
24+5*4

Here we have two operators + and *, Which operation do


you think will be evaluated first, addition or multiplication?
If the addition is applied first then answer will be 116 and
if the multiplication is applied first answer will be 44. To
answer such question we need to consult the operator
precedence table.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Examples:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Examples:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Algorithm Development

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Structured Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Sequence

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Decision Making and Branching
•Simple if statement
•IF ELSE Statement
•Nesting of IF-else statement
•The else-if statement

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Simple if Statement
The statements inside the body of “if” only execute if the given condition returns true. If the
condition returns false then the statements inside “if” are skipped.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Syntax of Simple if statement

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


If else Statement
If condition returns true then the statements inside the body of “if” are executed and the
statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the
statements in “else” are executed.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Syntax of if else statement

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Nesting of if-else statement

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Syntax of nesting of if-else statement

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Syntax of else if ladder

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Nesting of else if ladder ladder

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Switch statement in C

Department of Computer Science and Engineering


The switch Multiple-Selection Structure
switch ( integer expression )
{
case constant1 :
statement(s)
break ;
case constant2 :
statement(s)
break ;

...
default: :
statement(s)
break ;
}

Department of Computer Science and Engineering


Case labels in switch
• Which of the following are valid usages of case labels.
– case 1+1: //valid
– case ‘A’://valid
– case 67://valid
– case var: //valid
– case num1: //valid
– case 10://valid
– case 20+20: //valid
– case 10.12 //invalid
– case 7.5//invalid

Department of Computer Science and Engineering


Programs
• Write and execute a C program to find
whether a given alphabet is vowel or
consonant using switch.

Department of Computer Science and Engineering


Programs

Department of Computer Science and Engineering


The goto Statement
• goto causes an unconditional jump to a labeled statement
somewhere in the current function.
• Form of a labeled statement.
label: statement
Where label is an identifier.

Department of Computer Science and Engineering


Goto Example
int main()
{
char ch;
int count=1;
read: printf(" Enter the character\n");
scanf("%c",&ch);
count++;
printf("Entered character is %c\n",ch); if(count<=3)
goto read; printf("End");
}

Department of Computer Science and Engineering


The goto Statement
• Write and execute a C program to evaluate the square root
for five numbers using goto statement.

Department of Computer Science and Engineering


The goto Statement
#include<stdio.h> #include<math.h> int main() {
int num;
int count=1;
read: printf(" Enter the number\n"); scanf("%d",&num);
if(count<=5)
{
count++;
printf(" The square root of %d is %f\n",num,sqrt(num));
goto read;
}

printf("End");
}

Department of Computer Science and Engineering


Loops – While, Do, For
• Repetition Statements
– While
– Do
– For

82
Department of Computer Science and Engineering
The while Statement
• A while statement has the following syntax:
while ( condition )
statement;
• If the condition is true, the statement is executed
• Then the condition is evaluated again, and if it is still true, the
statement is executed again
• The statement is executed repeatedly until the condition becomes
false

83
Department of Computer Science and Engineering
Logic of a while Loop

condition
evaluated

true false

statement

5
Department of Computer Science and Engineering
The while Statement
• An example of a while statement:
int count = 0; while (count < 2)
{
printf("Welcome to Java!");
count++;
}

• If the condition of a while loop is false initially, the


statement is never executed
• Therefore, the body of a while loop will execute
zero or more times

13
Department of Computer Science and Engineering
Trace while Loop
Initialize count

int count = 0; while (count < 2)


{

printf("Welcome to C!"); count++;


}

7
Department of Computer Science and Engineering
Trace while Loop, cont.
(count < 2) is true
int count = 0;
while (count < 2)
{
printf("Welcome to C!"); count++;
}

8
Department of Computer Science and Engineering
Trace while Loop, cont.
Print Welcome to Java
int count = 0; while
(count < 2)
{
printf("Welcome to C!");
count++;
}

9
Department of Computer Science and Engineering
Trace while Loop, cont.
Increase count by 1
int count = 0; while (count < 2) count is 1 now

{
printf("Welcome to C!");

count++;
}

10
Department of Computer Science and Engineering
Trace while Loop, cont.
(count < 2) is still true since count
int count = 0; is 1

while (count < 2)


{
printf("Welcome to C!"); count+
+;
}

11
Department of Computer Science and Engineering
Trace while Loop, cont.
Print Welcome to Java
int count = 0; while
(count < 2)
{
printf("Welcome to C!");
count++;
}

12
Department of Computer Science and Engineering
Trace while Loop, cont.
Increase count by 1
int count = 0; while (count < 2) count is 2 now

{
printf("Welcome to C!");

count++;
}

13
Department of Computer Science and Engineering
animation

Trace while Loop, cont.


(count < 2) is false since count is 2
int count = 0; now

while (count < 2)


{
printf("Welcome to C!"); count++;
}

14
Department of Computer Science and Engineering
Trace while Loop
The loop exits. Execute the next
int count = 0; while (count < 2) statement after the loop.

{
printf("Welcome to C!"); count++;
}

15
Department of Computer Science and Engineering
The do-while Statement
• Syntax
do action
while (condition)
• How it works:
– execute action action
– if condition is true then execute action
again
– repeat this process until condition
evaluates to false.
• action is either a single statement or a true
group of statements within braces. condition

false

Department of Computer Science and Engineering


Trace do while Loop
Initialize count

int count = 0; do
{

printf("Welcome to C!"); count++;


} while (count < 2) ;

7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
Welcome to C!

printf("Welcome to C!");
count++;
} while (count < 2) ;

7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!");

count=1
count++;
} while (count < 2) ;

7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!"); count++;

1<2
} while (count < 2) ;

7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{

Welcome to C!
printf("Welcome to C!"); count+
+;
} while (count < 2) ;

7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!");
Count=2

count++;
} while (count < 2) ;

7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!"); count++;

2<2
} while (count < 2) ;

7
Department of Computer Science and Engineering
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
printf(count);
count = count - 1;
}

• This loop will continue executing until the user externally


interrupts the program.

103
Department of Computer Science and Engineering
Nested Loops
• How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 3)
{
count2 = 1;
while (count2 <= 3)
{
printf ("Here");
count2++;
}
count1++;
}

104
Department of Computer Science and Engineering
What is the output of the following code?

#include<stdio.h> void
main()
{
int val=1; do{
val++;
++val;
}while(val++>25);

printf("%d\n",val);
}

Department of Computer Science and Engineering


What is the output of the following code?

#include<stdio.h> void
main()
{
int val=1; do{
val++;
++val;
}while(val++>25);

printf("%d\n",val);
}

Answer: 4

Department of Computer Science and Engineering


What is the output of the following code?

#include<stdio.h> int
main()
{
int val=1; while(val<=5)
printf("Hi"); val++;
}

Department of Computer Science and Engineering


What is the output of the following code?

#include<stdio.h> int main()


{
int val=1; while(val<=5)
printf("Hi"); val++;
}
HiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHiHi……………..

Department of Computer Science and Engineering


What is the output of the following code?

#include<stdio.h>
Hi
int main() Hi
{ Hi
Hi
int val=1; Hi
while(val<=5)
{
printf("Hi"); val++;
}
}

Department of Computer Science and Engineering


What is the output of the following code?

#include<stdio.h> int
main()
{
int val=1;
while(val<=5);
{
printf("Hi"); val++;
}
}

Department of Computer Science and Engineering


Programs
• Write and execute a C program to read a number using
keyboard, check whether a given number is palindrome or
not using a while loop and display the result.

Department of Computer Science and Engineering


Programs
#include <stdio.h> int main()
{ int n, rev = 0, remainder, originalInteger; printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n; // reversed integer is stored in variable while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder; n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger); else
printf("%d is not a palindrome.", originalInteger); return 0;
}

Department of Computer Science and Engineering


For Loop
• Syntax
• Example

Department of Computer Science and Engineering


Programs
• Write and execute a C program to read numbers using
keyboard, find the sum of odd numbers and even numbers
in first n natural numbers using a for loop.

Department of Computer Science and Engineering


Programs
• Write and execute a C program to read a number using
keyboard, find the factorial of a number using for loop and
display the result.

Department of Computer Science and Engineering


Programs
• Write and execute a C program to print.
1
12
123
1234
12345

Department of Computer Science and Engineering


Programs
• Write and execute a C program to print.
#include <stdio.h>

void main( )
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
}

Department of Computer Science and Engineering


Programs
• Write a program in C to display the cube of the number upto given an
integer.

Input number of terms : 5

Expected Output :
Number is : 1 and cube of the 1 is :1 Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27 Number is : 4 and cube of the 4
is :64 Number is : 5 and cube of the 5 is :125

Department of Computer Science and Engineering


Solution
#include <stdio.h>
void main() {
int i,ctr;
printf("Input number of terms : ");
scanf("%d", &ctr); for(i=1;i<=ctr;i++) {
printf("Number is : %d and cube of the
%d is :%d \n",i,i, (i*i*i));
}
}

Department of Computer Science and Engineering


Programs
• Write a program in C to display the
multiplication table of a given integer.

Department of Computer Science and Engineering


#include <stdio.h>
void main()
{
int j,n;
printf("Input the number (Table to be calculated) : "); scanf("%d",&n);
printf("\n"); for(j=1;j<=10;j++)
{
printf("%d X %d = %d \n",n,j,n*j);
}
}

Department of Computer Science and Engineering


Programs
• Write and execute a C program to read two numbers using keyboard,
find GCD and LCM of two numbers using a do-while loop and display
the result.

Department of Computer Science and Engineering


Jumps in Loops
• The continue statement is used inside loops.
• When a continue statement is encountered inside a loop,
control jumps to the beginning of the loop for next iteration,
– skipping the execution of statements inside the body of loop for the
current iteration.

Department of Computer Science and Engineering


Continue statement

Department of Computer Science and Engineering


Continue Example
int main()
{
for (int j=0; j<=8; j++)
{
if (j==4)
continue; printf("%d ", j); Output
} return 0;
} 01235678

Department of Computer Science and Engineering


Continue Example
int main()
{
int counter=10; while (counter >=0)
{
if (counter==7)
{
counter--; continue;
}
printf("%d ", counter);
counter--; Output
}

10 9 8 6 5 4 3 2 1 0
return 0;
}

Department of Computer Science and Engineering


Continue Example- Guess the output
int main()
{
int j=0; do
{
if (j==7)
{
j++;
continue;
}
printf("%d ", j);
j++;
}while(j<10);

return 0;
}

Department of Computer Science and Engineering


Continue Example
int main()
{
int j=0; do
{
if (j==7)
{
j++;
continue;

}
printf("%d ", j);
j++;
}while(j<10); return 012345689
0;
}

Department of Computer Science and Engineering


C – break statement
• It is used to come out of the loop instantly.
• When a break statement is encountered inside a loop, the control
directly comes out of loop and the loop gets terminated.
• It is used with if statement, whenever used inside loop.
• This can also be used in switch case control structure. Whenever it
is encountered in switch- case block, the control comes out of the
switch- case.

Department of Computer Science and Engineering


C – break statement

Department of Computer Science and Engineering


Break Example- Guess the output
int main()
{
int num =0; while(num<=100)
{
printf("value of variable num is: %d\n", num);
if (num==2)
{
break;
}
num++;
}
printf("Out of while-loop");

return 0;
}

Department of Computer Science and Engineering


Break Example
int main()
{
int num =0; while(num<=100)
{
printf("value of variable num is: %d\n", num);
if (num==2)

{
break; Value of variable num is 0 Value of
} variable num is 1 Value of variable
num++;
} num is 2 Out of while loop
printf("Out of while-loop");
return 0;
}

Department of Computer Science and Engineering


Break Example- Guess the output
int main()
{
int var;
for (var =100; var>=10; var --)
{
printf("var: %d\n", var);
if (var==99)
{
break;
}
}
printf("Out of for-loop"); return 0;
}

Department of Computer Science and Engineering


Break Example- Guess the output
int main()
{
int var;
for (var =100; var>=10; var --)
{
printf("var: %d\n", var);
if (var==99)
{
break; var: 100
} var: 99 Out of for loop
}
printf("Out of for-loop"); return
0;
}

Department of Computer Science and Engineering


Interesting examples in for loop
main()
{
int num=10; for(;--num;)
printf(“%d”,num);
}

987654321
Department of Computer Science and Engineering
Interesting examples in for loop
main()
{
int i; for(i=0;i<5;i++);
printf("%d ",i);
}

6
Department of Computer Science and Engineering
Interesting examples in for loop
#include<stdio.h> main()
{
2 1
int x=1,y=1; 3 1
for(;y;printf("%d %d\n",x,y)) 4 1
5 1
y = x++ <= 5; 6 1
} 7 0

Department of Computer Science and Engineering


Thank you

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

You might also like