PSC Unit 2
PSC Unit 2
41
COURSE MATERIAL
UNIT 2
COURSE I B.TECH
SEMESTER 11
PREPARED BY A.REVATHI
(Faculty Name/s) Senior Assistant Professor
VERSION V-5
1 B. TECH_CSE-SEM 1
SVCE TIRUPATI BTECH_IT_SEM
41
1 B. TECH_CSE-SEM 1
SVCE TIRUPATI BTECH_IT_SEM
41
1. Course Objectives
The objectives of this course are to
To learn how to solve a given problem.
To illustrate the basic concepts of C programming language.
To discuss the concepts of Functions, Arrays, Pointers and Structures.
To familiar with Dynamic memory allocation concepts.
To apply concepts of structures and files to solve real word problems.
2. Prerequisites
Mathematics(Basics like prime number, factorial)
3. Syllabus
UNIT-II:
Control Statements:
Selection Statements- if and switch statements.
Iterative Statements: for, while and do-while statements.
Jump Statements: break and continue statements.
4. Courseoutcomes
The students will be able to
1|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
5. Co-PO / PSOMapping
PSC PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
CO1
3 2 2
CO2
3 2 3 3
CO3 3
2 3 2 3 2 3
6. Lecture Plan
Lecture No. Weeks Topics to be covered References
7. Activity BasedLearning
2|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
8. Lecture Notes
A statement is a part of your program that can be executed. That is, a statement
specifies an action. Statements generally contain expressions and end with a
semicolon. Statements that are written individually are called Single/Simple
statements. Statements that are written as a block are called Block/Compound
statements. A block begins with an open brace {and ends with a closing brace}.
a) Simple if Statement
The simple if statement contains only one condition, if the condition is true, it will
execute the statements thatare present between opening and closing braces.
Otherwise it will not execute those statements
3|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
Syntax: Example:
if(condition) /* A program to check whether a number is Less than 100
Single-statement or not */
OR #include<stdio.h>
if(condition) #include<conio.h>
{ void main()
Statement-block {
} int a;
clrscr();
printf("enter an integer ");
scanf("%d",&a);
if(a<=10)
printf("%d is less than 100",a);
getch();
}
b) if...else Statement If the condition is true, the block of if is executed; otherwise, the
block of else is executed
Syntax: Example:
if (condition) Write a program to check whether the given
{ number is a positive number or a negative
number
True Block Statements.
#include<stdio.h>
}
#include<conio.h>
else
void main()
{
{
FalseBlock Statements
int n;
}
clrscr();
printf("enter any number ");
scanf("%d",&n);
if(n>=0)
{
printf("\n %d is a positive number",n);
}
else
{
printf("\n %d is a negative number",n);
}
4|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
getch();
}
Syntax: Example:
if (condition) Write a program to finding greatest among
{ three numbers
if(condition) #include<stdio.h>
{ #include<conio.h>
} int a,b;
} clrscr();
{ scanf("%d%d",&a,&b);
} {
if(a>c)
printf(“ %d is greatest”,a);
else
printf(“ %d is greatest”,c);
}
else
{
if(b>c)
printf(“ %d is greatest”,b);
else
printf(“ %d is greatest”,c);
}
5|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
.
d) if..else Ladder Statement
The if-else ladder is used when multipath (multiple) decisions are involved.A
multipath decision is a chain of if-elsein which the statement associated with each
else is an if-statement.
Syntax: Example:
Write a program to test whether the given
number is a single digit or a double digit or a
if(condtion1) trible digit or more than three digits.
{
#include<stdio.h>
#include<conio.h>
statements1; void main()
} {
int n;
else if(condition2) clrscr();
{ printf("enter any number");
scanf("%d",&n);
statements2;
if(n>=0 && n<=9)
} printf("\n %d is a single digit number",n);
else if(condition3) else if(n>=10 && n<=99)
printf("\n %d is a double digit number",n);
{
else if(n>=100 && n<=999)
statements3; printf("\n %d is a trible digit number",n);
} else
printf("\n %d is more than three digits",n);
:
getch();
: }
else if(conditionN) }
{
statementsN;
}
else
{
statements;
}
6|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
7|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
case 'o':
printf("%c is a vowel ",ch);
break;
case 'u':
printf("%c is a vowel",ch);
break;
default:
printf("\n %c is not a
vowel",ch);
}
getch();
}
The expression must evaluate to an integer type. Thus, you can use character or
integer values, but floating- point expressions, for example, are not allowed. The
value of expression is tested against the values, one after another, of the constants
specified in the case statements. When a match is found, the statement sequence
associated with that case is executed until the break statement or the end of the
switch statement is reached. The default statement is executed if no matches are
found. The default is optional, and if it is not present, no action takes place if all
matches fail. Technically, the break statements inside the switch statement are
optional. They terminate the statement sequence associated with each constant. If
the break statement is omitted, execution will continue into the next case's
statements until either a break or the end of the switch is reached.
Difference between if & Switch
If statement Switch statement
Definition Depending on the condition in The user will decide which
the 'if' statement, 'if' and 'else' statement is to be executed.
blocks are executed.
Expression It contains either logical or It contains a single expression
equality expression. which can be either a
character or integer variable.
Evaluation It evaluates all types of data, It evaluates either an integer, or
such as integer, floating-point, character.
character or Boolean.
Sequence of First, the condition is checked. It executes one case after
8|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
execution If the condition is true then 'if' another till the break keyword is
block is executed otherwise not found, or the default
'else' block statement is executed.
Default If the condition is not true, If the value does not match
execution then by default, else block will with any case, then by default,
be executed. default statement is executed.
Editing Editing is not easy in the 'if-else' Cases in a switch statement are
statement. easy to maintain and modify.
Therefore, we can say that the
removal or editing of any case
will not interrupt the execution
of other cases.
Speed If there are multiple choices If we have multiple choices
implemented through 'if-else', then the switch statement is the
then the speed of the best option as the speed of the
execution will be slow. execution will be much higher
than 'if-else'.
9|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
getch();
}
The initialization is an assignment statement that is used to set the loop control
variable. The condition is a relational expression that determines when the loop exits.
The increment/decrement defines how the loop control variable changes each time
the loop is repeated.
2. do while loop
Do While is post tested/ exit controlled loop statement i.e first body of loop is
executed & finally condition is
checked. Even though condition is false, it will execute the body of loop statements
at least once.
Syntax: Example:
Write a program to print first 25 natural
Initialization; numbers
do ##include<stdio.h>
{ #include<conio.h>
void main()
Statements; {
Increment/Decrement operation; int n; clrscr(); n=1;
} while(test condition) ; do
{
printf("%3d",n);
n=n+1;
} while(n<=25);
getch();
}
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
void main() void main()
{ {
int n; clrscr( ); n=1; while(n>10) int n;
{ clrscr( );
printf(“%d\n”,n); n=1;
n=n+1; do
} {
getch( ); printf("%d",n);
} n=n+1;
Output: No output because condition is } while(n>10);
getch();
false
}
output: it prints 1 even though condition
is false
3. For loop
for is pre tested/ entry controlled loop statement i.e first condition is checked & body
of loop is executed.
Syntax: Example:
for(initialization; condition; increment / Write a program to print first 10 numbers
decrement operation;) #include<stdio.h>
{ #include<conio.h>
list of statements void main()
} {
int i;
clrscr();
for(i=1; i<=10; i++)
{
printf(“%d\t”,i);
}
getch();
}
However, if condition section is omitted, the for loop becomes an endless loop,
which is called an infinite loop. When the condition is absent, it is assumed to be true.
The for statement may have an initialization and increment/decrement sections. But
C programmers more commonly usefor ( ; ; )
1.3 JUMP STATEMENTS/ CONTROL TRANSFER STATEMENTS
C has four statements that perform either a conditional or unconditional branch:
return, goto, break, andcontinue. Of these, we can use return and goto
anywhere inside a function and the break and continue statements in
conjunction with any of the loop statements. We can also use break with switch.
1. return Statement
A return may or may not have a value associated with it. A return with a value can
11|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
be used only in a function
with a non-void return type. In this case, the value associated with return becomes
the return value of the function. A return without a value is used to return (exit) from a
void function.
The general form of the return statement is
return; (OR) return expression;
12|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
clrscr(); for(i=1; i<=10; i++)
for(i=1; i<=10; i++) {
{ if(i==6) continue; printf(“%d\t”,i);
if(i==6) break; printf(“%d\t”,i); }
} getch();
getch(); }
} output: 1 2 3 4 5 7 8 9 10
output: 1 2 3 4 5
4. goto Statement
The goto statement is used to branch from one point (statement) to another in the
program.
The goto statement requires a label in order to identify the place where the branch is
to be made. A label is a valid identifier followed by a colon. The label is placed
immediately before the statement where the control is to be transferred.
Furthermore, the label must be in the same function as the goto that uses it – we
can’t jump between functions.
The general form of goto is
label: goto label;
------- --------
-------- -------
goto label;
label:
13|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
clrscr();
for(i=1; i<=10; i++)
{
if(i==6) goto xyz; printf(“%d\t”,i);
}
xyz: printf(“\nthankyou\n”);
getch();
}
output: 1 2 3 4 5 thankyou
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
9. Practice Quiz
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
int main()
{
int a=25;
return 0;
}
A) 25 25 25
B) 25 26 27
C) 27 27 27
D) Compiler error
4. The continue statment cannot be used with
A. for
B. while
C. do while
D. switch
5. Which loop is guaranteed to execute at least one time.
A. for
B. while
C. do while
D. None of the above
10. Assignments
S.No Question BL CO
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
2 Explain all the loop statements with example programs 2 3
The function scanf() is used for formatted input from the standard input
and provides many of the conversion facilities. 1 3
It is used for formatted output to standard output device,( screen) . The
format specification contains string and the data to be output, as the
arguments(parameter) to the printf() function.
17|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
5 What are keywords? Give example.
Keywords have fixed meaning and these meaning cannot be
changed. These keywords can be used only for their intended purpose,
they cannot be used as programmer-defined identifiers.
1 3
The following are examples of few keywords are predefined by C
auto double int struct
break else if switch
case enum do while
6 Specify the syntax used for ‘for’ statement.
The for loop is entry controlled loop the provides a more concise loop
control structure. The general format of the loop is
1 3
for(intilialization;tescondition;increment/decrement)
{
Body of the loop;
}
7 What is a nested loop?
A nested loop is a loop that runs within another loop. Put it in another
sense, you have an inner loop that is inside an outer loop. In this 2 3
scenario, the inner loop is performed a number of times as specified by
the outer loop. For each turn on the outer loop, the inner loop is first
performed.
8 When is a "switch" statement preferable over an "if" statement?
18|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
to terminate the current iteration of the loop.
19|P S C - U N I T - 2
BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
2. B. A. Forouzan and R. F. Gilberg, Computer Science: A Structured
Programming Approach Using C, 3/e, Cengage Learning, 2007.
3. Brian W Kernighan and Dennis M Ritchie, The C Programming Language,
Second Edition, Prentice Hall Publication.
4. Paul Deitel, Harvey Deitel -C How to Program with an introduction to C++,
Eighth Edition
20|P S C - U N I T - 2
BTECH_CSE-SEM 11