0% found this document useful (0 votes)
13 views29 pages

Lopp

Uploaded by

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

Lopp

Uploaded by

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

What is the control structure?

Control structures / Control statements enable a programmer to


determine the order in which program statements are executed. These
control structures allow you to do two things: 1) skip some statements
while executing others, and 2) repeat one or more statements while
some condition is true.
S1 f
S2 true
S3 true

Conditional Structure:
Decision-making in C++ involves the usage of conditional
statements (also called decision control statements) to
execute specific blocks of code primarily based on given
situations and their results.
So basically, in decision-making, we evaluate the conditions
and make a decision about which part of the code should be
executed or not. It allows selective code execution which is
crucial for controlling the flow of a program and making it
more dynamic.
Types of Decision-Making Statements in
C++
In C++, the following decision-making statements are
available:
1. if Statement
2. if-else Statement
3. if-else-if
4. Nested if Statement
5. switch Statement
6. Conditional Operator
7. Jump Statements: break, continue, go, return

i go to school
if i go to school
1. if in C++:
In C++, the if statement is the simplest decision-making statement. It
allows the execution of a block of code if the given condition is true. The
body of the ‘if’ statement is executed only if the given condition is true.

Syntax of if in C++
if (condition) {
// code to be executed if the condition is true
}
Here if the condition is true then the code inside the if block
will be executed otherwise not.
Flowchart of if in C++

Example of if in C++

The below example demonstrates the use of the if statement


by finding if the age of a person is greater than 18 or not. if
the condition is true then he/she is allowed to vote.

// C++ program to find if the age of a person id greater


// than 18 or not
#include <iostream>
using namespace std;
int main()
{
int age = 19;
if (age > 18) {
cout << "allowed to vote" << endl;
}
return 0;
}

2. if-else in C++

The if-else decision-making statement allows us to make a decision based on the


evaluation of a given condition. If the given condition evaluates to true then the
code inside the ‘if’ block is executed and in case the condition is false, the code
inside the ‘else’ block is executed.

Syntax of if-else in C++

if (condition) {
// Code to be executed if the condition is true
}
else {
// Code to be executed if the condition is false
}
Flowchart of if-else in C++

Flow diagram of if else

Example of if-else in C

The below example demonstrates the use of an if-else statement to find if the
given number is positive or nonpositive.

 C++

// C++ program to find if the given number is positive or


// non positive

#include <iostream>
using namespace std;
int main()
{
int num = 5;

// Using if-else to determine if the number is positive


// or non positive
if (num > 0) {
cout << "number is positive." << endl;
}
else {
cout << "number is non-positive." << endl;
}
return 0;
}

Output
number is positive.

3. if-else if in C++
The if-else-if statements allow us to include additional situations after the
preliminary if condition. The ‘else if’ condition is checked only if the above
condition is not true. , and the `else` is the statement that will be executed if none
of the above conditions is true. If some condition is true, then no only the
associated block is executed.
Syntax if-else-if Ladder
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true
}
else {
// code to be executed if both the condition is false
}
We can use multiple else if statements with an if-else pair to specify different
conditions.
Flowchart of if-else-if Ladder in C++
flow diagram of the if-else-if ladder

Example of if-else-ladder in C++


The below example demonstrates the use of an if-else-if ladder. In the program,
you are given an age and if the age is less the 13 print child, if the age is between
13 to 18 print the growing stage, else print adult.
 C++

// C++ program to find if the person is child, growing age


// or adult using if else-if ladder

#include <iostream>
using namespace std;

int main()
{
int age = 18;

// if this condition is true child is printed


if (age < 13) {
cout << "child" << endl;
}
// if above above if statement is not true then we check
// this else if condition if it evalutes to true print
// growing age
else if (age >= 1 and age <= 18) {
cout << "Growing stage" << endl;
}

// if none of above condition is true print adult


else {
cout << "adult" << endl;
}
return 0;
}

Output
Growing stage

C++ Logical Operators


Last Updated : 31 Jul, 2023



