0% found this document useful (0 votes)
12 views46 pages

CP Unit 2

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)
12 views46 pages

CP Unit 2

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/ 46

RAGHU

ENGINEERING COLLEGE
(AUTONOMOUS | VISAKHAPATNAM)

AR23 I B.Tech I Semester


INTRODUCTION TO PROGRAMMING
UNIT-II 1
Syllabus
Control Statements
Control Flow, Conditional Branching Statements: if, if-else, if-else-if,
switch. Basic Loop Structures: while, do-while loops, for loop, nested
loops, The Break and Continue Statements, goto statement.
Control Flow:
• In C programs, statements are executed sequentially in the order in
which they appear in the program.
• But sometimes we may want to use a condition for executing only a
part of program. Also many situations arise where we may want to
execute some statements several times.
• Control statements enable us to specify the order in which the various
instructions in the program are to be executed. This determines the
flow of control. Control statements define how the control is
transferred to other parts of the program.

Introduction to Programming Raghu Engineering College (A) 3


• C language supports-
I. Conditional Branching Statements / Selection Statements :
a) if
b) if-else
c) Nesting of if...else
d) if-else-if
e) switch
II. Basic Loop Structures / Iteration Statements :
a) while loop
b) do-while loop
c) for loop
III. Unconditional Statements / Jump Statements :
a) break
b) continue
c) goto
Introduction to Programming Raghu Engineering College (A) 4
I. Conditional Branching Statements / Selection Statements :
a) Simple if or Null else:
• This is a unidirectional conditional control statement.
• A set of statements is executed if the condition is true. If the condition
is false, the code inside the if block is skipped.
• Syntax:
if(condition)
{
Statement 1;
}
Next Statement;

Introduction to Programming Raghu Engineering College (A) 5


Example: Program to print a message if negative number is entered

Introduction to Programming Raghu Engineering College (A) 6


b) if-else:
This is a bi-directional conditional control statement.
This statement is used to test a condition and take one of the two possible
actions.
➢ If the condition is true, then a single statement or a block of statements
is executed (inside of if).
➢ Otherwise another single statement or a block of statements is executed
(inside of else).
Syntax:
if(condition) {
statement 1;
}
else {
statement 2;
}
Next Statement;
Introduction to Programming Raghu Engineering College (A) 7
Example: Program to print the larger and smaller of the two numbers

Introduction to Programming Raghu Engineering College (A) 8


Class Exercises:
1. Write a C program to check whether a number is divisible by 5 and 11
or not.
2. Write a C program to find maximum between two numbers.
3. Write a C program to check whether a number is even or odd.

Introduction to Programming Raghu Engineering College (A) 9


c) Nesting of if...else:
• We can have another if...else statement in the if block or the else
block. This is called nesting of if...else statements.
• Syntax:

Introduction to Programming Raghu Engineering College (A) 10


Example: Program to find largest number from three given numbers

Introduction to Programming Raghu Engineering College (A) 11


d) if-else-if:
• It allows us to test multiple conditions sequentially, and as soon as one
of the conditions is true, the corresponding block of code associated
with that condition is executed, and the rest of the ladder is skipped.
• Syntax:

Introduction to Programming Raghu Engineering College (A) 12


Example:

Introduction to Programming Raghu Engineering College (A) 13


Introduction to Programming Raghu Engineering College (A) 14
e) switch:
• This is a multi-directional conditional control statement. Sometimes there is
a need in program to make a choice among a number of alternatives. For
making this choice, we use the switch statement. This can be written as.
• Syntax:
switch(expression)
{
case constant1: Block_A1;
break;
case constant2: Block_A2;
break;
……….
case constantN: Block_AN;
break;
default: Block_D;
}
Out_of_switch statements;
Introduction to Programming Raghu Engineering College (A) 15
• Firstly, the switch expression is evaluated, then the value of this
expression is compared one by one with every case constant.
• If the value of the expression matches with any case constant, then
all statements under that particular case are executed. If none of the
case constants matches with the value of the expression, then the
block of statements under 'default' is executed.
• 'default' is optional; if it is not present and no case matches, then no
action takes place.
• These cases and default can occur in any order.

Introduction to Programming Raghu Engineering College (A) 16


• Switch block expression can be any integer or character constant also.
• The constants following the case keywords should be of integer or
character type. They can be either constants or constant expressions.
These constants must be different from one another.
• We can't use floating-point or string constants.
• Multiple constants in a single case are not allowed; each case should
be followed by only one constant.
• Each case can be followed by any number of statements. It is also
possible that a case has no statement under it.
• The statements under case can be any valid C statements like if-else,
while, for, or even another switch statement.
• Writing a switch statement inside another is called nesting of switches.

Introduction to Programming Raghu Engineering College (A) 17


• Now we'll see some valid and invalid ways of writing switch expressions
and case constants.

