0% found this document useful (0 votes)
12 views12 pages

IP Unit 2

The document explains control structures in C programming, focusing on decision-making statements such as if, if-else, nested if, if-else-if ladder, and switch statements. It also covers loop statements including for, while, and do-while loops, along with the use of break and continue statements to control loop execution. These structures are essential for making decisions and repeating code based on conditions.

Uploaded by

nagabadramram708
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)
12 views12 pages

IP Unit 2

The document explains control structures in C programming, focusing on decision-making statements such as if, if-else, nested if, if-else-if ladder, and switch statements. It also covers loop statements including for, while, and do-while loops, along with the use of break and continue statements to control loop execution. These structures are essential for making decisions and repeating code based on conditions.

Uploaded by

nagabadramram708
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

Unit-2

Control Structures
Decision Making/Conditional Statements in C:

The conditional statements (also known as decision control structures) such as if, if
else, Nested –if, If-else-If, switch, etc. are used for decision-making purposes in C programs.

They are also known as Decision-Making Statements and are used to evaluate one or
more conditions and make the decision whether to execute a set of statements or not. These
decision-making statements in programming languages decide the direction of the flow of
program execution.

Need of Conditional Statements

There come situations in real life when we need to make some decisions and based on
these decisions, we decide what should we do next. Similar situations arise in programming
also where we need to make some decisions and based on these decisions we will execute the
next block of code. For example, in C if x occurs then execute y else execute z. There can
also be multiple conditions like in C if x occurs then execute p, else if condition y occurs
execute q, else execute r. This condition of C else-if is one of the many ways of importing
multiple conditions.

Types of Conditional Statements in C

Following are the decision-making statements available in C

1. if Statement
2. if-else Statement
3. Nested if Statement
4. if-else-if Ladder
5. switch Statement

1. If Statement:-

The if statement is the most simple decision-making statement. It is used to decide


whether a certain statement or block of statements will be executed or not i.e if a certain
condition is true then a block of statements is executed otherwise not.

Syntax:

if(condition)
{
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. C if statement accepts boolean
values – if the value is true then it will execute the block of statements below it otherwise not.
If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if
statement will consider the first immediately below statement to be inside its block.

1
Flowchart:

Example:
#include <stdio.h>
int main()
{
int i = 10;

if (i > 15) {
printf("10 is greater than 15");
}
printf("I am Not in if");
}
Output:
I am Not in if
As the condition present in the if statement is false. So, the block below the if statement is not
executed.

2. If-else Statement:

The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But what if we want to do something else
when the condition is false? Here comes the C else statement. We can use the else statement
with the if statement to execute a block of code when the condition is false. The if-else
statement consists of two blocks, one for false expression and one for true expression.

Syntax:

if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

2
Flowchart:

Example:
#include <stdio.h>
int main()
{
int i = 20;

if (i < 15) {
printf("i is smaller than 15");
}
else {
printf("i is greater than 15");
}
return 0;
}
Output:
i is greater than 15

3. Nested if Statement:

A nested if in C is an if statement that is the target of another if statement. Nested if


statements mean an if statement inside another if statement. Yes, both C and C++ allow us to
nested if statements within if statements, i.e, we can place an if statement inside another if
statement.

Syntax:-

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}

3
Flowchart:

Example:
C program to find the largest number among three number
#include <stdio.h>
int main()
{
int A, B, C;
printf("Enter three numbers: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B) {
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}
return 0;
}

Output:
Enter three numbers: 3 1 4
4 is the largest number.

4. if-else-if Ladder:-

The if else if statements are used when the user has to decide among multiple options.
The C if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest of the C
else-if ladder is bypassed. If none of the conditions is true, then the final else statement will
be executed. if-else-if ladder is similar to the switch statement.

Syntax:

if (condition)

4
statement;
else if (condition)
statement;
.
.
else
statement;
Flowchart:

