Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

CONDITIONALSTATEMENTS

UNIVERSITY OF THE PUNJAB


GUJRANWALA CAMPUS
SUBMITTED TO:
Dr . Naveed Ahmed Jhammat
SUBMITTED BY:
 BIT22003 Namra Ajmal
 BIT22004 Sidra Yousaf
 BIT22009 Samahar Waqar
 BIT22010 Noor e Sehar
 BIT22016 Amna Faiser
 BIT22032 Maham Shehzadi
Conditional statements :
 A conditional statement tells a program to
execute an action depending on whether a
condition is true or false . Conditional
statements play a crucial role in controlling the
flow of a program and making it more flexible
and responsive.
 Types of conditional statements
 If Statement
 If-else Statement
 If-else if Statement
 Nested Conditional Statements
 Switch Statement
If statement:
 Conditional Execution: The "if
statement" enables the execution
of a block of code based on a
specified condition.
 When an if statement is written
without braces, it only covers the
immediately following statement.
 The absence of braces can lead to
ambiguity and unexpected
behavior .For example:
if (condition)
statement1;
statement2; // This statement is
not part of the if block!
Example:
.
#include <iostream>
using namespace std;
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
printf("I am Not in if");
}
IF-ELSE STATEMENT
 The "if-else statement"
allows for two possible
outcomes based on a
condition .
 If the condition is true,
the code within the "if"
block is executed; if false,
the code within the "else"
block is executed.
..
Ternary operator
The ternary operator is a type of conditional
statement. It is a shorthand way of expressing a simple
if-else statement in a single line of code .
The basic syntax of the ternary operator is:
(condition) ? Statement 1 : statement 2;
If the condition is true then statement 1 is executed
and if the condition is false statement 2 will be
executed
int number = 7;
int result = (number % 2 == 0) ? "Even" : "Odd";
cout << "The number is " << result << endl;
.
If-else if statement:
 The "if-else if" statement
enables the evaluation of
multiple conditions in
sequence.
 If the first "if" condition is
true, the associated code
block is executed . If false,
the next "else if" condition is
checked, and so on. If none
of the conditions is true, the
code within the final "else"
block is executed.
SYNTAX: EXAMPLE:
if (condition)

statement;

else if (condition)

statement;

else

statement;
Nested If statement:
 Nested conditional
statements involve
placing one or more
conditional statements
inside another.
 This allows for more
complex decision-making
by evaluating multiple
conditions in a
hierarchical manner.
 We use this when we
want to implement
multilayer conditions.
SYNTAX EXAMPLE:
if (condition 1 )
{ .
// Executes when condition 1
is true
if (condition 2 )
{
// Executes when condition 2
is true
}
else
{
// Executes when condition 2
is false
}
Switch Statement:
A switch statement is a control
flow statement that allows you
to select one of many code
blocks to be executed based on
the value of a given expression.
The break statement is used to
exit the switch block. Without
break, the control falls
through to the next case,
which may not be the
intended behavior.
#include <iostream>
.
EXAMPLE: using namespace std;
int main() {
int month;
cout << "Enter a month (1-12): ";
cin >> month;
switch (month) {
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
// ... (continue for the rest of the months)
case 12:
cout << "December";
break;
default:
cout << "Invalid month";
}
return 0;
}
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.
Basically, break statements are
used in situations when we are
not sure about the actual
number of iterations for the
loop or we want to terminate
the loop based on some
condition.
Continue:
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.
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
APPLICATIONS OF CONDITIONAL
STATEMENTS
Grading system could be done through if-else if
statements.
A basic calculator could be formed by using
switch statements.
Classifies individuals into different age
categories.
 Simulates a traffic light scenario and provides
instructions based on the entered signal.
Temperature conversion could also be done
through the decision making statements.

You might also like