CP Notes2
CP Notes2
1
Decision Control Structure(CO2)
• Decision making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to be
false.
2
Selection Structure (CO2)
– IF statement
– IF … ELSE statement
– IF … ELSE ladder
3
If Statement (CO2)
Syntax: True
IF (condition is true)
If
{ condition
Statements;
Statements False
}
4
Sample Program (CO2)
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a;
clrscr( );
printf("\nEnter the number:");
scanf("%d",&a);
if(a>10)
{
printf(" \n a is greater than 10");
}
getch( );
}
5
If else statement (CO2)
True If False
Condition
Syntax:
IF (condition)
{ True False
True statements;
} statements statements
ELSE
{
False statements;
}
6
Sample Program (CO2)
#include<stdio.h>
#include <conio.h>
void main ( ){
int a;
clrscr( );
printf("\nEnter the number:");
scanf("%d",&a);
if(a>10)
{
printf(" \n a is greater than 10");
}
else
{
printf(" \n a is less than 10");
}
getch( );
}
7
Nested if else (CO2)
If
Condition
1
False
True
Statements
If
True False
Condition
2
True False
statements statements
8
Nested if else
Syntax:-
if (condition1) {
if (condition2) {
// code to execute if both condition1 and
condition2 are true
}
}
9
If else ladder (CO2)
The if-else ladder is used when there are multiple conditions to check in
sequence. It allows you to execute different blocks of code based on multiple
conditions.
Syntax:-
If (condition1)
{
statements;
}
else if (condition2)
{
statements;
}
else if (condition3)
{
statements;
}
else
{
statements;
}
10
Sample Program (CO2)
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3;
float avg;
printf("\nEnter the marks:");
scanf("%d%d%d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf("\n The average is:%f",avg);
printf("\n The Grade is:");
if(avg>=60)
{
printf("First class");
}
11
Sample Program (CO)
else if(avg>=50)
{
printf("Second class");
}
else if(avg>=35)
{
printf("Thrid class");
}
else
{
printf("Fail");
}
getch();
}
12
Looping Structure (CO2)
• Loops are a block of code that executes itself until the specified condition becomes
false. In this section, we will look in detail at the types of loops used in C
programming.
Types of Loop in C :
Let’s get into the three types of loops used in C programming.
1.for loop
2.while loop
3.do while loop
13
What is the Need for Looping Statements in C?
14
Advantages of Looping
• Loops are a powerful tool in programming that offer several advantages:
• Code Reusability:
– By using loops, you can avoid writing repetitive code. Instead of writing the same code
multiple times, you can write it once inside a loop and execute it as many times as needed.
– This significantly reduces the amount of code you need to write and maintain.
• Efficiency and Performance:
– Loops allow you to perform tasks efficiently by automating repetitive operations.
– This can lead to significant performance improvements, especially when dealing with large
datasets or complex calculations.
• Flexibility and Adaptability:
– Loops can be easily modified to control the number of iterations or the conditions under
which the loop continues.
– This flexibility allows you to adapt your code to different scenarios and requirements.
• Clear and Concise Code:
– Loops can make your code more readable and easier to understand.
– By condensing repetitive code into a concise loop, you can improve the overall clarity of your
program.
• Data Processing and Manipulation:
– Loops are essential for processing and manipulating data structures like arrays and lists.
– You can iterate over elements, perform operations on them, and make decisions based on
their values.
15
While and do while loop
While Loop:
•Condition Check: The condition is checked before the loop body is executed.
•Execution: If the condition is true, the loop body is executed.
•Termination: The loop continues to execute as long as the condition remains
true. Once the condition becomes false, the loop terminates.
Do-While Loop:
•Condition Check: The condition is checked after the loop body is executed.
•Execution: The loop body is executed at least once, regardless of the initial
condition.
•Termination: After the first iteration, the condition is checked. If it's true, the
loop body is executed again. This process continues until the condition becomes
false.
16
Main Key Difference:-
Key Difference:
The key difference lies in when the condition is checked. In a while loop, the
condition is checked at the beginning, so the loop may not execute even once if
the condition is initially false. In a do-while loop, the condition is checked at the
end, ensuring that the loop body is executed at least once.
// While Loop
int i = 10;
while (i > 0) {
printf("%d ", i);
i--;
}
// Do-While Loop
int j = 10;
do {
printf("%d ", j);
j--;
} while (j > 0);
17
for loop in C
A for loop is a control structure that enables a set of instructions to get executed
for a specified number of iterations. It is an entry-controlled loop.
Syntax:-
for (initialization; test condition; Increment/Decrement)
{
A for loop in C is a control flow statement that allows you to execute a block
of code repeatedly for a specific number of times. It's particularly useful when
you know in advance how many times you want to iterate.
18
For loop (CO2)
19
For loop in C declaration
•Initialization: This part is executed only once, at the beginning of the loop.
It's often used to declare and initialize a loop counter variable.
•Condition: This expression is evaluated before each iteration. If the condition
is true, the code inside the loop is executed. If it's false, the loop terminates.
•Increment/Decrement: This expression is executed after each iteration. It's
commonly used to update the loop counter variable.
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:-1 2 3 4 5 6 7 8 9 10 20
For loop in C declaration
Syntax
for(initialization;test-condition; inc/dec){
//code to be executed
21
While Loop (CO2)
Syntax:
.
WHILE (condition)
{ False
. condition
Body of the loop;
.
True
}
Body of The loop
22
While Loop (CO2)
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,fact=1,n;
printf("\nEnter the Number:");
scanf("%d",&n);
while(i<=n)
{
fact =fact *i;
i++; // i=i+1
}
printf("\n The value of %d! is:%d",n,fact);
getch();
}
23
Do while loop
• It is an exit-controlled loop. It prints the output at least once before checking the
condition. Afterwards, the condition is checked and the execution of the loop
begins.
Syntax:-
do{
//code to be executed
}while(test condition);
24
Do while loop
•The above code prints numbers from 1 to 10 using the do while loop in C.
•It prints 1 before checking if i less than 10.
•It checks the condition i<10 and executes until i becomes 10
25
Nested Loop
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements
inside another loop. Let's observe an example of nesting loops in C.
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 of Nested
Advertisementloop
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
26
Nested Loop
Syntax
for (initi; cond; Inc/Dec)
{
NOTE:-- The nested for loop means any type of loop which is defined inside the 'for' loop.
27
Difference between while and do while
28
Switch Statement in C
The switch statement is one of the most basic decision-making concepts in the C
programming language. Switch statement is a control flow component that facilitates
multi-way branching according to an expression's value. In C, the switch statement is
composed of the switch keyword, an expression enclosed in parenthesis, and several
case labels that specify potential values.
29
Case Structure (CO2)
Switch
Case 1
Case 2
Default
case
30
Case Structure (CO2)
Syntax:
switch (expression)
{
case constant 1:
block1;
break;
case constant 2:
block2;
break;
.
.
default :
default block;
break;
}
31
SWITCH Statement
33
Break and continue Statement
34
Loops with break statement (CO2)
Syntax:
35
Loops with Continue statement (CO2)
Syntax:
36
Goto statement (CO2)
Syntax:
label:
…………
…………
goto label;
…………
37
Goto statement example
1.#include <stdio.h>
2.int main()
3.{
4. int num,i=1;
5. printf("Enter the number whose table you want to print?");
6. scanf("%d",&num);
7. table:
8. printf("%d x %d = %d\n",num,i,num*i);
9. i++;
10. if(i<=10)
11. goto table;
12.}
38