Example:
#include <stdio.h>
int main()
{
int i = 20;

if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output:
i is 20

5. Switch Statement:

The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement. The switch block consists of cases to be executed based on the value of the switch
variable.

5
Syntax:-

Switch (Expression)
{
Case 0:
Statement-0;
Break;
Case 1:
Statement-1;
Break;
------------------------
------------------------
------------------------
Default:
Statements;
}
Flowchart:
#include <stdio.h>
int main()
{
// variable to be used in switch statement
int var = 2;

// declaring switch cases


switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
Output:
Case 2 is executed

6
Loop Statements:

Loops in programming are used to repeat a block of code until the specified condition
is met. A loop statement allows programmers to execute a statement or group of statements
multiple times without repetition of code.
There are mainly two types of loops in C Programming:
Entry Controlled loops: In Entry controlled loops the test condition is checked before
entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.

Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of
the loop body. The loop body will execute at least once, irrespective of whether the condition
is true or false. do-while Loop is Exit Controlled loop.

1. For loop:
For loop in C programming is a repetition control structure that allows programmers to
write a loop that will be executed a specific number of times. for loop enables programmers
to perform n number of steps together in a single line.
Syntax:
for (initialize expression; condition; increment/decrement)
{
//
// body of for loop
//
}
In for loop, a loop variable is used to control the loop. Firstly we initialize the loop
variable with some value, then check its test condition. If the statement is true then control
will move to the body and the body of for loop will be executed. Steps will be repeated till
the exit condition becomes true. If the test condition will be false then it will stop.

 Initialization Expression: In this expression, we assign a loop variable or loop


counter to some value. for example: int i=1;
 Condition: In this expression, test conditions are performed. If the condition
evaluates to true then the loop body will be executed and then an update of the
loop variable is done. If the test expression becomes false then the control will
exit from the loop. for example, i<=9;
 Increment/Decrement: After execution of the loop body loop variable is updated
by some value it could be incremented, decremented, multiplied, or divided by
any value.
Flowchart:

7
Example:
#include <stdio.h>
// Driver code
int main()
{
int i = 0;
for (i = 1; i <= 5; i++)
{
printf( "Hello World\n");
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World

2. 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.
Syntax:

initialization_expression;
while (test_expression)
{
// body of the while loop

Increment/Decrement;
}
Flowchart:

8
Example:
#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
// Test expression
while(i < 10)
{
// loop body
printf( "Hello World\n");

// update expression
i++;
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

3. do-while:
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 execute at least once irrespective of the test condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
Flowchart:

9
Example:
#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
do
{
// loop body
printf( "Hello World\n");

// Update expression
i++;
// Test expression
} while (i < 1);
return 0;
}
Output:
Hello World

Break Statements:-
The break statement exits or terminates the loop or switch statement based on a
certain condition, without executing the remaining code.
Syntax:
break;

Flowchart:

Uses of break in C:

The break statement is used in C for the following purposes:


 To come out of the loop.
 To come out from the nested loops.
 To come out of the switch case.

10
The statements inside the loop are executed sequentially. When the break statement is
encountered within the loop and the condition for the break statement becomes true, the
program flow breaks out of the loop, regardless of any remaining iterations.
#include <stdio.h>
int main()
{
int i;
// for loop
for (i = 1; i <= 10; i++) {

// when i = 6, the loop should end


if (i == 6) {
break;
}
printf("%d ", i);
}
printf("Loop exited.\n");
return 0;
}
Output:
1 2 3 4 5 Loop exited.
Explanation:

Loop Execution Starts and goes normally till i = 5.


When i = 6, the condition for the break statement becomes true and the program control
immediately exits the loop.
The control continues with the remaining statements outside the loop.

Continue Statement:
The continue statement in C is used to skip the remaining code after the continue
statement within a loop and jump to the next iteration of the loop. When the continue
statement is encountered, the loop control immediately jumps to the next iteration, by
skipping the lines of code written after it within the loop body.
Syntax:
continue;
Flowchart:

11
Example:
#include <stdio.h>
int main()
{
int i;
// loop
for (i = 0; i < 5; i++) {
if (i == 2) {
// continue to be executed if i = 2
printf("Skipping iteration %d\n", i);
continue;
}
printf("Executing iteration %d\n", i);
}
return 0;
}
Output:
Executing iteration 0
Executing iteration 1
Skipping iteration 2
Executing iteration 3
Executing iteration 4

Explanation: The for loop iterates from 0 to 4. Inside the loop, we check if i is equal to 2. If
the condition is true, the continue statement is executed, and it skips the remaining code
within the loop for that iteration. If the condition is false, the code proceeds normally.

12

You might also like