0% found this document useful (0 votes)
16 views22 pages

Lecture 7

Uploaded by

elitelife029
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)
16 views22 pages

Lecture 7

Uploaded by

elitelife029
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/ 22

Programming

Fundamentals

By: Engr. Raheel Zaman


1. Conditional Operator in C++
 The conditional operator in C++ is a ternary
operator that allows you to evaluate a
condition and return one of two values based
on whether the condition is true or false. It is a
shorthand for an if-else statement and is often
used for simple decisions.
Syntax
 condition ? expression1 : expression2;

 condition: A Boolean expression that evaluates


to true or false.
 expression1: Executed and returned if the
condition is true.
 expression2: Executed and returned if the
condition is false.
 How It Works:

 The condition is evaluated.


 If condition is true, expression1 is executed.
 If condition is false, expression2 is executed.
Example 1
 #include <iostream>
 using namespace std;

 int main() {
 int a = 10, b = 20;

 // Using the conditional operator


 int max = (a > b) ? a : b;
 cout << "The larger value is: " << max << endl;
 return 0; }
 Output:

 The larger value is: 20


Example 2
 #include <iostream>
 using namespace std;

 int main() {
 int num = 5;
 // Using the conditional operator
 string result = (num % 2 == 0) ? "Even" : "Odd";

 cout << num << " is " << result << endl;
 return 0; }
 Output:

5 is Odd
2. Switch statement in C++
 The switch statement in C++ is a control
structure that allows you to choose one block
of code to execute out of many possible
options, based on the value of an expression.
It is an alternative to a series of if-else
statements when you are checking the same
variable for multiple values.
Syntax:
 switch (expression) {
 case constant1:
 // Code to execute if expression == constant1
 break;
 case constant2:
 // Code to execute if expression == constant2
 break;
 // Additional cases as needed
 default:
 // Code to execute if no case matches
 }
 expression: An integral or enumerated
type( user-defined data type) (e.g., int, char,
enum) whose value is compared against
constants.
 case constant:: Defines a specific value to
compare the expression against.
 break: Exits the switch statement. If omitted,
execution will "fall through" to the next case.
 default: Optional; executed if none of the case
values match the expression.
How It Works
 The expression is evaluated.
 The program compares the value of
expression with each case constant.
 When a match is found, the corresponding
code block is executed.
 If no match is found, the default block (if
present) is executed.
 The break statement stops execution of the
switch and prevents fall-through.
Example 1: Days of the Week
 int main() {
 int day = 3;

 switch (day) {
 case 1:
 cout << "Monday" << endl;
 break;
 case 2:
 cout << "Tuesday" << endl;
 break;
 case 3:
 cout << "Wednesday" << endl;
 break;
 case 4:
 cout << "Thursday" << endl;
 break;
 case 5:
 cout << "Friday" << endl;
 break;
 case 6:
 cout << "Saturday" << endl;
 break;
 case 7:
 cout << "Sunday" << endl;
 break;
 default:
 cout << "Invalid day" << endl; }
 return 0; }
Output:

 Wednesday
Limitations of switch

 Cannot use floating-point types (e.g., float,


double) in the expression.
 Cannot use complex conditions or ranges
(e.g., x > 5).
 Does not support string comparisons directly.
3. goto Statement in C++

 The goto statement in C++ is a control flow


statement that allows you to jump to a labeled
part of the code. It can be used to transfer
control unconditionally to a predefined label
within the same function. While it can simplify
certain types of code, its overuse is
discouraged as it often makes the program
harder to understand and debug.
Syntax

 goto label;
 // Some code
 label:
 // Code to execute

 label: A user-defined identifier followed by a


colon (:) that serves as the target of the goto
statement.
How It Works:
 When the goto statement is encountered,
control jumps to the line identified by the
label.
 The program skips over any code between the
goto statement and the label.
Example : Using goto to Jump
Forward
#include <iostream>
 using namespace std;

 int main() {
 cout << "Start of the program." << endl;
 goto skip; // Jump to the label 'skip'
 cout << "This line will be skipped." << endl;
 skip:
 cout << "This line is executed after the jump." <<
endl;
 return 0; }
Output:
 Start of the program.
 This line is executed after the jump.

You might also like