Cps Module 2 Notes
Cps Module 2 Notes
Cps Module 2 Notes
# 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()
{
intm,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
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.
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, writea
program to determine the youngest of the three
#include<stdio.h>
#include<conio.h>
voidmain()
{
intram,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;
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);
}
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.
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 callingfunction.
Syntax:
return;
or
return(value);
Note: if condition fails very first time the body of the loop will not be executed.
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()
{
intnum, 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();
}
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.
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();
}
A break causes the switch or loop A continue doesn't terminate the loop, it causes the loop to
statements to terminate the moment it go to the next iteration. All iterations of the loop are
is executed. Loop or switch ends executed even if continue is encountered. The continue
abruptly when break is encountered. statement is used to skip statements in the loop that appear
after the continue.
When a break statement is encountered, When a continue statement is encountered, it gets the control
it terminates the block and gets the to the next iteration of the loop.
control out of the switch or loop.
A break causes the innermost enclosing A continue inside a loop nested within a
loop or switch to be exited immediately. switch causes the next loop iteration.
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.