In C++ programming languages, logical operators are symbols that allow you to
combine or modify conditions to make logical evaluations. They are used to
perform logical operations on boolean values (true or false).
In C++, there are three logical operators:
1. Logical AND ( && ) Operator
2. Logical OR ( || ) Operator
3. Logical NOT ( ! ) Operator
Let’s discuss each of the operators in detail.
1. Logical AND Operator ( && )
The C++ logical AND operator (&&) is a binary operator that returns true if both
of its operands are true. Otherwise, it returns false. Here’s the truth table for the
AND operator:
Operand 1 Operand 2 Result

true true true

true false false

false true false


Operand 1 Operand 2 Result

false false false

Note: In C, false is represented by 0 while the true is represented as any non-zero


value, generally 1.
Syntax of Logical AND
expression1 && expression2

Example of Logical AND in C++


 C++

// C++ Program to illustrate the logical AND Operator


#include <iostream>
using namespace std;

int main()
{

// initialize variables
int age = 25;
bool isStudent = true;

// Using AND operator in if condition


if (age > 18 && isStudent) {
cout << "You are eligible for a student discount."
<< endl;
}
else {
cout << "You are not eligible for a student "
"discount."
<< endl;

return 0;
}

Output
You are eligible for a student discount.

Explanation: In the above code, we have used AND operator in the if condition to
check whether the age is greater than 18 and the person is a check. If both
conditions are true, the message “You are eligible for a student discount.” will be
printed. Otherwise, the else statement is executed.
2. Logical OR Operator ( || )
The C++ logical OR operator ( || ) is a binary operator that returns true if at least
one of its operands is true. It returns false only when both operands are false.
Here’s the truth table for the OR operator:
Operand 1 Operand 2 Result

true true true

true false true

false true true

false false false

Syntax of Logical OR
expression1 || expression2

Example of Logical OR in C++


 C++

// C++ program to demonstrate the logical or operator


#include <iostream>
using namespace std;

int main()
{

int num = 7;

// using logical or for conditional statement


if (num <= 0 || num >= 10) {
cout
<< "The number is outside the range of 0 to 10."
<< endl;
}
else {
cout << "The number is between 0 to 10." << endl;
}

return 0;
}

Output
The number is between 0 to 10.
Explanation: In the above code, the condition num < 0 || num > 10 checks
whether the number is either less than equal to 0 or greater than equal to 10. If
either of these conditions is true, the message “The number is outside the range of
0 to 10.” will be printed otherwise else statement is printed.
3. Logical NOT Operator ( ! )
The C++ logical NOT operator ( ! ) is a unary operator that is used to negate the
value of a condition. It returns true if the condition is false, and false if the
condition is true. Here’s the truth table for the NOT operator:
Operand
1 Result

true false

false true

Syntax of Logical NOT


! expression

Example of Logical NOT in C++


 C++

// C++ program to illustrate the logical not operator


#include <iostream>
using namespace std;

int main()
{

bool isLoggedIn = false;

// using logical not operator


if (!isLoggedIn) {
cout << "Please log in to access this feature."
<< endl;
}
else {
cout << "Welcome to GeeksforGeeks!" << endl;
}

return 0;
}

Output
Please log in to access this feature.
Explanation: In the above code, the condition ‘!isLoggedIn’ checks whether the
user is not logged in. If the condition is true (i.e., the user is not logged in), the
message “Please log in to access this feature.” will be displayed otherwise else
statement will be printed.

4. Nested if-else in C++


The Nested if-else statement contains an ‘if’ statement inside another ‘if’
statement. This structure lets in more complex selection-making by way of
comparing multiple conditions. In this type of statement, multiple conditions are
checked, and then the body of the last if statement is executed.
Syntax of Nested if-else in C++
if (condition1) {
// code to be executed if condition 1 is true
if (condition2) {
// code to be executed when condition 2 is true.
}
else {
// code to be executed if condition1 is true but
condition2 is false.
}
}
else {
// code to be executed when condition 1 is false
}
Flowchart of Nested if-else
Flow diagram of Nested if-else

Example of Nested if-else in C++


The below example demonstrates the use of nested if-else. In the below program,
we are given a number and we have to check whether the number is positive or
negative or zero if the number is positive check whether it is even or odd.
 C++

// C++ program to check if the given number is positive,


// negative or zero if positive then check if it is even or
// odd

#include <iostream>
using namespace std;

int main()
{
int number = 44;
// to check if number is positive
if (number > 0) {

// to check if the positive number is even or odd


if (number % 2 == 0) {
cout << "positive and even number" << endl;
}
else {
cout << "positive and odd number" << endl;
}
}
// to check if the number is 0
else if (number == 0) {
cout << "the number is zero" << endl;
}
// to check if the number is negative
else {
cout << "the number is negative" << endl;
}
return 0;
}

Output
positive and even number

5. Switch Statement in C++


In C++, the switch statement is used when multiple situations need to be
evaluated primarily based on the value of a variable or an expression. switch
statement acts as an alternative to multiple if statements or if-else ladder and has
a cleaner structure and it is easy for handling multiple conditions.
Syntax of C++ switch
switch (expression) {
case value1:
// code to be executed if the value of expression is
value1.
break;
case value2:
// code to be executed if the value of expression is
value2.
break;
//…..
default:
// code to be executed if expression doesn't match any
case.
}
Flowchart of switch in C++
Flowchart of switch in C++

Example pf switch in C++


The below example demonstrates the use of switches in decision-making. In the
below program, we are given a character and you have to give output as per the
given condition: if the given input is A then print GFG, and if the given input is B
print GeeksforGeeks else print invalid input.
 C++

// C++ program to use switch case and print certain output


// based on some conditions

#include <iostream>
using namespace std;

int main()
{
char input = 'B';
switch (input) {
// if the input character is A then print GFG
case 'A':
cout << "GFG" << endl;
break;

// if the input character is B then print GeeksforGeeks


case 'B':
cout << "GeeksforGeeks" << endl;
break;
default:
// if th einput character is invalid then print
// invalid input
cout << "invalid input" << endl;
}
return 0;
}

Output
GeeksforGeeks

6. Ternary Operator ( ? : ) in C++


The conditional operator is also known as a ternary operator. It is used to write
conditional operations provided by C++. The ‘?’ operator first checks the given
condition, if the condition is true then the first expression is executed otherwise
the second expression is executed. It is an alternative to an if-else condition in C+
+.
Syntax of Ternary Operator in C++
condition ? expression1 : expression2
Flowchart of Conditional Operator in C++
Flow Diagram of Conditional operator

Example of Ternary Operator in C++


The below program demonstrates the use of a conditional operator to find the
maximum of two numbers.
 C++

// C++ program to demonstrate the use of ternary/conditional


// operator to find the max from two numbers

#include <iostream>
using namespace std;
int main()
{
int num1 = 10, num2 = 40;
int max;
// if the condition is true then num1 will be printed
// else num2 will printed
max = (num1 > num2) ? num1 : num2;
cout << max;
return 0;
}

Output
40

7. Jump Statements in C++


Jump statements are used to alter the normal flow of the code. If you want to
break the flow of the program without any conditions then these jump statements
are used. C++ provides four types of jump statements.
 break
 continue
 goto
 return
A) break
break is a control flow statement that is used to terminate the loop and switch
statements whenever a break statement is encountered and transfer the control to
the statement just after the loop.
Syntax
break;
break statement is generally used when the actual number of iterations are not
predefined so we want to terminate the loop based on some conditions.
Flowchart of break
Flow diagram of break

