CPF Notes
CPF Notes
TOPIC
1. Decision Making and
Branching
&
2. Decision Making and
Looping
1. Decision Making and
Branching
Syntax :
if (condition)
{
//block of code
}
Example 1 : Example 2:
Syntax :
if (condition)
{
// block of code
}
else
{
// block of code
}
Example 1 : Example 2 :
Syntax :
if (condition_1)
{
// block of code
}
else if (condition_2)
{
// block of code
}
else
{
// block of code
}
Example 1 : Example 2 :
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 :
Example 1 : Example 2 :
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.
while (condition)
{
// block of code
}
Syntax : Flowchart
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.
do while loop
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>