KEMBAR78
If statements in c programming | PPTX
Hello!
Myself Archana R
Assistant Professor In
Dept Of CS
SACWC.
I am here because I love to
give presentations.
1
Decision Making And Branching
In Programming In C
INTRODUCTION
‘C’ language processes decision making capabilities supports the flowing
statements known as control or decision making statements
There are 4 Types:
●If statement
●switch statement
●conditional operator statement
●goto statement
3
If statement
 The if statement is powerful decision making
statement and is used to control the flow of execution
of statements.
 if statement is the most simple decision-making
statement. It is used to decide whether a certain
statement or block of statements will be
executed or not.
“
5
Types of if Statement
(a) Simple if statement
(b) If else statement
(d) Else –If ladder
5
(c) Nesting of If-else statement
Simple if Statement
If the test expression is true then a true block statement are executed,
otherwise the false – block statement are executed. In both cases
either true-block or false-block will be executed not both.
Syntax:
● if (test expression)
● {
statement block;
● }
statement-x ;
“
7
Flow Chart For Simple If Statement
7
 If the student belongs to the
sports category then additional bonus
marks are added to his marks before
they are printed. For others bonus
marks are not added .
8
Simple If Statement
Example
if (category = sports)
{
marks = marks + bonus marks;
}
printf(“%d”,marks);
If Else Statement
If the test expression is true then a true block statement are executed, otherwise the false block
statement are executed. In both cases either true-block or false-block will be executed not both.
Syntax:
if (test expression)
{
true-block statements;
}
else
{
false-block statements;
}
statement – x;
“
10
Flow Chart For If else Statement
10
 Here if the code is equal to ‘1’ the
statement boy=boy+1; Is executed and the control is
transferred to the statement st-n, after skipping the
else part. If code is not equal to ‘1’ the statement boy
=boy+1; is skipped and the statement in the else
part girl =girl+1; is executed before the control
reaches the statement st-n.
11
If else Statement
Example
If (code == 1)
boy = boy + 1;
else
girl = girl + 1;
st-x;
Nesting of if else statement
When a series of decisions are involved we may have to use more than one
if-else statement in nested form of follows.
Syntax: if ( Test Condition1) {
if ( Test Condition2) {
Statement -1;
}
else {
Statement -2; }
else {
Statement -3;}
}
“
13
Flow Chart For nesting of If else Statement
13
14
Nesting of If else Statement
Example
Else-If ladder
A multi path decision is charm of its in which the statement associated with
each else is an If. It takes the following general form.
This construct is known as the wise-If ladder. The conditions are evaluated
from the top of the ladder to down wards. As soon as a true condition is
found the statement associated with it is executed and the control the is
transferred to the st-X (i.e.., skipping the rest of the ladder). when all the n-
conditions become false then the final else containing the default – st will be
executed.
16
Else-If ladder
SYNTAX
if (Test Condition -1) {
Statement -1; }
else if ( Test Condition -2) {
Statement -2;}
else if ( Test Condition -3) {
Statement -3; }
else if ( Test Condition –n) {
Statement –n;
}else {
default statement; }
Statements-X;
17
Else-If ladder Flow Chart
“
18
18
The above construction is known as else if ladders.
The conditions are evaluated from top to bottom.
As soon as a true condition is found, the statement associated
with it is executed and
The control is transferred to the Rest of the Program
Statement–X (skipping rest of the ladder).
When all the “n” conditions become false, then the final else
containing the default statement will be executed.
Ex : If (code = = 1) Color = “red”;
Else if ( code = = 2) Color = “green”
Else if (code = = 3) Color = “white”;
Else Color = “yellow”;
If code number is other than 1,2 and 3 then color is yellow.
Else-If ladder
Thanks!
19

If statements in c programming

  • 1.
    Hello! Myself Archana R AssistantProfessor In Dept Of CS SACWC. I am here because I love to give presentations. 1
  • 2.
    Decision Making AndBranching In Programming In C
  • 3.
    INTRODUCTION ‘C’ language processesdecision making capabilities supports the flowing statements known as control or decision making statements There are 4 Types: ●If statement ●switch statement ●conditional operator statement ●goto statement 3
  • 4.
    If statement  Theif statement is powerful decision making statement and is used to control the flow of execution of statements.  if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not.
  • 5.
    “ 5 Types of ifStatement (a) Simple if statement (b) If else statement (d) Else –If ladder 5 (c) Nesting of If-else statement
  • 6.
    Simple if Statement Ifthe test expression is true then a true block statement are executed, otherwise the false – block statement are executed. In both cases either true-block or false-block will be executed not both. Syntax: ● if (test expression) ● { statement block; ● } statement-x ;
  • 7.
    “ 7 Flow Chart ForSimple If Statement 7
  • 8.
     If thestudent belongs to the sports category then additional bonus marks are added to his marks before they are printed. For others bonus marks are not added . 8 Simple If Statement Example if (category = sports) { marks = marks + bonus marks; } printf(“%d”,marks);
  • 9.
    If Else Statement Ifthe test expression is true then a true block statement are executed, otherwise the false block statement are executed. In both cases either true-block or false-block will be executed not both. Syntax: if (test expression) { true-block statements; } else { false-block statements; } statement – x;
  • 10.
    “ 10 Flow Chart ForIf else Statement 10
  • 11.
     Here ifthe code is equal to ‘1’ the statement boy=boy+1; Is executed and the control is transferred to the statement st-n, after skipping the else part. If code is not equal to ‘1’ the statement boy =boy+1; is skipped and the statement in the else part girl =girl+1; is executed before the control reaches the statement st-n. 11 If else Statement Example If (code == 1) boy = boy + 1; else girl = girl + 1; st-x;
  • 12.
    Nesting of ifelse statement When a series of decisions are involved we may have to use more than one if-else statement in nested form of follows. Syntax: if ( Test Condition1) { if ( Test Condition2) { Statement -1; } else { Statement -2; } else { Statement -3;} }
  • 13.
    “ 13 Flow Chart Fornesting of If else Statement 13
  • 14.
    14 Nesting of Ifelse Statement Example
  • 15.
    Else-If ladder A multipath decision is charm of its in which the statement associated with each else is an If. It takes the following general form. This construct is known as the wise-If ladder. The conditions are evaluated from the top of the ladder to down wards. As soon as a true condition is found the statement associated with it is executed and the control the is transferred to the st-X (i.e.., skipping the rest of the ladder). when all the n- conditions become false then the final else containing the default – st will be executed.
  • 16.
    16 Else-If ladder SYNTAX if (TestCondition -1) { Statement -1; } else if ( Test Condition -2) { Statement -2;} else if ( Test Condition -3) { Statement -3; } else if ( Test Condition –n) { Statement –n; }else { default statement; } Statements-X;
  • 17.
  • 18.
    “ 18 18 The above constructionis known as else if ladders. The conditions are evaluated from top to bottom. As soon as a true condition is found, the statement associated with it is executed and The control is transferred to the Rest of the Program Statement–X (skipping rest of the ladder). When all the “n” conditions become false, then the final else containing the default statement will be executed. Ex : If (code = = 1) Color = “red”; Else if ( code = = 2) Color = “green” Else if (code = = 3) Color = “white”; Else Color = “yellow”; If code number is other than 1,2 and 3 then color is yellow. Else-If ladder
  • 19.