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

Control Statements

Ps

Uploaded by

Manpreet Kaur
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)
5 views

Control Statements

Ps

Uploaded by

Manpreet Kaur
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/ 19

Control Statements

Loops
• Loops can execute a block of code as long as a
specified condition is reached.
• Loops save time, reduce errors, and they make
code more readable.
while loop
• The while loop loops through a block of code
as long as a specified condition is true:
• Syntax
while (condition) {

// code block to be executed

}
Example
int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}
do while loop
• The do/while loop is a variant of
the while loop.
• This loop will execute the code block once,
before checking if the condition is true, then it
will repeat the loop as long as the condition is
true.
Example
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
for loop
• The for loop in C Language provides a
functionality/feature to repeat a set of
statements a defined number of times. The for
loop is in itself a form of an entry-controlled
loop.
• Unlike the while loop and do…while loop, the
for loop contains the initialization, condition,
and updating statements as part of its syntax.
Syntax of for loop
for(initialization; check/test expression;
updation)
{
// body consisting of multiple statements
}
Example
int i;

for (i = 0; i < 5; i++)


{
printf("%d\n", i);
}
Nested Loops
• It is also possible to place a loop inside
another loop. This is called a nested loop.
• The "inner loop" will be executed one time for
each iteration of the "outer loop"
Syntax
for ( initialization; condition; increment )
{
for ( initialization; condition; increment )
{
// statement of inside loop
}
// statement of outer loop
}
switch statement
• Switch case statement evaluates a given
expression and based on the evaluated
value(matching a certain condition), it
executes the statements associated with it.
Basically, it is used to perform different
actions based on different conditions(cases).
Syntax
switch(expression)
{
case value1:
statement_1;
break;
case value2:
statement_2;
break;
.
.
.
case value_n:
statement_n;
break;
default:
default_statement;
}
#include <stdio.h>
int main()
{
int var = 1;
switch (var)
{ case 1:
printf("Case 1 is Matched.");
break;
case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;
default:
printf("Default case is Matched.");
break;
}
return 0;
}
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.
Example
for (int i = 1; i < 5; i++)
{
if (i == 3)
{
break;
}
else
{
printf("%d ", i);
}
}
continue statement in C
• The continue statement in C is used in loops to
skip the current iteration and move on to the
next iteration without executing the
statements below the continue in the loop
body.
Example
for (int i = 1; i <= 7; i++)
{
if (i == 3)
continue;
printf("%d ", i);
}

You might also like