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

Control statements

Uploaded by

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

Control statements

Uploaded by

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

Control statements

1
Control statements

 Conditional control statements

 Multiple branching control statements

 Loop control statements

 Jump control statements

2
Conditional control statements
 Conditional control statements help with implementing
decision-making in a program. A conditional control statement
selects the block of statements that will execute based on the
given condition. The condition can be checked in different
ways as:
 if

 if-else

 nested if-else

 if-else if ladder

3
if statement
 if
if is used to specify a block of code to be executed when a
specified condition is true.

Syntax
if(expression)
{
//code to be executed if condition is true.
}

4
if statement example
program.c program.cpp

5
if-else statement
 if-else
if is used to specify a block of code to be executed when a specified
condition is true. else is used to specify a block of code to be
executed when a specified condition is false.

Syntax
if(expression)
{
//code to be executed if condition is true.
}
else
{
//code to be executed if condition is false.
}

6
if-else statement example
program.c program.cpp

7
nested if-else statement
 nested if-else
When an if else statement is present inside the body of another “if ” or “else” then this is called nested if else.

Syntax
if(condition)
{
//Nested if else inside the body of "if"
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else
{
//Statements inside the body of "else"
}

8
nested if-else statement example
program.c program.cpp

9
if-else if ladder statement
 nested if-else
The if – else if ladder statement is useful when multiple conditions are needed to be checked within the program.

Syntax
if (condition1)
{
// condition 1 statements
}
else if (condition2)
{
// condition 2 statements
}
else if (condition3)
{
// condition3 statements
}
else
{
// else statements
}

10
if-else if ladder statement example
program.c program.cpp

11
Multiple branching control statements
 switch-case
A switch statement allows a variable to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each switch case.

Syntax
switch(variable or expression)
{
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}

12
switch case statement example
program.c program.cpp

13
Loop control statements
In programming, a loop is used to repeat a block of code until the
specified condition is met. C programming has three types of loops:
 for

 while

 do while

Nesting of loops is the feature in C that allows the looping of


statements inside another loop.
 Nested loops

14
for loop statement
Syntax
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}

15
for loop example
program.c program.cpp

16
while loop statement
Syntax
while (testExpression)
{
// the body of the loop
}

17
while loop example
program.c program.cpp

18
do while loop statement
Syntax
do {
// the body of the loop
}
while (testExpression);

19
do while loop example
program.c program.cpp

20
nested loop statement
Syntax
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of outer loop
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of inner loop
}
// statements inside the body of outer loop
}

21
nested loop example
program.c

22
nested loop example
program.cpp

23
Jump control statements
Jump statements are used to interrupt the flow of the program or
escape a particular section of the program. There are many more
operations they can perform within the loops, switch statements,
and functions. There are four types of jump statements.
 break

 continue

 goto

 exit

 return

24
break statement
 break
The break is a keyword in C which is used to bring the program
control out of the loop. The break statement is used inside loops or
switch statement.

Syntax
break;

25
break statement example
program.c program.cpp

26
continue statement
 continue
The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.

Syntax
` continue;

27
continue statement example
program.c program.cpp

28
goto statement
 goto
The goto statement is a jump statement which is sometimes also referred to as unconditional jump
statement. The goto statement is used to jump from one block to another block during execution and
transfer the flow of execution of the code.

Syntax
// forward jump statement
goto label;
……….
……….
label: statements;

//Backward jump statement


label: statements;
……….
……….
goto label;

29
goto statement example
program.c program.cpp

30
exit statement
 exit
The exit() function is used to terminate a process or
function calling immediately in the program.

Syntax
void exit ( int status);

31
exit statement example
program.c program.cpp

32
return statement
 return
A return statement ends the execution of a function, and
returns control to the calling function. Execution
resumes in the calling function at the point immediately
following the call. A return statement can return a value
to the calling function.

33

You might also like