Procedural Programming_Unit_2_Decision & Loop Control Structure-1
Procedural Programming_Unit_2_Decision & Loop Control Structure-1
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
Example:
Nested if statements
3
Unit-2 Control Structures in C
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.
4
Unit-2 Control Structures in C
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:
6
Unit-2 Control Structures in C
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:
• expression : This is the value or variable that is being compared with the constants in
8
Unit-2 Control Structures in C
9
Unit-2 Control Structures in C
• 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
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.
13
Unit-2 Control Structures in C
• 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:
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.
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