0% found this document useful (0 votes)
2 views

Procedural Programming_Unit_2_Decision & Loop Control Structure-1

CS engineering

Uploaded by

parivnems
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Procedural Programming_Unit_2_Decision & Loop Control Structure-1

CS engineering

Uploaded by

parivnems
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit-2 Control Structures in C

Unit 2 - Control Structure in C


Decision Control Structures in C
• Control Statement are program statements that are cause a jump of control from one part
of program to another part of program.
• The statements are classified into two types:
o Branching Statements
o Looping Statements

if
statements ForLoop
ifelse
statements
Switch
stateme Do..While While
Loop Loop
nestedif elseif
statements ladder

Branching Looping

Branching Statements:
• In decision making statements, group of statements are executed when the condition is
true. If condition is false, then else part statements are executed.
• Thereare3typesofdecision-makingcontrolstatementsinClanguage.Theyare,
➢ if statements
➢ if else statements
➢ nested if statements
➢ if else if ladder
➢ switch statements

if statement
The if statement in C is a conditional statement that allows you to control the flow of
your program based on a specified condition. The basic syntax of the if statement is as
follows:

1
Unit-2 Control Structures in C

Example:

If-else statement
The if-else statement allows you to create a branching structure based on a condition.
The if-else statement is fundamental for creating decision-making structures in C, allowing
you to execute different blocks of code based on various conditions. The basic syntax of the
if-else statement is as follows:

2
Unit-2 Control Structures in C

Flow Chart if else statement:

Example:

Nested if statements

Nested if statements are used to create more complex decision-making structures by


placing one if statement inside another. Nested if statements are useful to check additional
conditions based on the result of an outer condition.

3
Unit-2 Control Structures in C

Here's the basic syntax of nested if statements:

The inner if statements are nested within the code blocks of the outer if and else
statements. The inner if statements are only evaluated if their corresponding outer conditions
(condition1 and condition1 in this case) are true.

1. The outer if statement checks condition1.


2. If condition1 is true, the code block inside the first set of curly braces {} is executed.
3. Inside this block, there's another if statement (if (condition2)) that checks a second
condition.
4. The code inside the inner if statement's block is executed only if both condition1 and
condition2 are true.
5. The code following the inner if statement's block is executed regardless of the truth
value of condition2.
6. If condition1 is false, the else block is executed.
7. Inside the else block, there's another if statement (if (condition3)) that checks a third
condition.
8. The code inside the inner if statement's block is executed only if condition1 is false
and condition3 is true.
9. The code following the inner if statement's block is executed regardless of the truth
value of condition3.

4
Unit-2 Control Structures in C

Flow Chart Nested if statements

Example:

5
Unit-2 Control Structures in C

If else ladder "if-else ladder" refers to a series of if and else statements that are chained
together to handle multiple conditions in a hierarchical manner. This construct is useful when
several mutually exclusive conditions are there, and to execute the block of code associated with
the first true condition.
Here's the basic syntax of an if-else ladder:

Flow Chart if else ladder /else if statements:

6
Unit-2 Control Structures in C

Example to find the given number is positive, negative or equal to zero:

switch statement

The switch statement is a control flow statement that provides a way to select one of
many code blocks to be executed. It is often used as an alternative to a series of

7
Unit-2 Control Structures in C
if-else
statements when you have multiple conditions to check. The basic syntax of a switch
statement is as follows:

Working of switch cases

• expression : This is the value or variable that is being compared with the constants in

8
Unit-2 Control Structures in C

the case labels.


• case constant1: The case labels specify the values or constants that expression is
compared against. If expression matches a case constant, the corresponding code
block is executed.
• break: The break statement is used to exit the switch statement. If it's omitted, the
program will continue executing the code in subsequent case blocks until a break is
encountered or the switch statement ends.
• default: If none of the case constants match the value of expression, the code inside
the default block is executed. The default block is optional.
Example:

9
Unit-2 Control Structures in C

