0% found this document useful (0 votes)
32 views14 pages

Unit-2 PSWC

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)
32 views14 pages

Unit-2 PSWC

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/ 14

IT DEPARTMENT

Unit-2
Control Statements: Selection Statements- if and switch statements.
Iterative Statements: for, while and do-while statements.
Jump Statements: break, continue and goto statements.
What are Control Statements in C?
In C programming, a control statement is a statement that alters the flow of execution of a
program. We use control statements to determine the order in which the instructions within
a program are executed. They allow us to make decisions, repeat actions, and control the
flow of our program based on certain conditions.
Types of Control Statements in C:
There are three main types of control statements in C:
• Selection Statements (Conditional Statements)
• Iteration Statements (Loops)
• Jump Statements

1. Selection Statements or Conditional Statements(Decision Control):

Selection statements, also known as conditional statements, are a fundamental part of C


programming language. They allow you to make decisions in your code by executing certain
blocks of code based on the evaluation of conditions. We have primarily four types of
selection statements: if, if-else, Nested else-if & switch
if statement in C
In the C programming language, we use the if statement for conditional branching. It allows
a program to evaluate if a given condition is true or false and execute a block of code
accordingly.
Syntax:
if (condition)
{
// statements to execute if condition is true
}
IT DEPARTMENT

Flowchart :

Example:
#include <stdio.h>int main() {
if (25 > 17) {
printf("25 is greater than 17");
}
return 0;
}
Output :
25 is greater than 17
When we run the code, it checks the condition 25 > 17. Since this condition is true, the
program willexecute the printf() function inside the if statement
if-else statement in C
The if-else statement in C is a fundamental control flow structure that allows for conditional
execution of code. It tests a condition: if the condition is true, one block of code is executed,
and if the conditionis false, another block (or none at all) is executed.
Syntax :
if (condition)
{
// statements to execute if condition is true
}
else
{
// statements to execute if condition is false
}
Flowchart :
IT DEPARTMENT

Example: Write a C program whether the given is even or odd?

// C Program to demonstrate the use of if-else statement

#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
return 0;
}
Output
Enter a number:
5
5 is an odd number
Nested else-if statement in C :
Nested else-if statements in C are a way to create more decision-making structures in your
code. Theyinvolve using if statements within the branches of other if or else statements.
Syntax :
if (outer condition) {
if (inner condition1) {
// Code for inner condition1
} else if (inner condition2) {
// Code for inner condition2
} else {
// Code for other cases within outer condition
}
} else if (another outer condition) {
// Code for another outer condition

} else {
// Code for cases not covered by any condition
}
IT DEPARTMENT

Example:
Write a C program whether the given number is positive or negative or zero.
PROGRAM:

#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num > 0)
{
printf("%d is a positive number.\n", num);
}
else if (number < 0)
{
printf("%d is a negative number.\n", num);
}
else
{
printf("The number is zero.\n");

}
return 0;
}

Output:

Enter a number:
-8
-8 is a negative number.

Enter a number:
9
9 is a positive number.
IT DEPARTMENT

Switch statement in C
The switch statement is a control flow statement in C (and many other programming
languages) thatallows you to choose one of several possible code blocks to execute based
on the value of an expression. It’s often used as a more concise alternative to a series of if-
else statements when you need to compare a single value against multiple possible values.
Syntax :
switch (expression)
{

case value1:
// code will be executed if expression equals value1

break;
case
value2:
// code will be executed if expression equals value2

break;
// additional

default:
// code will be executed if none of the above cases match
}
Flowchart :
IT DEPARTMENT

Example:
You have to create a program to find a week of the day by using number.

PROGRAM:
#include <stdio.h>
int main()
{
int day;
printf("Enter the day number (1 for Sunday, 2 for Monday, ..., 7 for Saturday): ");
scanf("%d", &day)

switch(day) {
case 1:
printf("Sunday\n");break;
case 2:
printf("Monday\n");break;
case 3:
printf("Tuesday\n");break;
case 4:
printf("Wednesday\n");break;
case 5:
printf("Thursday\n");break;
case 6:
printf("Friday\n");break;
case 7:
printf("Saturday\n");break;

default:
printf("Invalid day number\n");
}
return 0;
}
Output:
Enter the day number (1 for Sunday, 2 for Monday, ..., 7 for Saturday):
6
Friday
IT DEPARTMENT

2. Iteration Statements (Loops)

We use loops it to execute a block of code repeatedly based on a condition or a set of


conditions. Wehave primarily three types of iteration statements: for loop, while loop, do-
while loop
for loop in C
The for loop in C provides a concise way to iterate over a block of code using an initializer, a
condition,and an iterator. We commonly use it when the number of iterations is known
beforehand.
It has three main parts:
• Initialization: Executed once at the beginning of the loop. This step allows you to
declare andinitialize any loop control variables.
• Condition: Evaluated before each iteration. If the condition evaluates to true (non-
zero), theloop body is executed. If it evaluates to false (zero), the loop is
terminated.
• Update: Executed after each iteration. It’s typically used to update loop control
variables.
Syntax :
for (initialization; condition; update)
{
// code to be executed in each iteration of the loop;
}

Flowchart :
IT DEPARTMENT