Example
The below example demonstrates the use of breaks to manage the control flow.
 C++
// C++ program to use break statement to break the loop when
// i become 3

#include <iostream>
using namespace std;

int main()
{
for (int i = 0; i < 5; i++) {
// if i become 3 then break the loop and move to
// next statement out of loop
if (i == 3) {
break;
}
cout << i << endl;
}
// next statements
return 0;
}

Output
0
1
2
B) continue
The continue statement is used to skip the loop body for the current iteration and
continue from the next iteration. Unlike the break statement which terminates the
loop completely, continue allows us just to skip one iteration and continue with
the next iteration.
Syntax
continue;
Flowchart of continue
Flow diagram of continue

Example
The below example demonstrates the use of continue to manage the control flow.
 C++

// C++ program to use continue statement to continue the


// loop when i become 3

#include <iostream>
using namespace std;

int main()
{
for (int i = 0; i < 5; i++) {
// if i become 3 then skip the rest body of loop and
// move next iteration
if (i == 3) {
continue;
}
cout << i << endl;
}
return 0;
}

Output
0
1
2
4
C) goto
It is a jump statement that is used to transfer the control to another part of the
program. It transfers the control unconditionally to the labeled statement.
Syntax
goto label;
// ...
label:
// Statement or block of code
Flowchart of goto
Flow Diagram of goto