Loop Controls Structures in C


Loop control statements in C are used to perform looping operations until the given
condition is true. Control comes out of the loop statements once condition becomes false.
There are 3 types looping statements
o for loop
o while loop
o do while loop
for loop
for loop is a control flow statement that allows to repeatedly execute a block of code
a specified number of times. The basic syntax of a for loop is as follows:

• Initialization : This part is executed once before the loop starts. It's typically used to
initialize a loop control variable.
• Condition: This is a Boolean expression that is evaluated before each iteration. If the
condition is true, the loop continues; otherwise, it terminates.
• Update: This part is executed after each iteration and is usually used to update the loop
control variable.
• Code Block: The block of code inside the curly braces {} is the body of the loop. It gets
executed repeatedly as long as the condition is true.

10
Unit-2 Control Structures in C

Here's an example that uses a for loop to print the numbers 1 to 5:

In the above example:


• Initialization: int i = 1 initializes a loop control variable i with the value 1.
• Condition: i <= 5 is the condition. As long as i is less than or equal to 5, the loop
continues.
• Update: i++ increments the value of i by 1 after each iteration.
• Code Block: printf("%d\t", i); prints the current value of i to the console.

Example:

While loop

The while loop is control flow statement that allows to repeatedly execute a block of
code as long as a specified condition is true. The basic syntax of a while loop is as follows:

11
Unit-2 Control Structures in C

Flow chart:

• Condition : The boolean expression in the parentheses is evaluated before each iteration.
If the condition is true, the loop continues; otherwise, it terminates.
• Code Block: The block of code inside the curly braces {} is the body of the loop. It gets
executed repeatedly as long as the condition is true.

Here's an example that uses a while loop to print the numbers 1 to 10:

• Initialization : int i = 1 initializes a loop control variable i with the value 1 before the
loop starts.

12
Unit-2 Control Structures in C

• Condition : i <= 10 is the condition. As long as i is less than or equal to 5, the loop
continues.
• Update: i++ increments the value of i by 1 after each iteration.
• Code Block: printf("%d\n", i); prints the current value of i to the console.

do while

do-while loop is another type of loop that is similar to the while loop. The key
difference is that the do-while loop guarantees that the loop body will be executed at least
once, regardless of the initial condition. The basic syntax of a do-while loop is as follows:

Flow chart:

• Code Block: The block of code inside the curly braces {} is the body of the loop. It
gets executed at least once.
• Condition: The boolean expression in the parentheses is evaluated after each
iteration. If the condition is true, the loop continues; otherwise, it terminates.

Here's an example that uses a do-while loop to print the numbers 1 to 5:

13
Unit-2 Control Structures in C

Example to find factorial of a number using do while:

• Initialization : int i = 1 initializes a loop control variable i with the value 1 before the
loop starts.
• Code Block: printf("%d\n", i); prints the current value of i to the console.
• Update: i++ increments the value of i by 1 after each iteration.
• Condition: i <= 5 is the condition. If this condition is true, the loop continues; otherwise,
it terminates.

14
Unit-2 Control Structures in C

unconditional statement
goto statement
The goto statement is a control transfer statement that allows the control jump to a
labelled statement within the same function. The basic syntax of the goto statement is as
follows:

Flow chart goto statement:

An example to illustrate the usage of goto:

15
Unit-2 Control Structures in C

Break Statement:
The break in C is a loop control statement that breaks out of the loop when
encountered. It can be used inside loops or switch statements to bring the control out of the
block. The break statement can only break out of a single loop at a time.

The break statement has the following two usages:


• When the break statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement following the loop.
• It can be used to terminate a case in the switch statement.

Syntax:
break;
Flow Diagram

continue statement:
The continue statement resets program control to the beginning of the loop when
encountered. As a result, the current iteration of the loop gets skipped and the control
moves on to the next iteration. Statements after the continue statement in the loop are not
executed. The continue statement works somewhat like the break statement.

Syntax:
continue;

16

You might also like