0% found this document useful (0 votes)
10 views38 pages

CP Notes2

The document explains decision control structures in C programming, focusing on control statements such as if, if-else, nested if-else, and switch statements. It also covers looping structures including for, while, and do-while loops, highlighting their syntax and use cases. Additionally, it discusses the advantages of loops, the use of break and continue statements, and the goto statement for transferring control within a program.

Uploaded by

pikachusaifi123
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)
10 views38 pages

CP Notes2

The document explains decision control structures in C programming, focusing on control statements such as if, if-else, nested if-else, and switch statements. It also covers looping structures including for, while, and do-while loops, highlighting their syntax and use cases. Additionally, it discusses the advantages of loops, the use of break and continue statements, and the goto statement for transferring control within a program.

Uploaded by

pikachusaifi123
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/ 38

Decision Control Structure(CO2)

In simple words, Control statements in C help the computer execute a certain


logical statement and decide whether to enable the control of the flow
through a certain set of statements or not. Also, it is used to direct the
execution of statements under certain conditions.
Types of Control Statements in C :-
• Decision-making control statements
1. Simple if statement
2. If-else statements
3. Nested if-else statements
4. else-if ladder

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)

• It allows the program to make a choice from alternative paths.

• C provide the following selection structures

– IF statement

– IF … ELSE statement

– Nested IF … ELSE statement

– IF … ELSE ladder

3
If Statement (CO2)

The if statement evaluates a condition. If the condition is


true (non-zero), the code inside the if block executes. If it's
false, the code is skipped.

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)

The if-else statement provides an alternative block of code to


execute if the condition is false.

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

•An if statement can be nested within another if statement, allowing


you to check multiple conditions in a structured manner.

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)

• It is used to execute some instructions several time based on some condition.

• 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?

• Here are some uses of loops in C:-


• Loops allow the user to execute the same set of
statements repeatedly without writing the same code multiple
times.
• It saves time and effort and increases the efficiency.
• It reduces the chance of getting errors during compilation.
• Loop makes the code readable and easier to understand, especially
when dealing with complex logic or large data sets.
• It promotes code reusability.
• Loops help traverse data structures like arrays or linked lists

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)
{

Body of the loop

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.

Program:- #include <stdio.h>

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

•Here, the initialization statement is executed first and only once.


•The test condition is checked, if false the loop terminates
•If the test condition is true, the body of the loop executes
•The update expression gets updated
•Again the test condition is evaluated
•The process repeats until the test condition becomes false.

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);

•The body of the loop executes before checking the condition.


•If the test condition is true, the loop body executes again.
•Again the test condition is evaluated.
•The process repeats until the test condition becomes false.

24
Do while loop

//Example: do...while loop in C:-


#include <stdio.h>
#include<conio.h>
int main()
{
int i = 0;
do
{
printf("%d\n", i+1);
i++;
} while (i < 10);
return 0;
}

•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.
}

// outer loop statements.


}

26
Nested Loop

Syntax
for (initi; cond; Inc/Dec)
{

for (initi; cond; Inc/Dec)


{

Body of the loop

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.

The working of the switch the statement goes like this:-

•It starts with the keyword switch, followed by a set of () containing


an expression.
•The expression is evaluated once and compared with the values of
each case.
•If any case matches, the corresponding case statement executes.
•After the execution of the matching case statement, the control comes out of
the switch statement due to the break keyword.
•If no case matches, the default statement, if present, gets executed

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

#include <stdio.h> case 5:


int main() { printf("Friday");
int day; break;
printf("Enter a number (1-7): "); case 6:
scanf("%d", &day); printf("Saturday");
break;
switch (day) { case 7:
case 1: printf("Sunday");
printf("Monday"); break;
break; default:
case 2: printf("Invalid input");
printf("Tuesday"); }
break; return 0;
case 3: }
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
32
Rules for switch Statement

Rules for the switch statement in C:

1.The data type of the switch expression must be of an integer or character.


2.All the case labels must be followed by a colon “:”.
3.The case the value must be an integer or character constant.
4.The break keyword is optional, but it can decrease the execution time of
your program. It will stop the unnecessary execution of other cases after a
match is found.

33
Break and continue Statement

34
Loops with break statement (CO2)

Syntax:

for (initi; condt; Inc/Dec)


{
…………
if(cond)
break;
…………
}

35
Loops with Continue statement (CO2)

When a continue statement is encountered inside a loop, the control is transferred to


the beginning .

Syntax:

for (initi; condt; Inc/Dec)


{
…………
if(cond)
continue;
…………
}

36
Goto statement (CO2)

The goto statement is known as jump statement in C. As the name


suggests, goto is used to transfer the program control to a predefined
label. The goto statment can be used to repeat some part of the code for a
particular condition. It can also be used to break the multiple loops which
can't be done by using a single break statement. However, using goto is
avoided these days since it makes the program less readable and
complicated.

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.}

When should we use goto?


The only condition in which using goto is preferable is when we need to break the
multiple loops using a single statement at the same time.

38

You might also like