0% found this document useful (0 votes)
18 views45 pages

CP Project

The document provides an overview of conditional statements and looping in C programming, detailing types of conditional statements such as if, if-else, else-if, nested if, and switch statements. It also explains the various looping constructs including for, while, and do-while loops, along with the usage of break and continue statements. The conclusion emphasizes the importance of mastering these concepts for efficient programming.

Uploaded by

Akshata Khatal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views45 pages

CP Project

The document provides an overview of conditional statements and looping in C programming, detailing types of conditional statements such as if, if-else, else-if, nested if, and switch statements. It also explains the various looping constructs including for, while, and do-while loops, along with the usage of break and continue statements. The conclusion emphasizes the importance of mastering these concepts for efficient programming.

Uploaded by

Akshata Khatal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

CONDITIONAL STATEMENTS

AND
LOOPING
IN C PROGRAMMING

1
CONDITIONAL STATEMENTS IN C
Conditional statements are fundamental in programming and allow for decision-making based on
conditions. In C programming, conditional statements enable you to execute different blocks of
code based on whether certain conditions are true or false.

Types of conditional statements:

(1) If statement
(2) If-else statement
(3) Else if statement
(4) Nested if statement
(5) Switch statement

2
if STATEMENT
The if statement executes the code only if the condition is 100% true.

SYNTAX:

if (condition)
{
// code to be executed if condition is true
}

3
PROGRAM

4
OUTPUT

5
if-else STATEMENT
It controls the flow of the program based on the condition of “if statement”. The only difference is
that it executes some statement code block if the expression is true. Otherwise it executes the else
statement code block. It is used to perform two operations for single condition.

SYNTAX: FLOWCHART OF if-else STATEMENT

if (condition)
{
// execute your code
}
else
{
// execute your code
}

6
PROGRAM

7
OUTPUT

CASE 1 CASE 2

8
else-if STATEMENT
It is used to perform multiple cases for different conditions. In this type there is one if condition and
multiple else if conditions and one else block.

SYNTAX: FLOWCHART

if (condition)
{
//statement;
}
else if (condition)
{
//statement 2;
}
else if (condition)
{
// statement 3;
}
else
{
// statement;
}
9
PROGRAM

10
OUTPUT

CASE 1 CASE 2 CASE 3

CASE 4

11
nested if STATEMENT
It plays an important role in C programming. It simply mean the use of conditional statements inside
another conditional statement.

SYNTAX:

if (condition 1)
{
if (condition 2)
{
// statement block executes when the Boolean test expression is true
}
}
else
{
//else statement block
}

12
PROGRAM

13
OUTPUT

CASE 1 CASE 2 CASE 3

14
switch STATEMENT
It is a statement that rests the value of a variable and compares it with multiple cases. Once the
case match is found, a block of statements associated with that particular case is executed. Each
switch case must include break keyword. The break keyword in each case indicates the end of a
particular.

SYNTAX: FLOWCHART

switch (expression)
{
case 1: statement 1;
break;
case 2: statement 2;
break;
case 3: statement 3;
break;
case n: statement n;
break;

default: statement;
break;
} 15
PROGRAM

16
17
OUTPUT
CASE 1 CASE 2 CASE 3

CASE 4

18
SWITCH STATEMENT V/S NESTED IF ELSE
STATEMENT
Both switch statements and nested if-else statements are used for decision-making In C
programming, but they are suitable for different use cases.

SWITCH STATEMENT NESTED IF ELSE STATEMENT

Syntax is more concise and easier to read when Can handle more complex conditions, including
there are multiple conditions based on a single ranges and logical operators.
variable.
Best suited for scenarios with multiple discrete Used for more complex and varied conditions. E.g:
values for a single variable. E.g: Menu selection, Scenarios involving multiple variables, ranges, or
day-of-week processing, etc. Limitations: Does logical conditions.
not support conditions like value > 5 or combined
conditions like (value == 2 && flag == 1).
Performance is generally better when there are Conditions are evaluated sequentially, which may
many cases to check. result in slower performance for large numbers of
conditions.

Only works with integers, characters and No limitations on conditions- supports ranges,
enumerations. complex logical expressions and different data
Cannot evaluate ranges or complex conditions. types. Becomes harder to debug and maintain as
the nesting grows.

Use switch for simplicity and performance in Use nested if-else for flexibility and complex
19
LOOPING IN C
In C programming, a loop is used to repeat a block of code until the specified condition is met.
C programming has 3 types of loops:

1. Entry controlled loops:


- for loop
- while loop

