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

Control-Statements

The document discusses control statements in C programming, highlighting the importance of decision-making in controlling program flow. It covers various types of control statements, including if statements, nested ifs, and if-else ladders, along with their syntax and examples. Additionally, it explains loop control statements like break and continue, which manage loop execution.

Uploaded by

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

Control-Statements

The document discusses control statements in C programming, highlighting the importance of decision-making in controlling program flow. It covers various types of control statements, including if statements, nested ifs, and if-else ladders, along with their syntax and examples. Additionally, it explains loop control statements like break and continue, which manage loop execution.

Uploaded by

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

Control Statements

In C, the control flows from one instruction to the


next instruction until now in all programs. This
control flow from one command to the next is
called sequential control flow. Clearly,
programmer may want to skip instructions or
repeat a set of instructions repeatedly when
writing logic. This can be referred to as
sequential control flow. The declarations in C let
programmers make such decisions which are
called decision-making or control declarations.
Types of Control Statements in C

If statements
Switch Statement
Conditional Operator Statement
Goto Statement
Loop Statements
If statement
If statement enables the programmer to choose a set of
instructions, based on a condition. When the condition is
evaluated to true, a set of instructions will be executed and a
different set of instructions will be executed when the
condition is evaluated to false. We have 4 types of if
Statement which are:

1. If..else
2. Nested if
3. Else if ladder
4. Simple if or null else
5. Null else or Simple else
If statement
Syntax for the if selection structure is as follows:

if ( this logical expression is true )


statement ;
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
if / else
In this statement, there are two types of statements
execute. First, if the condition is true first statement will
execute if the condition is false second condition will be
executed.

Syntax for the if / else selection structure is as


follows:

if ( this logical expression is true )


statement ;
else
statement ;
if / else Selection Structures Example

#include <stdio.h>
int main ( )
{
int a = 1, b = 2, c ;
if (a > b)
c = a;
else
c = b;
}
nested-if in C
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.
if (condition1)
{
// Executes when condition1 is
true
if (condition2)
{
// Executes when condition2 is
true
}
}
#include <stdio.h>

int main() {
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf("i is smaller than 15\n");

// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}

return 0;
}
if-else-if ladder in C

Here, a user can 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 are
true, then the final else statement will be executed.
if / else if / else Selection
Structures
• The actual syntax for the multiple if / else if / else
selection structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ;
#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");
}
if / else if / else Selection
Structures
• Syntax for the if / else if / else selection
structure is as follows:

if ( this logical expression is true )


statement ;
else if ( this logical expression is true )
statement ;
else
statement ;
Simple Program Using if / else if /
else
#include <stdio.h>
int main ( )
{
int a , b ;
printf ("Enter values for a and b > ") ;
scanf ("%d%d", &a, &b ) ;
if ( a < b )
printf ("a is less than\n") ;
else if ( a == b )
printf (" a is equal to b\n") ;
else
printf ("a is larger than b\n") ;
}
C break: This loop control statement is used to terminate
the loop. As soon as the break statement is encountered
from within a loop, the loop iterations stop there, and
control returns from the loop immediately to the first
statement after the loop.
Syntax:

break;
C continues: This loop control statement is just like the
break statement. The continue statement is opposite to
that of the break statement, instead of terminating the
loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the
loop to continue or execute the next iteration. When the
continue statement is executed in the loop, the code
inside the loop following the continue statement will be
skipped and the next iteration of the loop will begin.
Syntax:

continue;

You might also like