Introduction to Programming Raghu Engineering College (A) 18


Example: Program to perform arithmetic calculations on integers

Introduction to Programming Raghu Engineering College (A) 19


Class Exercises:
1. Write a C program to input any alphabet and print vowel or consonant.
2. Write a C program to input week number and print week day.
3. Write a C program to check whether a number is negative, positive or
zero.

Introduction to Programming Raghu Engineering College (A) 20


II. Basic Loop Structures / Iteration Statements :
• Loops are used when we want to execute a part of the program or a
block of statements several times.
• With the help of a loop, we can execute a part of the program
repeatedly as long as some condition is true.
• There are three loop statements in C -
a) while
b) do while
c) for

Introduction to Programming Raghu Engineering College (A) 21


a) while loop:
• The while loop continues to execute as long as the condition remains
true, and it terminates when the condition becomes false.
• Syntax:
while(condition)
{
Body of loop;
}
Next statement out of loop;

Introduction to Programming Raghu Engineering College (A) 22


• First, the condition is evaluated; if it is true, then the statements in the
body of the loop are executed. After the execution, again the condition
is checked, and if it is found to be true, then again the statements in the
body of the loop are executed.
• This means that these statements are executed continuously as long as
the condition is true, and when it becomes false, the loop terminates,
and the control comes out of the loop.
• Each execution of the loop body is known as an iteration.

Introduction to Programming Raghu Engineering College (A) 23


Example: Program to print the numbers from 1 to 10 using while loop

• Here, initially, the condition (i <= 10) is true. After each iteration of
the loop, the value of 'i' increases. When the value of 'i' equals 11, the
condition becomes false, and the loop terminates.
Introduction to Programming Raghu Engineering College (A) 24
b) do while loop:
• The 'do...while' statement is also used for looping. The body of this loop
may contain a single statement or a block of statements.
• The syntax for writing this loop is:
do
{
Body of loop;
-----
} while(condition);
Next statement out of loop;
• Here, firstly, the statements inside the loop body are
executed, and then the condition is evaluated. If the
condition is true, then again the loop body is executed, and
this process continues until the condition becomes false.
• Note that, unlike the while loop, here a semicolon is
placed after the condition.
Introduction to Programming Raghu Engineering College (A) 25
Example: Program to print the numbers from 1 to 10 using do while loop

• Generally while loop is used more frequently than the do while loop
but some situations may arise when it is better to check the condition
at the bottom of loop.
Introduction to Programming Raghu Engineering College (A) 26
Class Exercises:
1. Write a C program to print numbers from 1 to 10.
2. Write a C program to find sum of digits of any number
3. Write a C program to print numbers from 1 to 20 with a difference of 2
(Example:1 3 5…)

Introduction to Programming Raghu Engineering College (A) 27


c) for loop:
• The 'for' statement is very useful while programming in C.
• It has three expressions, and semicolons are used for separating these
expressions.
• The 'for' statement can be written as:
for(Initialization_expression; condition; Update_expression)
{
Body of loop;
}
Next statement out of loop;

Introduction to Programming Raghu Engineering College (A) 28


• Initialization expression is executed only once when the loop starts and is used to
initialize the loop variables. This expression is generally an assignment expression.
• Condition is tested before each iteration of the loop. This condition generally uses
relational and logical operators.
• Update expression is executed each time after the body of the loop is executed.
• Now, let us see how this loop works.
➢Firstly, the initialization expression is executed, and the loop variables are
initialized.
➢And then the condition is checked. If the condition is true, then the body of the
loop is executed. After executing the loop body, control transfers to update
expression
➢Update expression modifies the loop variables.
➢Then again, the condition is checked, and if it is true, the body of the loop is
executed. This process continues until the condition is true, and when the
condition becomes false, the loop is terminated, and control is transferred to the
statement following the loop.
Introduction to Programming Raghu Engineering College (A) 29
The work done by the for loop can be performed by writing a while loop as-
for(Initialization_expression; condition; Update_expression)
{
Body of loop;
}

Initialization expression;
while(condition)
{
Body of loop;
Update expression;
}

Introduction to Programming Raghu Engineering College (A) 30


Example: Program to print the numbers from 1 to 10 using for loop

Introduction to Programming Raghu Engineering College (A) 31


Nesting of Loops:
• When a loop is written inside the body of another loop, then it is
known as nesting of loops. Any type of loop can be nested inside any
other type of loop.
• For example, a 'for' loop may be nested inside another 'for' loop or
inside a 'while' or 'do-while' loop.

Introduction to Programming Raghu Engineering College (A) 32


Example: Program to understand nesting in for loop

Here for each iteration of the outer loop, the inner loop is executed 4 times.

Introduction to Programming Raghu Engineering College (A) 33


Class Exercises:
1. Write a C program to find multiplication of digits of any number
2. Write a C program to print armstrong numbers between 100 to 999
3. Write a C program to print numbers in reverse order from 10 to 1 with
a difference of 2 (Example: 10 8 6….)

