0% found this document useful (0 votes)
15 views32 pages

Unit 3

Unit 3 covers decision-making statements in programming, focusing on various types of control structures such as if statements, switch-case statements, and their respective uses. It details branching statements, including simple if, if-else, nested if, and ladder if statements, along with syntax and examples. Additionally, it explains switch-case statements, their rules, and the differences between switch and if-else statements.

Uploaded by

Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

Unit 3

Unit 3 covers decision-making statements in programming, focusing on various types of control structures such as if statements, switch-case statements, and their respective uses. It details branching statements, including simple if, if-else, nested if, and ladder if statements, along with syntax and examples. Additionally, it explains switch-case statements, their rules, and the differences between switch and if-else statements.

Uploaded by

Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIT 3 : DECISION MAKING

STATEMENTS
3.1 if statements :
3.1.1 simple if statements
3.1.2 if…else statements
3.1.3 if…else if….else statements
3.1.4 Nested if statements.
3.2 Switch..case statements
3.2.1 Use of break and default
3.2.2 Difference between switch and if
statements
Control Statement or Control
Structure
 Control Statements or Control
Structures are used to control the
flow of Program.
 Control structures are the statements
that control the flow of the source
code.
 Control statements enable us to
specify the flow of program
control; i.e., the order in which the
instructions in a program must be
executed.
 They make it possible to make
decisions, to perform tasks
repeatedly or to jump from one
section of code to another.
 There are three categories of flow
controls:
1. Branching or Selection Statement
2. Looping or Iterative Statement
3. Jumping
3.1. Branching or Conditional
Statement or if Statement
 Branching are decision making
statements.
 The C language programs follows a
sequential form of execution of
statements. Many times it is required
to alter the flow of sequence of
instructions.
 C language provides statements that
can alter the flow of a sequence of
instructions.
 3.1.1. Simple if Statement
 3.1.2. if…else Statement
 3.1.3. Nested if Statement
 3.1.4. Ladder if or elseif Statement
3.1.1. Simple if Statement
 When we need to execute a block
of statements only when a given
condition is true then we use if
statement.
Syntax:
if (test - condition)
{
//Statement
}
Next statements
Flowchart:
Example:
void main()
{
int a=10,b=20;
clrscr();
if(a<b)
{
printf("Inside IF");
}
printf("\nOutside IF");
getch();
}
OUTPUT:
Inside IF
Outside IF
3.1.2. if…else Statement
 An if statement can be followed by an
optional else statement, which
executes when the Boolean
expression is false.
Syntax:
if(test - condition)
{
// True Statements
}
else
{
// False Statements
}
Flowchart:
Example:
void main()
{
int a=10,b=20;
clrscr();
if(a>b)
{
printf(“a is Maximum");
}
else
{
printf("\n b is Maximum");
}
getch();
}
OUTPUT:
3.1.3. Nested if Statement
 If statement within if statement
is known as nested if statement.
Syntax:
if(test - condition 1)
{
if(test - condition 2)
{
// True Statement
}
}
else
{
if(test - condition 3)
{
// False Statement
}
}
Flowchart:
Example:
void main()
{
int a=10,b=20,c=30;
clrscr();
if(a<b)
{
if(b<c)
printf("Inside IF");
}
else
{
printf("\nInside ELSE");
}
getch();
}
OUTPUT:
Inside IF
3.1.4. Ladder if or elseif
Statement
 else-if statements in C is like another
if condition, it's used in a program
when if statement having
multiple decisions.
Syntax:
if(test - condition 1) else
{ {
// Statement 1 // Else Statement
} }
else if(test - condition 2) next- statement
{
// Statement 2
}
...
...
...
else if(test - condition n)
{
// Statement n
}
Flowchart:
Example:
void main()
/*W A Menu Driven {
int c;
Program to perform
clrscr();
the follwing task
printf("\n1. Sunday");
1. Sunday printf("\n2. Monday");
2. Monday printf("\n3. Tuesday");
printf("\n4. Wednesday");
3. Tuesday
printf("\n5. Thursday");
4. Wednesday printf("\n6. Friday");
5. Thursday printf("\n7. Saturday");
printf("\nEnter Your Choice:(1 to 7):");
6. Friday
scanf("%d",&c);
7. Saturday */ if(c==1)
#include<stdio.h> printf("Sunday");
else if(c==2)
#include<conio.h>
printf("Monday");
else if(c==3)
printf("Tuesday");
else
printf("Invalid Chioce");
getch();
}
3.2 Switch Case or Selection
Statement
 Switch case is used to select one
option from one or more option.
 Switch is used for only int, char
and enum data.
 We can also use nested switch (switch
within switch).
Syntax:
switch(expression)
{
case value1:
Statement 1;
break;
case value2:
Statement 2;
break;
...
...
...
case valuen:
Statement n;
break;
default:
default Statement;
break;
}
next- statement;
Flowchart:
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("Enter your choice:");
scanf("%c",&c);
switch(c)
{
case 'A':
printf("Assignment");
break;
case 'B':
printf("Quiz");
break;
case 'C':
printf("Viva");
break;
default:
printf("Invalid Choice");
break;
}
getch();
}
Rules of Switch case.
 Case label should be unique.
 Case label must be ends with : (colon).
 Case label must have constant value
or constant expression.
 Case label must be int, char or enum.
 Case label should not be floating
point number.
 Default label is optional.
 break statement takes control out
of loop.
 Two or more case may share one
break operation.
 Nesting is also possible.
 Relational operations are not
allowed in switch case.
3.2.1 Use of break
 You can use the break statement to end
processing of a particular
labeled statement within the switch
statement.
 It branches to the end of the switch
statement.
 Without break, the program continues to
the next labeled statement, executing
the statements until a break or the end
of the statement is reached.
Use of default
 A switch statement can have an
optional default case, which must
appear at the end of the switch.
 The default case can be used for
performing a task when none of
the cases is true.
 No break is needed in the default
case.
3.2.2 Difference between
switch and if-else statements

You might also like