0% found this document useful (0 votes)
64 views24 pages

Decision Making

Here are the key points about an if-else-if statement: - It allows you to check for multiple conditions in a single statement - The conditions are checked sequentially, from top to bottom - As soon as a condition is found to be true, the code for that condition is executed - Any subsequent conditions are skipped, even if they may also be true - An else clause handles the case when all conditions are false So an if-else-if allows for efficient checking of multiple exclusive conditions without nested if statements.

Uploaded by

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

Decision Making

Here are the key points about an if-else-if statement: - It allows you to check for multiple conditions in a single statement - The conditions are checked sequentially, from top to bottom - As soon as a condition is found to be true, the code for that condition is executed - Any subsequent conditions are skipped, even if they may also be true - An else clause handles the case when all conditions are false So an if-else-if allows for efficient checking of multiple exclusive conditions without nested if statements.

Uploaded by

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

Assignment marks(5+5+5=15)

1) Write a program that gets temperature from the


user in Celsius and convert it into Fahrenheit
using the formula F=9/5*Cel+32
2) Write a program that input 4 numbers and
calculate the sum, average and product of all
the numbers.
3) Write a program that inputs base and height
from the user and calculate area of a triangle by
using the formula Area=1/2*base*height
Variable declaration
 The process of specifying the variable name and its type is called
variable declaration.
 Once a variable is declared, its data types cannot be changed
during program execution. However the value of variable can be
changed during execution.
 Syntax: data_type variable_name;
data_type: it indicates type of data that can be stored in variable.
variable_name: it refers to the memory location of the variable.
Examples:
 int marks;
 Float average;
 char grade;
Variable Initialization
 The process of assigning a value to a variable at the
time of initialization is called variable initialization.
 The equal sign = is used to initialize a variable.
 Variable name is written on left side and the value is
written on the right side of equal sign.
 Syntax:
 type_name variable =value

Examples:
 int a =20;
 float avg =2.13;
“getch( )“ function

 The word getch stands for get character.


 The getch function is used to input single character from the
user.
 When this function is execute, it waits for any key to be pressed.
 The character entered by the user is not displayed on the screen.
 The function getch() is defined in the header file conio.h.
 This header file must be included in the program to use this
function.

Syntax: getch();
Decision Constructs
A statement used to control the flow of execution in a
program is called control structure.
 The instructions in a program can be organized in three
kinds of control structures to control execution flow.

These control structures are as follows:


 Sequence structure (straight line paths)
 Selection structure (one or many branches)
 Loop structure (repetition of a set of activities)
Sequence structure (straight line paths)

 In sequence structure , the


statements are executed in
the same order in which they
are specified in program.
 The control flows from one
statement to other logical
sequence.
 All statements are executed
exactly once.
 It means that no statement is
skipped and no statement is
executed more than once.
Selection structure (one or many branches)

 A selection structure selects a


statement or set of statements to
execute on the basis of a condition.
 Statement or set of statements is
executed when a particular condition
is true and ignored when the
condition is false.
 Branching Statements are included in
this control structure. Following
branching statements are supported
by C++ :
 if statement.
 if-else statement.
 switch statement
Loop structure (repetition of a set of activities)

 A repetition structure executes


a statement or set of
statements repeatedly.
 It is also known as iteration
structure or loop.
 The repetition structures
include.
 for loop
 while loop
 do-while loop
Flowchart
A flowchart is a diagram that depicts the “flow” of a
program.
 Flowchart is a diagrammatic representation of an
algorithm.
 Flowchart are very helpful in writing program and
explaining program to others.
 Flowchart symbols are specific shapes used to create a
visual representation of a program.
Symbols representation

 What do each of the following symbols represent?


Answer

 What do each of the following symbols represent?

Decision
Terminal

Input/Output
Operation Connector

Process Module
Review

 What type of structure is this?


Answer

 Sequence structure

13
Review

 What type of structure is this?


Answer

 Selection structure / Decision

15
Review

 What type of structure is this?


Answer

 Loops structure / Repetition

17
The Basic If Statement

 Syntax
if (Expression)
Action
Expression

 If the Expression is true then


execute Action true false

 Action is either a single statement


or a group of statements within
Action
braces
Example

if (Value < 0) {
Is our number negative?
Value = -Value;
}
If Value is less than Value < 0
zero then we need to
update its value to
true false
that of its additive
inverse If Value is not less
than zero then our
Value = -Value number is fine as is

Our number is
now definitely
nonnegative
The If-Else Statement
 Syntax

if (Expression)
Action1
else
Action2 Expression
 If Expression is true then execute
Action1 otherwise execute Action2 true false

if (v == 0) {
Action1 Action2
cout << "v is 0";
}
else {
cout << "v is not 0";
}
Finding the Max

cout << "Enter two integers: ";


int Value1;
int Value2;
cin >> Value1 >> Value2;
int Max;
if (Value1 < Value2) {
Max = Value2;
}
else {
Max = Value1;
}
cout << "Maximum of inputs is: " << Max << endl;
Finding the Max
Is Value2 larger than Value1

Yes, it is . So Value2 is
larger than Value1. In
this case, Max is set No, its not. So Value1
to Value2 is at least as large as
Value2. In this case,
Value1 < Value2 Max is set to Value1
true false

Max = Value2 Max = Value1

Either case, Max is set


correctly
An If-Else-If Statement / Nested if…else
statements
 One inside another, test for multiple cases
 Once a condition met, other statements are skipped

 if( studentGrade >= 90 )


cout << "A";
else if (studentGrade >= 80 )
cout << "B";
else if (studentGrade >= 70 )
cout << "C";
else if ( studentGrade >= 60 )
cout << "D";
else
cout << "F";
An If-Else-If Statement

if ( number < 0 ){
cout << number << " is negative" << endl;
}
else if ( number > 0 ) {
cout << number << " is positive" << endl;
}
else {
cout << number << " is zero" << endl;
}

You might also like