0% found this document useful (0 votes)
14 views19 pages

CPF Notes

Uploaded by

ghanshyamp873
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)
14 views19 pages

CPF Notes

Uploaded by

ghanshyamp873
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/ 19

NOTES OF CPF

TOPIC
1. Decision Making and
Branching
&
2. Decision Making and
Looping
1. Decision Making and
Branching

Decision Making or Conditional Statement

What is Decision Making or Conditional Statement ?


It evaluates condition or logical expression first and based on its result (either true
or false), the control is transferred to particular statement (means to execute
particular code).

Decision Making Statements in C :

One way Decision : if (Also known as simple if)


Two way Decision : if…else
Two way Decision : ?: (Conditional Operator)
Multi way Decision : if…else if…else if…else
n-way Decision : switch…case
if statement

if is single branch decision making statement.


if condition is true then only body will be executed.

Syntax :

if (condition)
{
//block of code
}

Example 1 : Example 2:

#include <stdio.h> #include<stdio.h>


int main()
{ int main()
int x = 20; {
int y = 18; int a;
if (x > y) printf("Enter Number:");
{ scanf("%d",&a);
printf("x is greater than y.");
} if(a == 0)
return 0; {
} printf("Zero");
}
Output : return 0;
x is greater than y. }
Output :
Enter Number : 0
Zero
if...else statement
if…else is two branch decision making statement
if condition is true then true part will be executed else false part will be executed.
If body of if statement contains only one statement then { } are not compulsory.
But if body of if statement contains more than one statements then { } are
compulsory.

Syntax :
if (condition)
{
// block of code
}
else
{
// block of code
}
Example 1 : Example 2 :

#include <stdio.h> #include<stdio.h>

int main() int main()


{ {
int time = 20; int a, b;
printf("Enter Two Numbers:");
if (time < 18) { scanf("%d%d",&a,&b);
printf("Good day."); if(a > b)
} {
else printf("%d is largest.", a);
{ }
printf("Good evening."); else
} {
return 0; printf("%d is largest.", b);
} }
Output : return 0;
Good evening. }
Output :
Enter Two Numbers : 3 5
5 is largest.
if…else if …else statement (Ladder if ...else)(Multiple if...else)

if…else if…else if…else is multi branch decision making statement.

If first if condition is true then remaining if conditions will not be evaluated.


If first if condition is false then second if condition will be evaluated and if it is
true then remaining if conditions will not be evaluated.

if…else if…else if…else is also known as ladder if…else .

Syntax :
if (condition_1)
{
// block of code
}
else if (condition_2)
{
// block of code
}
else
{
// block of code
}
Example 1 : Example 2 :

#include <stdio.h> #include<stdio.h>

int main() int main()


{ {
int time = 16; int a;
if (time < 10) printf("Enter Number:");
{ scanf("%d",&a);
printf("Good morning.");
} if(a > 0)
else if (time < 20) printf("Positive Number");
{
printf("Good day."); else if(a= =0)
} printf("Zero");
else
{ else
printf("Good evening."); printf("Negative Number");
}
return 0; return 0;
} }
Output : Output :
Good day. Enter Number : -5
Negative Number
Nested if
Nested if statement, meaning one if statement inside of another, allows you to test
multiple criteria and increases the number of possible outcomes.

Syntax :

if (condition-1)
{
if (condition-2)
{
// block of code
}
else
{
// block of code
}
}
else
{
// block of code
}
Example 1 :

#include<stdio.h>

int main()
{
int a, b, c;
printf("Enter Three Numbers:");
scanf("%d%d%d",&a,&b,&c);

if(a>b)
{
if(a>c)
printf("%d is max",a);

else
printf("%d is max",c);
}

else
{
if(b>c)
printf("%d is max",b);
else
printf("%d is max",c);
}
return 0;
}
Output :
Enter Three Numbers : 7
9
8
9 is max
? : (Conditional Operator/ Short Hand If...Else / Ternary Operator)
The Conditional Operator is similar to the if-else.
It is also known as a ternary operator.
If condition is true, then first expression (before colon(:)) is performed, otherwise
second expression(after colon(:)) is performed.

Syntax :

variable = (condition)? Expression1 : Expression2;

Example 1 : Example 2 :

#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int time = 15; int a, b, max;
(time < 18) ? printf("Good day.") : printf("Good evening."); printf("Enter Two Numbers:");
return 0; scanf("%d%d",&a,&b);
}
max = a>b?a:b;
Output : printf("%d is largest",max);
Good day. return 0;
}

Output :
Enter Two Numbers : 5
6
6 is largest
Switch Statement
Instead of writing many if..else statements, you can use the switch statement.
How it works:
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 The break statement breaks out of the switch block and stops the execution.
 The default statement is optional, and specifies some code to run if there is no
case match.

Syntax
switch (expression) {
case x:
// block of code
break;
case y:
// block of code
break;
default:
// block of code
}

