Control Statements in C
Prof. Navya Francis
Asst. Professor
Kristu Jayanti College
• The control statements in programming languages decide the
direction of the flow of program execution.
• They are also known as decision control structures.
Introduction
Need of Conditional Statements
In everyday situations, we often find ourselves making decisions that influence our
subsequent actions.
Likewise, within the domain of programming, there are scenarios that necessitate decision-
making, with the subsequent course of action dictated by these choices, shaping the
execution of the succeeding code block.
Types of Conditional Statements in C/C++
oif Statement
oif-else Statement
oNested if Statement
oif-else-if Ladder
oswitch Statement
oConditional Operator
oJump Statements:
o break
o continue
o goto
o return
If Statement
• The "if" statement in C serves as a decision-making construct,
allowing the execution of a code block contingent on the evaluated
expression's value. This fundamental concept in C programming
enables the incorporation of conditional code within our program.
Syntax:
if(condition)
{
// Statements to execute if condition is true
}
Flowchart
Example 1: C Program to check whether the
number is even or odd.
# include ,stdio.h>
void main
{
int n = 10;
if(n%2==0)
{
printf(“%d is Even number”,n);
}
}
Output
10 is Even number
if else statement
• The if-else statement is employed when there is a need to execute certain
code if a condition is true and an alternative code if the condition is false.
• Following an if statement, an optional else statement can be included,
activating when the boolean expression evaluates to false.
Syntax:
if(condition)
{
/* statement(s) will execute if the
boolean expression is true */
}
else
{
/* statement(s) will execute if the
boolean expression is false */
}
Flowchart:
Nested if statement
A nested if statement is an if statement placed
inside another if statement. Nested if statements
are often used when you must test a combination
of conditions before deciding on the proper
action. ie.)You can use one if or else if statement
inside another if or else if statement(s).
Syntax:
if ( test condition 1)
{
//If the test condition 1 is TRUE then these it will check for test condition 2
if ( test condition 2)
{
//If the test condition 2 is TRUE then these statements will be executed
Test condition 2 True statements;
}
else
{
//If the test condition 2 is FALSE then these statements will be executed
Test condition 2 False statements;
}
else
{
//If the test condition 1 is FALSE then these statements will be executed
Test condition 1 False statements;
}
else if ladder/if else ladder
else if ladder/if else ladder if-else ladder is an extension of if…else statement.
By using one if-else construct, it is possible to choose between two choices.
There may be a situation wherein it is required to select one among more
than two choices. This can be accomplished by another if-else in the else
part of the construct. This type of if construct is called as else if ladder
statement.
Syntax:
if(condition 1)
statement 1;
else if(condition 2)
statement 2;
"
"
"
else if(condition n)
statement n;
else default statement;
Thank You

C Programming: Control Statements in C Pgm

  • 1.
    Control Statements inC Prof. Navya Francis Asst. Professor Kristu Jayanti College
  • 2.
    • The controlstatements in programming languages decide the direction of the flow of program execution. • They are also known as decision control structures. Introduction
  • 3.
    Need of ConditionalStatements In everyday situations, we often find ourselves making decisions that influence our subsequent actions. Likewise, within the domain of programming, there are scenarios that necessitate decision- making, with the subsequent course of action dictated by these choices, shaping the execution of the succeeding code block.
  • 4.
    Types of ConditionalStatements in C/C++ oif Statement oif-else Statement oNested if Statement oif-else-if Ladder oswitch Statement oConditional Operator oJump Statements: o break o continue o goto o return
  • 5.
    If Statement • The"if" statement in C serves as a decision-making construct, allowing the execution of a code block contingent on the evaluated expression's value. This fundamental concept in C programming enables the incorporation of conditional code within our program.
  • 6.
    Syntax: if(condition) { // Statements toexecute if condition is true } Flowchart
  • 7.
    Example 1: CProgram to check whether the number is even or odd. # include ,stdio.h> void main { int n = 10; if(n%2==0) { printf(“%d is Even number”,n); } } Output 10 is Even number
  • 8.
    if else statement •The if-else statement is employed when there is a need to execute certain code if a condition is true and an alternative code if the condition is false. • Following an if statement, an optional else statement can be included, activating when the boolean expression evaluates to false.
  • 9.
    Syntax: if(condition) { /* statement(s) willexecute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ } Flowchart:
  • 10.
    Nested if statement Anested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action. ie.)You can use one if or else if statement inside another if or else if statement(s).
  • 11.
    Syntax: if ( testcondition 1) { //If the test condition 1 is TRUE then these it will check for test condition 2 if ( test condition 2) { //If the test condition 2 is TRUE then these statements will be executed Test condition 2 True statements; } else { //If the test condition 2 is FALSE then these statements will be executed Test condition 2 False statements; } else { //If the test condition 1 is FALSE then these statements will be executed Test condition 1 False statements; }
  • 12.
    else if ladder/ifelse ladder else if ladder/if else ladder if-else ladder is an extension of if…else statement. By using one if-else construct, it is possible to choose between two choices. There may be a situation wherein it is required to select one among more than two choices. This can be accomplished by another if-else in the else part of the construct. This type of if construct is called as else if ladder statement.
  • 13.
    Syntax: if(condition 1) statement 1; elseif(condition 2) statement 2; " " " else if(condition n) statement n; else default statement;
  • 14.