Introduction to Programming Raghu Engineering College (A) 34


Infinite Loops:
• The loops that go on executing infinitely and never terminate are called
infinite loops.
• Let us take some examples and see what types of mistakes lead to
infinite loops.
A) for (i = 0; i <= 5; i--)
printf("%d",i);
This loop will execute till the value of 'i' is less than or equal to 5, i.e.,
the loop will terminate only when 'i' becomes greater than 5. The
initial value of 'i' is 0, and after each iteration, its value is decreasing,
hence it will never become greater than 5. So, the loop condition will
never become false, and the loop will go on executing infinitely.

Introduction to Programming Raghu Engineering College (A) 35


B) int k=1;
do
{
printf("%d",k);
sum=sum+k;
}while(k<5);
Here, we are not changing the value of 'k' inside the loop body, and hence the loop
becomes infinite.
C) while(n=2)
{
…..
}
A common mistake made by beginners is to use the assignment operator (=) where
the equality operator (==) should have been used. If this mistake is made in the loop
condition, then it may cause the loop to execute infinitely. For example, consider this
loop:
Introduction to Programming Raghu Engineering College (A) 36
D) int i=1;
while(i<=5);
printf("%d",i++);
This loop will produce no output and will go on executing infinitely.
The mistake here is that we have put a semicolon after the condition.
• These were some of the examples where infinite loops occur due to
mistakes, but sometimes infinite loops are intentionally used in
programs.

Introduction to Programming Raghu Engineering College (A) 37


III. Unconditional Statements / Jump Statements :
a) Break statement:
• The break statement is used inside loops and switch statements.
Sometimes it becomes necessary to come out of the loop even before
the loop condition becomes false. In such a situation, the break
statement is used to terminate the loop.
• This statement causes an immediate exit from that loop in which this
statement appears.
• It can be written as:
break;

Introduction to Programming Raghu Engineering College (A) 38


• When the break statement is encountered, the loop is terminated, and the
control is transferred to the statement immediately after the loop.
• The break statement is generally written along with a condition. If break
is written inside a nested loop structure, then it causes an exit from the
innermost loop.

Introduction to Programming Raghu Engineering College (A) 39


Example: Program to understand the use of break
#include<stdio.h>
main( )
{
int n;
for(n=1; n<=5; n++)
{
if(n==3)
{
printf("I understand the use of break\n").;
break;
} Output:
Number=1
printf("Number %d\n", n) ;
Number=2
} I understand the use of break
printf("Out of for loop\n"); Out of for loop
}
Introduction to Programming Raghu Engineering College (A) 40
b) continue statement:
• The continue statement is used when we want to go to the next iteration
of the loop after skipping some statements of the current iteration of the
loop.
• This continue statement can be written simply as-
continue;
• It is generally used with a condition. When continue statement is
encountered all the remaining statements after continue in the current
iteration are not executed and the loop continues with the next iteration.
• The difference between break and continue is that when break is
encountered, the loop terminates, and the control is transferred to the
next statement following the loop. But when a continue statement is
encountered, the loop is not terminated, and the control is transferred to
the beginning of the loop.

Introduction to Programming Raghu Engineering College (A) 41


Introduction to Programming Raghu Engineering College (A) 42
Example: Program to understand the use of continue statement
#include<stdio.h>
main( )
{
int n;
for(n=1; n<=5; n++)
{
if(n==3)
{
printf("I understand the use of continue\n").;
continue; Output:
} Number=1
printf("Number %d\n", n) ; Number=2
} I understand the use of continue
printf("Out of for loop\n"); Number=4
} Number=5
Out of for loop
Introduction to Programming Raghu Engineering College (A) 43
c) goto:
• This is an unconditional control statement that transfers the flow of
control to another part of the program.
• The goto statement can be used as-
goto label;
....
....
label:
statement;
....
....
• Here label is any valid C identifier and it is followed by a colon(:).

Introduction to Programming Raghu Engineering College (A) 44


Example: Program to print whether the number is even or odd

• The label can be placed anywhere.


• If the label is after the goto, then the control
is transferred forward, and it is known as a
forward jump or forward goto.
• If the label is before the goto, then the control
is transferred backward, and it is known as a
backward jump or backward goto.
• In a forward goto, the statements between
goto and the label will not be executed, and
in a backward goto, statements between goto
and the label will be executed repeatedly.

Introduction to Programming Raghu Engineering College (A) 45


Class Exercises:
1. Write a C program to check whether a number is prime or not
2. Write a C program to print Hello world for 10 times, but skip printing
if the number of iteration is multiple of 3
3. Write a C program to display odd numbers from from 1 to 100, stop
the process if the current number is divisible by 77

Introduction to Programming Raghu Engineering College (A) 46

You might also like