Example 1 : Example 2 :
#include <stdio.h>
#include<stdio.h>
int main()
{ int main()
int day; {
printf("Enter day number(1-7):"); int a,b;
scanf("%d",&day); int op;
printf("1.Addition\n 2.Subtraction\n 3.Multiplication\n
switch(day) 4.Division\n");
{ printf("Enter the values of a & b: ");
case 1: scanf("%d %d",&a,&b);
printf("Sunday");
break; printf("Enter your Choice :");
case 2: scanf("%d",&op);
printf("Monday");
break; switch(op)
case 3: {
printf("Tuesday"); case 1 :
break; printf("Sum of %d and %d is :%d",a,b,a+b);
case 4: break;
printf("Wednesday");
break; case 2 :
case 5: printf("Difference of %d and %d is : %d",a,b,a-b);
printf("Thursday"); break;
break;
case 6: case 3 :
printf("Friday"); printf("Multiplication of %d and %d is :%d",a,b,a*b);
break; break;
case 7:
printf("Saturday"); case 4 :
break; printf("Division of Two Numbers is %d :",a/b);
default: break;
printf("Wrong input");
} default :
return 0; printf(" Enter Your Correct Choice.");
} }
Output : return 0;
Enter day number(1-7):5 }
Thursday Output :
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 3
5
Enter your Choice :3
Multiplication of 3 and 5 is :15
goto statement
goto is an virtual loop.
The goto statement allows us to transfer control of the program to the specified
label.
Syntax Syntax
goto label; label:
….. statement;
….. …..
….. …..
…..
label:
statement; goto label;

The label is an identifier. When the goto statement is encountered, the control of
the program jumps to label: and starts executing the code.

Example 1 : (WAP to print Odd numbers Example 2 : (WAP to print the sum of 1
between 1 to n using goto statement) to N using goto statement)
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i=1,n; int i=1,sum = 0,n;
printf("Enter a number:"); printf("Enter value of N:");
scanf("%d",&n); scanf("%d",&n);
odd: next:
if(i%2!=0) sum = sum + i;
{ i += 1;
printf("%d,",i); if(i <= n)
} goto next;
i=i+1; printf("Sum = %d\n", sum);
if(i<=n) return 0;
{ }
Output :
goto odd;
Enter value of N:5
} Sum = 15
return 0;
}
Output :
Enter a number:25
1,3,5,7,9,11,13,15,17,19,21,23,25,
2. Decision Making and
Looping
Looping Statements or Iterative Statements

What is loop?
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more
readable.

Looping Statements are :


Entry Controlled Loop Exit Controlled Loop
Test condition is checked first, and then Loop body will be executed first, and
loop body will be executed. then condition is checked.
If Test condition is false, loop body will If Test condition is false, loop body will
not be executed. be executed once.
Examples : while loop , for loop Examples : do…while loop
Flowchart : Flowchart :
While loop :
while is an entry controlled loop.
Statements inside the body of while are repeatedly executed till the condition is
true.
Syntax : Flowchart

while (condition)
{
// block of code
}

Example 1 : (WAP to print 1 to n Example 2 : (WAP to find factors of


using while loop) a number using while loop)
#include<stdio.h> #include <stdio.h>

int main() int main()


{ {
int i,n; int i=1,n;
i=1; printf("Enter n to find factors=");
printf("Enter n:"); scanf("%d",&n);
scanf("%d",&n); while(i<=n)
{
while(i<=n) if(n%i==0)
{ printf("%d,",i);
printf("%d\n",i); i=i+1;
i=i+1;
} }
return 0; return 0;
} }
Output : Output :
Enter n:5 Enter n to find factors=6
1 1,2,3,6,
2
3
4
5
for Loop
for is an entry controlled loop
Statements inside the body of for are repeatedly executed till the condition is true.

Syntax : Flowchart

for (expression 1; expression 2; expression 3)


{
// block of code
}

In for loop,
Expression 1 is executed (one time) before the execution of the code block.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed.

Example 1 : (WAP to print numbers 1 Example 2 : (WAP to find factors of a


to n using for loop) number using for loop)
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i,n; int i,n;
printf("Enter a number:"); printf("Enter n to find factors=");
scanf("%d",&n); scanf("%d",&n);
for(i=1;i<=n;i++) for(i=1;i<=n;i++)
{ {
printf("%d\n",i); if(n%i==0)
} printf("%d,",i);
return 0; }
} return 0;
Output : }
Enter a number:6
Output :
1
2 Enter n to find factors=8
3
1,2,4,8,
4
5
6

do while loop

do while is an exit controlled loop.


Statements inside the body of do while are repeatedly executed till the condition is
true.
The loop will always be executed at least once, even if the condition is false,
because the code block is executed before the condition is tested.

Syntax : Flowchart

do {
// block of code
}
while (condition);
Example 1 : (WAP to print Odd numbers Example 2 : (WAP to find factors of
between 1 to n using do while loop) a number using do while loop)
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i=1,n; int i=1,n;
printf("Enter a number:"); printf("Enter a number:");
scanf("%d",&n); scanf("%d",&n);
do do
{ {
if(i%2!=0) if(n%i==0)
{ {
printf("%d,",i); printf("%d,",i);
} }
i=i+1; i=i+1;
} }
while(i<=n); while(i<=n);
return 0; return 0;
} }
Output : Output :
Enter a number:6 Enter a number:8
1,3,5, 1,2,4,8,
Break and Continue statement
The break statement can be used to The continue statement breaks one
jump out of a loop. iteration (in the loop), if a specified
condition occurs, and continues with the
next iteration in the loop.

Example : Example :
#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int i; int i;
for (i = 0; i < 10; i++) for (i = 0; i < 8; i++)
{ {
if (i == 4) if (i == 4)
{ {
break; continue;
} }
printf("%d\n", i); printf("%d\n", i);
} }
return 0; return 0;
} }
Output : Output :
0 0
1 1
2 2
3 3
5
6
7

You might also like