Unit 2 Notes C Programing
Unit 2 Notes C Programing
The Decision Making Statements are used to evaluate the one or more conditions and make the
decision whether to execute set of statement or not.
if statement
The if statement checks whether the text expression inside parenthesis () is true or not. If
the test expression is true, statement/s inside the body of if statement is executed but if test is
false, statement/s inside body of if is ignored.
Syntax
if (test_expression)
Flow chart
Example program
include <stdio.h>
int main()
int i = 10;
if (i > 15) {
Output:
I am Not in if
if – else statement
The if...else statement is used if the programmer wants to execute some statement/s when
the test expression is true and execute some other statement/s if the test expression is false.
Syntax
if (test expression)
else
Flow chart
Example program
#include <stdio.h>
int main()
{ int i = 20;
if (i < 15) {
else {
return 0;
Output:
i is greater than 15
The nested if...else statement is used when program requires more than one test
expression.
Syntax
if (condition1)
Flow chart
Example program
include <stdio.h>
int main()
int i = 10;
if (i < 15)
if (i < 12)
printf("i is smaller than 12 too\n");
else
} return 0;
Output:
i is smaller than 15
if-else-if ladder
Syntax:
if (condition)
statement;
else if (condition)
statement;
..
else statement;
Flow chart
Example program
#include <stdio.h>
int main()
{int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
Output:
i is 20
Switch Statement
Switch case statement evaluates a given expression and based on the evaluated value(matching a
certain condition), it executes the statements associated with it.
The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
The switch case statement is used for executing one condition from multiple conditions. It is
similar to an if-else-if ladder.
Syntax
switch(expression)
break; case
value2: statement_2;
break; . . .
break;
default: default_statement; }
Flow chart
Example program
#include <stdio.h>
int main ()
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
printf("Well done\n" );
break;
default :
printf("Invalid grade\n" ); }
return 0;
Output
Well done
Your grade is B
JUMP STATEMENTS
Break
continue
goto statements
Break: The break statement can be used in terminating all three loops for, while and do...while
loops.
Example program
#include<stdio.h>
#include<stdlib.h>
void main ()
int i;
printf("%d ",i);
if(i == 5)
break;
Output
Continue Statement
The continue statement is opposite to that of the break statement, instead of terminating the loop,
it forces to execute the next iteration of the loop.
When the continue statement is executed in the loop, the code inside the loop following the
continue statement will be skipped and the next iteration of the loop will begin.
#include <stdio.h>
int main()
if (i == 6)
continue;
else
return 0;
Output:
1 2 3 4 5 7 8 9 10
goto statement
In C programming, goto statement is used for altering the normal sequence of program
execution by transferring control to some other part of the program.
Syntax
goto label;
.............
.............
label:
statement;
Label is an identifier. When, the control of program reaches to goto statement, the control of the
program will jump to the label: and executes the code below it.
Example program
#include <stdio.h>
void printNumbers()
{ int n = 1;
label:
n++;
if (n <= 10)
int main()
printNumbers();
return 0;}
Output:
1 2 3 4 5 6 7 8 9 10
LOOP STATEMENT / ITERATIVE STATEMENTS
Entry Controlled loops: In Entry controlled loops the test condition is checked before entering
the main body of the loop. For Loop and While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the
loop body. The loop body will execute at least once, irrespective of whether the condition is true
or false. do-while Loop is Exit Controlled loop.
for Loop
The for loop in C language is used to iterate the statements or a part of the program
several times. It is frequently used to traverse the data structures like the array and linked list.
Syntax
//code to be executed }
Flow Chart
Example program
#include <stdio.h>
int main()
int i = 0;
} return 0;}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
while loop
The while loop checks whether the test expression is true or not. If it is true, code/s inside
the body of while loop is executed, that is, code/s inside the braces { } are executed. Then again
the test expression is checked whether test expression is true or not. This process continues until
the test expression becomes false.
Syntax
while (test_expression)
Flow chart
Example Program
#include <stdio.h>
int main () {
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
do-while loop
The do-while loop is similar to a while loop but the only difference lies in the do-while
loop test condition which is tested at the end of the body.In the do-while loop, the loop body
will execute at least once irrespective of the test condition.
Syntax
do
{
// body of do-while loop update_expression;
} while (test_expression);
Flow Chart
Example program
#include <stdio.h>
int main()
{ // Initialization expression
int i = 2;
do
{// loop body
printf( "Hello World\n"); // Update expression
i++; // Test expression
} while (i < 1);
return 0;
}
Output
Hello World
Nested Loops
Nesting of loops is the feature in C that allows the looping of statements inside another
loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for
defining any number of loops. The nesting level can be defined at n times.
You can define any type of loop inside another loop; for example, you can define 'while' loop
inside a 'for' loop.
Syntax
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Flow Chart
Example program
#include <stdio.h>
int main()
{
}}
return 0;
Output
Week: 1
Day:1
Day:2
Day:3
Day:4
Day:5
Day:6
Day:7
Block Statement
Example
#include <stdio.h>
int main()
int a=7
{ a=a+1,
Printf(“%d”, a);
return 0;
}
Storage classes
Storage classes in C are used to determine the lifetime, visibility, memory location, and initial
value of a variable. There are four types of storage classes in C
Automatic
External
Static
Register
Extern RAM Zero Global Till the end of the main program
Maybe declared anywhere in the
program
Static RAM Zero Local Till the end of the main program,
Retains value between multiple
functions call
Automatic
Example
#include <stdio.h>
int main()
{
auto int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.
return 0;
}
Output:
70 80 67
Static
The variables defined as static specifier can hold their value between the multiple
function calls.
Static local variables are visible only to the function or the block in which they are
defined.
A same static variable can be declared many times but can be assigned at only one time.
Default initial value of the static integral variable is 0 otherwise null.
The visibility of the static global variable is limited to the file in which it has declared.
The keyword used to define static variable is static.
Example
#include<stdio.h>
void display()
int a=a+10;
printf(“a=%d\n”,a);
int main()
{
For(i=1; i<=3; i++)
{display();
}return 0;
Output
a=20
a=30
a=40
Register
The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
We cannot dereference the register variables, i.e., we cannot use &operator for the
register variable.
The access time of the register variables is faster than the automatic variables.
The initial default value of the register local variables is 0.
The register keyword is used for the variable which should be stored in the CPU register.
However, it is compiler?s choice whether or not; the variables can be stored in the
register.
We can store pointers into the register, i.e., a register can store the address of a variable.
Static variables can not be stored into the register since we can not use more than one
storage specifier for the same variable.
Example
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register.
The initial default value of a is 0.
printf("%d",a);
}
Output: 0
External
The external storage class is used to tell the compiler that the variable defined as extern is
declared with an external linkage elsewhere in the program.
The variables declared as extern are not allocated any memory. It is only declaration and
intended to specify that the variable is declared elsewhere in the program.
The default initial value of external integral type is 0 otherwise null.
We can only initialize the extern variable globally, i.e., we can not initialize the external
variable within any block or method.
An external variable can be declared many times but can be initialized at only once.
If a variable is declared as external then the compiler searches for that variable to be
initialized somewhere in the program which may be extern or static. If it is not, then the
compiler will show an error.
Example 1
#include <stdio.h>
int main() In this program just declared the extern ‘a’ variable in
{ main functions but not define variable in globally
extern int a;
printf("%d",a);
}
Output
Example 2
#include <stdio.h>
int a; //variable a is defined globally
int main()
{
extern int a; // just declare a variable in main function,
printf("%d",a); // default value zero will be print in output
}
Output
0
Example 3
#include <stdio.h>
int a;
int main()
{
extern int a = 0; // this will show a compiler error since we can not use extern and initializer at s
ame time
printf("%d",a);
}
Output
Example 4
#include <stdio.h>
int main()
{
extern int a; // Compiler will search here for a variable a defined and initialized somewhere in th
e pogram or not.
printf("%d",a);
}
int a = 20;
Output
20
PART A
If
It is a conditional statement.
If the condition is true, it executes some statements.
If the condition is false then it stops the execution the statements.
While
It is a loop control statement.
Executes the statements within the while block if the condition is true.
If the condition is false the control is transferred to the next statement of the loop.
2) If else statement
4) Switch Statement
30.Write the Syntax for IF Statement?
if ( <expression> )
<statement>
31.Write the Syntax for IF- ELSE Statement?
if ( <expression> )
<statement 1> else
<statement 2>
32. Write the Syntax for NESTED- IF - ELSE Statement?
if ( <expression1> )
{
if(<expression2>)
{
<Statements>
}
else
{
<Statements>
}
}
else
<statement>
33. Write the Syntax for Switch Case Statement?
switch ( <expression> )
( <case list>
} where
<case list> is a sequence of
case <value>:
<statement list>
break; and optionally one
default:
<statement list>
break;
PART B
1. Explain the following (i) conditional statements. (ii) switch Statements (iIi)Nested if-else statement
2. Write about the need and types of looping statements in C language and discuss with examples.
3. Write about the need and types of branching statements in C language and discuss with examples.
4. List out the block statements available in C with an example?
5. Explain with syntax, if, if-else and nested if-else statements in, C‟ program
6. Write a program in „C‟ to find the sum of „n‟ natural numbers without using any loops
7. Write a C program to demonstrate the use of unconditional goto statement
8. Show how break and continue statements are used in a C program, with example
9. Write a calculator program in C language to do simple operations like addition, subtraction, multiplication
and division. Use switch statement in your program
10. Differentiate between the if statement and the while loop in c programming. Provide a simple code
example for each.
11. Write C programs to find the factorial of a number using do-while, where the number n is entered by user.
12. List the differences between while loop and do-while loop. Write a C program to find sum of Natura
numbers from 1 to N using for loop
13. Write a C program to find the biggest of three numbers
14. Explain with example, the meaning of statement and block in a C program
15. Explain briefly about the decision making statement with suitable example?
16. Write a program to check whether a given number is prime or not.
17. Write a C program to print the students grade of student.
18. Write a C program that takes three coefficients (a,b,and c) of a quadtatic equation ; (ax2+bx+c) as input
and compute all possible roots and print them with appropriate messages.
19. Write a C program to find GCD of two numbers using ternary operator and for loop
20. Write a C program that takes from user an arithmetic operator („+‟, „-„, „*‟, or „/‟) and two operands.
Perform corresponding arithmetic operation on the operands using switch statement
Case-Study Problems
Program
#include <stdio.h>
int main() {
// variable to store the given number
int n;
//take input from the user
scanf("%d", &n);
//if else condition to check whether the number is even or odd
if (n % 2 == 0) {
OUTPUT
14
Even and not divisible by 4