Example
The below example demonstrates the use of the goto statement.
 C++

// C++ program to demonstrate the use of goto statement

#include <iostream>
using namespace std;

int main()
{
int age = 17;
if (age < 18) {
goto Noteligible;
}
else {
cout << "You can vote!";
}
Noteligible:
cout << "You are not eligible to vote!\n";
return 0;
}

Output
You are not eligible to vote!
Note Use of goto is generally avoided in modern programming practices because
it may disturb the readability of the code and make the code error-prone,
although it is still valid and used occasionally.
D) return
The return statement is used to exit the function immediately and optionally
returns a value to the calling function. It returns to the function from where it was
called and if it is the ‘main’ function then it marks the end of the execution. So
basically, return is a mechanism used to communicate the results back to the
calling function.
Syntax
return expression;
Flowchart of return
Flow diagram of return

Example
The below example demonstrates the use of a return statement to return a value
from a function.
 C++

// C++ program to use return statement to return the sum


// calculated by a function

#include <iostream>
using namespace std;

// Function to add two numbers and returns the result


int add(int a, int b)
{
int sum = a + b;
return sum; // Return the sum to the calling code
}
int main()
{
int res = add(3, 5);
cout << "The sum is: " << res << endl;

return 0;
}

Output
The sum is: 8

C++ Ternary or Conditional Operator


Last Updated : 31 Jul, 2023



In C++, the ternary or conditional operator ( ? : ) is the shortest form of writing
conditional statements. It can be used as an inline conditional statement in place of
if-else to execute some conditional code.
Syntax of Ternary Operator ( ? : )
The syntax of the ternary (or conditional) operator is:
expression ? statement_1 : statement_2;

As the name suggests, the ternary operator works on three operands where
 expression: Condition to be evaluated.
 statement_1: Statement that will be executed if the expression evaluates
to true.
 statement_2: Code to be executed if the expression evaluates to false.
// image
The above statement of the ternary operator is equivalent to the if-else statement
given below:
if ( condition ) {
statement1;
}
else {
statement2;
}

Example of Ternary Operator in C++


 C++
// C++ program to illustrate the use of ternary operator
#include <iostream>
using namespace std;

int main()
{

// creating a variable
int num, test = 40;

// assigning the value of num based on the value of test


// variable
num = test < 10 ? 10 : test + 10;

printf("Num - Test = %d", num - test);

return 0;
}

Output
Num - Test = 10

In the above code, we have used the ternary operator to assign the value of the
variable num depending upon the value of another variable named test.
Note: The ternary operator have third most lowest precedence, so we need to use
the expressions such that we can avoid errors due to improper operator
precedence management.

C++ Nested Ternary Operator


A nested ternary operator is defined as using a ternary operator inside another
ternary operator. Like if-else statements, the ternary operator can also be nested
inside one another.
Example of Nesting Ternary Operator in C++
In the below code, we will find the largest of three numbers using the nested
ternary operator.
 C++

// C++ program to find the largest of the three number using


// ternary operator
#include <iostream>
using namespace std;

int main()
{
// Initialize variable
int A = 39, B = 10, C = 23;

// Evaluate largest of three using ternary operator


int maxNum
= (A > B) ? ((A > C) ? A : C) : ((B > C) ? B : C);

cout << "Largest number is " << maxNum << endl;

return 0;
}

Output
Largest number is 39

You might also like