For example :
#include<stdio.h>
int main() {
for(int i = 1; i <= 8; i++)
{
printf("%d\t ", i);
}
return 0;
}
Output :
1 2 3 4 5 6 7 8
This output corresponds to the loop iterating through the numbers from 1 to 8 and printing
each number with tab spaces.
while loop in C
A while loop in C is a control flow structure that allows for a piece of code to be executed
repeatedlyas long as a particular condition is true.

Here, the condition is evaluated before the execution of the loop’s body. If the condition is
true, the code inside the loop will run. After the loop body has run, the condition is
evaluated again, and if it’s still true, the body runs again. This process continues until the
condition becomes false. If the conditionstarts off as false, the loop body will never execute.
Syntax :
while (condition)
{
// code to be executed as long as condition is true;
}
Flowchart :
IT DEPARTMENT

For example, let’s understand with the help of a scenario:


There’s a scenario where you’re tracking the distance of an Uber/Ola driver as they
approach your location and notifies you when the driver has arrived. So, in the below
program you know the initial distance of the driver from your location, and the program
simulates the driver’s progress by decreasingthe distance in increments of 200 meters.

#include <stdio.h>
int main() {

int distanceInMeters =1000;


while (distanceInMeters > 0)
{
printf("Driver is %d meters away...", distanceInMeters);
distanceInMeters -= 200; // Assuming the driver covers 200 meters every iteration.
}
printf("Your Uber has arrived!");
return 0;

}
Output :
Driver is 1000
meteraway...Driver is 800
meters away... Driver is
600 meters away... Driver
is 400 meters away...
Driver is 200 meters
away... Your Uber has
arrived!

The program uses a loop to simulate the approaching Uber/Ola driver by decreasing the
distance in increments of 200 meters. The loop exits when the driver reaches your
location, and the final message is displayed.
IT DEPARTMENT

do-while loop
The do-while loop checks the condition after executing the loop body, which ensures that the
loop bodyis executed at least once, even if the condition is initially false.
Syntax :
do
{
// code to be executed;
} while (condition);
Flowchart :

Example:
// C Program to demonstrate the use of do...while loop
#include <stdio.h>
int main()
{
// loop variable declaration and
initializationint i = 0;
do {
printf("Anitians\n");
;i++;

} while (i <3);
return 0;
}
Anitians
Anitians
Anitians
IT DEPARTMENT

The working of the do…while loop is explained below:


1. When the program control first comes to the do…while loop, the body of the loop is
executed first and then the test condition/expression is checked, unlike other loops
where the test condition is checked first. Due to this property, the do…while loop is
also called exit controlledor post-tested loop.
2. When the test condition is evaluated as true, the program control goes to the start
of the loop and the body is executed once more.
3. The above process repeats till the test condition is true.

4. When the test condition is evaluated as false, the program controls move on to the
next statements after the do…while loop.
As with the while loop in C, initialization and updating is not a part of the do…while loop
syntax. Wehave to do that explicitly before and, in the loop, respectively.
Difference between while and do…while Loop in C
The following table lists the important differences between the while and do…while Loop in
C.
IT DEPARTMENT

3. Jump Statements
In the C programming language, a jump statement is used to alter the normal flow of
execution of a program. It allows the program to transfer control to a different part of the
code, such as a different function or a different block of code within the same function.
There are three types of jump statementsin C: goto, break, and continue.
Goto statement:
The goto statement is utilized to transfer control to a labelled statement in the same
function. It is often considered bad practice to use goto statements as they can make the
code difficult to read and understand. However, there are some events where they may be
useful, such as when implementing error handling.
Syntax:
goto label;
…….
…….
label: statement;
The goto statement transfers control to the labelled statement, which is defined elsewhere
in the function.

Example:
#include <stdio.h>

int main() {
int i = 1;
loop:
printf("%d ", i);
i++;
if (i<= 10) {
goto loop;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
IT DEPARTMENT

Explanation:
In this example, the program uses a goto statement to jump back to the loop label until the
condition ismet.

Break statement:
The break statement is utilized to exit a loop or switch statement before its normal
termination. It iscommonly utilized in loops to exit early if any specific condition is met.
Syntax:
while (condition)
{if (condition) {
break;
}
}
Example:
#include
<stdio.h>int
main() {
int i;
for (i = 1; i<= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
1234
In this example, the program uses the break statement to exit the loop early when i is equal
to 5.
IT DEPARTMENT

Continue statement:
The continue statement is utilized to skip the remaining code in a loop for the current iteration
and moveon to the next iteration. It is commonly used in loops to skip over certain elements.
Syntax:
for (int i = 0; i< n; i++)
{
if (condition)
{
continue;
}
// Code to execute if condition is false
}
Example:
#include<stdio.h>
int main() {
int i;
for (i = 1; i<= 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
return 0;
}
Output:
13579
Explanation:
In this example, the program uses the continue statement to skip over even numbers in
the loop andonly print odd numbers.
On the other hand, the break and continue statements are commonly utilized in loops to
control the flowof execution. The break statement is utilized to exit a loop prematurely when
a certain condition is met, while the continue statement is utilized to skip over certain
iterations of a loop.
It is important to note that the excessive use of break and continue statements can make
the code difficult to read and understand. Therefore, it is suggested to utilize them often and
only when necessary. In some cases, it may be better to use a flag variable or a conditional
statement to control the loop flowinstead of using a break or continue statement.

You might also like