0% found this document useful (0 votes)
27 views

شرح if condition

The document discusses control flow in C++ using if/else conditional statements, ternary operators, switch/case statements, and nested conditional logic. It provides syntax examples and sample code to create three applications: 1) an award system using switch/case, 2) a discount calculator using switch/case, and 3) a simple calculator using both if/else and switch/case statements.

Uploaded by

Mohamed Xcane
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)
27 views

شرح if condition

The document discusses control flow in C++ using if/else conditional statements, ternary operators, switch/case statements, and nested conditional logic. It provides syntax examples and sample code to create three applications: 1) an award system using switch/case, 2) a discount calculator using switch/case, and 3) a simple calculator using both if/else and switch/case statements.

Uploaded by

Mohamed Xcane
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/ 15

30#Control Flow – If Condition Introduction

/*

Control Flow

- If Condition Introduction

Syntax

if (Condition Is True)

// Do Something

*/

#include <iostream>

using namespace std;

int main()

int age = 20;

cout << "Welcome\n";

if (age < 18) // False

cout << "Beware\n";

cout << "See You\n";

return 0;

}
31#Control Flow – If, Else If, Else

/*

Control Flow

- If ... else if ... else

Syntax

if (Condition Is True)

// Do Something

*/

#include <iostream>

using namespace std;

int main()

int age = 15;

int points = 450;

int rank = 4;

if (age >= 18)

{
cout << "Welcome Your Age Is Ok\n";

else if (points > 500)

cout << "Welcome Your Points Is Ok\n";

else if (rank > 5)

cout << "Welcome Your Rank Is Ok\n";

else

cout << "Iam Sorry\n";

return 0;

}
32#Control Flow – Nested If Conditions

/*

Control Flow

- Nested If Conditions

*/

#include <iostream>

using namespace std;

int main()

int age = 25;

int points = 1500;

if (age >= 18)

cout << "Welcome Your Age Is OK\n";

if (points >= 1000)

cout << "You Are VIP\n";

return 0;

}
33#Control Flow – Ternary Conditional Operator

/*

Control Flow

- Ternary Operator

Syntax

(Condition) ? True : False;

*/

#include <iostream>

using namespace std;

int main()

int age = 15;

if (age >= 18)

cout << "Your Age Is OK\n";

else

cout << "Your Age Is Not OK\n";

}
cout << (age >= 18 ? "Age Is OK\n" : "Age Is Not OK\n");

string msg = age >= 18 ? "Age Is OK\n" : "Age Is Not OK\n";

cout << msg;

return 0;

}
34#Control Flow – Nested Ternary Operator

/*

Control Flow

- Nested Ternary Operator

- Alternate Syntax For If Condition

Syntax

(Condition Is True) ? True : False;

*/

#include <iostream>

using namespace std;

int main()

int age = 15;

int points = 450;

if (age >= 18)

cout << "OK\n";

else
{

if (points >= 500)

cout << "OK Because Of Points\n";

else

cout << "No Age Or Points\n";

cout << (age >= 18 ? "OK\n" : (points >= 500 ? "OK P\n" : "No P\n"));

cout << (points >= 500 ? "OK P\n" : "No P\n");

if (age >= 18)

cout << "OK\n";

else

cout << "Not OK\n";

return 0;

}
Control Flow – Switch, Case
/*

Control Flow

- Switch

*/

#include <iostream>

using namespace std;

int main()

int day;

cout << "Choose A Day From 1 To 25\n";

cin >> day;

if (day == 1)

cout << "Open From 08:00 To 14:00";

else if (day == 2)

cout << "Open From 08:00 To 14:00";

else if (day == 3)
{

cout << "Open From 10:00 To 16:00";

else

cout << "Closed";

switch (day)

case 1:

case 2:

cout << "Open From 08:00 To 14:00";

break;

case 3:

cout << "Open From 10:00 To 16:00";

break;

default:

cout << "Closed";

return 0;

}
Switch Training – Create Three Application
/*

Control Flow

- Switch Trainings

--- Award System Application

--- Discount Application

--- Simple Calculator Application

*/

#include <iostream>

using namespace std;

int main()

// App 1 => Award System Application

int num;

cout << "Type The Number\n";

cin >> num;

switch (num)

case 100:

case 110:
case 120:

cout << "Congratz For The iPhone";

break;

case 200:

cout << "Congratz For The iPad";

break;

case 300:

cout << "Congratz For The TV";

break;

case 400:

cout << "Congratz For The KeyChain";

break;

default:

cout << "No Award For This Number";

// App 2 => Discount Application

int price = 100;

int discount = 10;

int years;

cout << "Type The Number Of Years in Company\n";

cin >> years;

switch (years)
{

case 1:

discount = 20;

break;

case 2:

discount = 40;

break;

case 3:

discount = 80;

break;

cout << "The Price Is " << price - discount << "\n";

// App 3 => Simple Calculator

int n1, n2, op;

cout << "Type Number One\n";

cin >> n1;

cout << "Type Number Two\n";

cin >> n2;

cout << "Choose Operation Number\n";

cout << "[1] +\n";

cout << "[2] -\n";

cout << "[3] /\n";


cout << "[4] *\n";

cin >> op;

if (op == 1)

cout << num_one + num_two << "\n";

else if (op == 2)

cout << num_one - num_two << "\n";

else if (op == 3)

cout << num_one / num_two << "\n";

else if (op == 4)

cout << num_one * num_two << "\n";

else

cout << "Operation Is Not Valid\n";

switch (op)
{

case 1:

cout << n1 << " + " << n2 << " = " << n1 + n2 << "\n";

break;

case 2:

cout << n1 << " - " << n2 << " = " << n1 - n2 << "\n";

break;

case 3:

cout << n1 << " / " << n2 << " = " << n1 / n2 << "\n";

break;

case 4:

cout << n1 << " * " << n2 << " = " << n1 * n2 << "\n";

break;

default:

cout << "Operation Is Not Valid\n";

return 0;

You might also like