1 Sem
1 Sem
# include<stdio.h >
void main( )
{
int numbers;
clrscr();
printf (“Type a number:”);
scanf (“%d”, & number);
if (number < 0)
{
number = – number;
}
printf (“The absolute value is % d \n”, number);
getch();
}
Example: C Program to check equivalence of two numbers using if statement
#include<conio.h>
#include<stdio.h>
void main()
{
int m,n;
clrscr();
printf(" \n enter two numbers:");
scanf(" %d %d", &m, &n);
if(m-n= = 0)
printf(" \n two numbers are equal");
getch();
}
Note: if statement will execute one statement bellow to it by default, if we want to execute more than
one statement, then those all statements we have to group in open and close Curly brackets { and }.
The if-else statement: The if-else statement is an extension of the simple if statement. If the test-
expression/condition is true, then true-block statements immediately following if statement are
executed otherwise the false-block statements are executed. In other case, either true-block or false-
block will be executed, not both.
Syntax:
if(test-expression/condition)
{
true-block statements;
}
else
{
false-block statements;
}
statement-x
Example: C program to read any number as input through the keyboard and find out whether it is
Odd Number or Even Number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the Number");
scanf("%d",&n);
if(n%2==0)
{
printf("This is Even Number");
}
else
{
printf("This is Odd Number");
}
getch();
}
Example: C program to find biggest among two numbers using if else.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two Number");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("The number a=%d is bigger”, a);
}
else
{
printf("The number b=%d is bigger”,b);
}
getch();
}
Nested if….else statement: C language supports if-else statements to test additional conditions
apart from the initial test expression. The if-else construct works in the same way as normal if
statement nested if construct is also know as if-else-if construct. When an if statement occurs within
another if statement, then such type of is called nested if statement.
Syntax:
if(test-condition-1)
{
if(test-condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(test-condition-3)
{
statement-3;
}
else
{
statement-4
}
}
statement-x
Example: C program if the ages of Ram, sham and Ajay are input through the keyboard, write a
program to determine the youngest of the three
#include< stdio.h >
#include< conio.h >
void main()
{
int ram,sham,ajay;
clrscr();
printf("Enter the Three Ages of Ram,Sham and Ajay\n");
scanf("%d%d%d",&ram,&sham,&ajay);
else
if(ram < sham)
{
{
if(sham < ajay)
if(ram < ajay)
{
{
printf("Sham is Youngest");
printf("Ram is Youngest");
}
}
else
else
{
{
printf("Ajay is Youngest");
printf("Ajay is Youngest");
}
}
}
}
getch();
}
else… if Ladder: There is another way of putting 'if's together when multipath decisions are
involved. A multipath decision is a chain of 'if's' in which the statement associated with each else is
an if and last else if’s else part contain only else.
The conditions are evaluated from the top to bottom. As soon as true condition is found, the
statement associated with it is executed and the control is transferred to statement-x(skipping the rest
of ladder) when all the conditions become false, then the final else containing the default statement
will be executed.
Syntax:
if(condition-1)
Statement-1;
else if(condition-2)
Statement -2;
else if(condition-3)
Statement -3;
else if(condition-n)
Statement -n;
else
Default Statement;
Statement -x;
Switch statement: A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked for each switch
case.
The following rules apply to a switch statement −
1. The expression used in a switch statement must have an integral or enumerated type, or be of
a class type in which the class has a single conversion function to an integral or enumerated
type.
2. You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon (:).
3. The constant-expression for a case must be the same data type as the variable in the switch,
and it must be a constant or a literal.
4. When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
5. When a break statement is reached, the switch terminates, and the flow of control jumps to
the next line following the switch statement.
6. Not every case needs to contain a break. If no break appears, the flow of control will follow
through to subsequent cases until a break is reached.
7. A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true.
No break is needed in the default case.
Syntax:
switch(expression)
{
case constant-expression-1 : statement(s);
break;
case constant-expression-2 : statement(s);
break;
.
.
.
case constant-expression-n : statement(s);
default : statement(s);
}
Example: C Program to find the roots of a quadratic equation using switch statement
#include<stdio.h>
#include<math.h>
void main()
{
int flag;
float x, x1, x2;
float a, b, c, d;
float rpart, ipart;
clrscr();
printf("\n Enter 3 numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a==0)
{
x=-b/c;
printf("\n Only root x : %f", x);
exit();
}
d=b*b-4*a*c;
if(d>0)
flag=1;
else if(d==0)
flag=2;
else
flag=3;
switch(flag)
{
case 1:printf("\n Real & Distinct roots are: ");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("\n x1=%f \n x2=%f", x1, x2);
break;
case 2:printf("\n Repeated roots are: ");
x1=-b/(2*a);
x2=x1;
printf("\n x1 & x1 : %f", x1);
break;
case 3: d=sqrt(abs(d));
rpart=-b/(2*a);
ipart=d/(2*a);
printf("\n Complex Roots are: ");
printf("\n x1=%f+i%f", rpart, ipart);
printf("\n x2=%f-i%f", rpart, ipart);
}
getch();
}
Continue: The continue statement skips some statements inside the loop. The continue statement is
used with decision making statement such as if...else.
Syntax of continue Statement
continue;
How continue statement works?
Goto: A goto statement in C programming provides an unconditional jump from the 'goto' to a
labeled statement in the same function.
NOTE − Use of goto statement is highly discouraged in any programming language because it makes
difficult to trace the control flow of a program, making the program hard to understand and hard to
modify. Any program that uses a goto can be rewritten to avoid them.
Syntax
The syntax for a goto statement in C is as follows −
goto label;
..
..
label: statement;
Here label can be any plain text except C keyword and it can be set anywhere in the C program above
or below to goto statement.
Flow Diagram
Forward Goto:
Backward Goto
Label:
………..
………..
goto label;
……….
Examples: Forward goto.
#include<stdio.h>
#include <conio.h>
int main()
{
printf (“Hello Worldn”);
goto Label;
printf(“How are you?”);
printf(“Are you Okey?”);
Label:
printf(“Hope you are fine”);
getch();
}
Return: The return statement terminates the execution of a function and returns control to the
calling function. Execution resumes in the calling function at the point immediately following the
call. A return statement can also return a value to the calling function.
Syntax:
return;
or
return(value);
C – Loop control statements: You may encounter situations, when a block of code needs to be
executed several number of times. In general, statements are executed sequentially: The first
statement in a function is executed first, followed by the second, and so on.
A loop statement allows us to execute a statement or group of statements multiple times.
In C programing loops are basically categorized into two categories.
1. Entry control loop
2. Exit control loop
Entry Control loop: An entry control loop checks the condition at the time of entry and if
condition or expression becomes true then control transfers into the body of the loop. Such type of
loop controls entry to the loop that’s why it is called entry control loop.
Flowchart:
Note: if condition fails very first time the body of the loop will not be executed.
Exit Control Loop: An Exit Control Loop checks the condition for exit and if given condition for
exit evaluate to true, control will exit from the loop body else control will enter again into the loop.
Such type of loop controls exit of the loop that’s why it is called exit control loop.
Flowchart:
Note:even if condition fails first time but body of loop will exicute minimum one time.
Working:
Step 1: First initialization happens and the counter variable gets initialized.
Step 2: In the second step the condition is checked, where the counter variable is tested for the given
condition, if the condition returns true then the C statements inside the body of for loop gets
executed, if the condition returns false then the for loop gets terminated and the control comes
out of the loop.
Step 3: After successful execution of statements inside the body of loop, the counter variable is
incremented or decremented, depending on the operation (++ or –).
Example: C Program to calculate the sum of first n natural numbers.
#include <stdio.h>
#include<conio.h>
void main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when n is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
getch();
}
While loop: while loop is a most basic loop in C programming. while loop has one control
condition, and executes as long the condition is true. The condition of the loop is tested before the
body of the loop is executed; hence it is called an entry-controlled loop.
Syntax:
Initialization of loop variable;
while (condition)
{
//body of loop
statement(s);
updation of loop variable;
}
Flowchart:
Working:
step1: The loop variable is initialized with some value and then it has been tested for the condition.
step2: If the condition returns true then the statements inside the body of while loop are executed else
control comes out of the loop.
step3: The value of loop variable is incremented/decremented then it has been tested again for the
loop condition.
while(k<=first/2)
{
if((first % k) == 0)
{
remark=1;
break;
}
k++;
}
if(remark==0)
printf("\n %d ",numbr);
++first;
}
getch();
}
do-while loop: is an exit controlled loop i.e. the condition is checked at the end of loop. It means
the statements inside do-while loop are executed at least once even if the condition is false. Do-while
loop is an variant of while loop.
Syntax:
Initialization of loop variable;
do
{
statement 1;
statement 2;
………….
statement n;
updation of loop variable;
}while (condition);
NOTE: We have to place semi-colon after the While condition.
Flowchart:
Working:
1. First we initialize our variables, next it will enter into the Do While loop.
2. It will execute the group of statements inside the loop.
3. Next we have to use Increment and Decrement Operator inside the loop to increment or decrements
the value.
4. Now it will check for the condition. If the condition is True, then the statements inside the do while
loop will be executed again. It will continue the process as long as the condition is True.
5. If the condition is False then it will exit from the loop.
Example: Write a C program to print the sum of all even and odd numbers up to n.
#include<stdio.h>
void main()
{
int n,s1=0,s2=0,i;
printf("Enter Number : ");
scanf("%d",&n);
i=1;
do
{
if(i%2==0)
s1=s1+i;
else
s2=s2+i;
i++;
}while(i<=n);
printf("\nSum of Even Numbers : %d\n",s1);
printf("\nSum of Odd Numbers : %d\n",s2);
getch();
}
Prof. Ravindra R Patil Programing in C and Data Structures 17PCD13/23 22
S J P N Trust's CSE
Hirasugar Institute of Technology, Nidasoshi. Academic
Inculcating Values, Promoting Prosperity Notes(Module-2)
Approved by AICTE and Affiliated to VTU Belgaum.
17PCD13/23
While Do While
Only one statement for all the Requires separate statement for
package to work all the while conditions.
While (condition) Do
{ {
statement; statements;
}. } while (condition);
Takes less time to execute but Takes more time to execute and
and the code is shorter. code becomes longer.