2. Exit controlled loops:


- do while loop

24
for LOOP Flow chart of FOR LOOP

SYNTAX OF FOR LOOP:-

for (initialization; condition; increment/decrement)


{

// Code to execute

25
PROGRAM
To print numbers 1 to 5 :

26
OUTPUT

23
PROGRAM
To print “Hello World” n number of times:

24
OUTPUT

25
while LOOP
While loop does not depend upon the number of iterations. In for loop the number of iterations
was previously known to us but in the while loop the execution is terminated on the basis of the
test condition. If the test condition will become false then it will break from the while loop else
body will be executed.
FLOW CHART OF WHILE LOOP
SYNTAX OF WHILE LOOP:-

Initialization-expression;
while (test-expression)
{

// Code to be executed
Update expression;

26
PROGRAM

27
OUTPUT

28
PROGRAM
To calculate the sum of natural numbers up to a user-defined number n:

29
OUTPUT

30
do while LOOP
The do while loop is similar to a while loop but the only difference lies in the do while loop test
condition which is tested at the end of the body. In the do while loop, the loop body will be
executed at least once irrespective of the test condition.

SYNTAX:- FLOWCHART OF DO WHILE LOOP

initialization
do
{

// Code to be executed
update expression;

}
while (test expression)

31
PROGRAM

32
OUTPUT

33
PROGRAM

34
OUTPUT

CASE 1

CASE 2

35
DO WHILE V/S WHILE
In C programming, do-while and while loops are used to execute a block of code repeatedly, but they
differ in when the condition is checked.

DO WHILE WHILE

Syntax: Syntax:

The block of code is executed at least once, The condition is checked first. If the condition is
regardless of the condition. After executing the true, the block of code is executed. The process
code, the condition is checked. If the condition is repeats until the condition becomes false.
true, the loop continues. If false, the loop stops.
The block is guaranteed to execute at least once, The loop might not execute even once if the
even if the condition is false. condition is false initially.

36
CONCEPT OF INFINITE LOOP

The loop will continue to execute forever if there is no way provided for
termination called infinite loop. Break or exit(0) function are used to
terminate this infinite loop. So if you don’t provide a mechanism for
termination then you will receive infinite loop as an output.

To stop the infinite loop in between press Ctrl+C keys on the keyboard

37
BREAK
The break statement in C is used to terminate a loop or switch statement prematurely, meaning it
exits the current loop or switch before its natural completion.

Use Cases:
1.In Loops (for, while, do-while): To exit the loop immediately, regardless of the loop's
condition.

2.In switch Statements: To exit the switch block after executing a case. Without break, the
program will continue executing subsequent case blocks (known as "fall-through").

 How break Works in Loops: When break is encountered inside a loop, it stops the current
loop's execution and transfers control to the statement immediately following the loop.

 How break Works in switch Statements: Inside a switch, break is used to terminate a case.
Without it, the program will execute all the subsequent case blocks until it encounters a break
or the end of the switch.

38
PROGRAM OF BREAK
WITH LOOP OUTPUT

39
PROGRAM OF BREAK
WITH SWITCH OUTPUT

40
CONTINUE
The continue statement in C is used to skip the current iteration of a loop and proceed to the next
iteration, effectively by passing the code that comes after it within the loop.

When continue is encountered in a loop:

he rest of the statements in the current iteration are skipped.


he loop proceeds to evaluate its condition (or move to the next iteration).

41
PROGRAM OF CONTINUE
WITH for LOOP OUTPUT

42
BREAK V/S CONTINUE
gramming, both break and continue are control flow statements, but they serve different purposes within lo

BREAK CONTINUE

Terminates the loop entirely and exits it Skips the rest of the current iteration and moves to the
next iteration of the loop.
Exits the nearest enclosing loop (or a switch statement) Does not terminate the loop but skips over certain steps.
immediately.
Use Case: When you want to stop the loop based on a Use Case: When you want to skip specific iterations but
specific condition. continue looping.

43
CONCLUSION

In conclusion, looping and conditional statements are fundamental


building blocks of programming in C. They provide powerful tools to
implement decision-making and repetitive tasks efficiently. By
mastering these concepts, you can write robust and optimized code
that forms the foundation for solving complex problems. Remember,
practice is key to fully leveraging the potential of these constructs in
real-world applications. Thank you!

44
THANK YOU !

By
Ninad Kathe (5)
Samiksha Karkera (3)
Mayuresh Kharat (12)
Aryan Kudalkar (20)
Akshata Khatal (14)
Shruti Khedekar (16)
45

You might also like