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

If Sec1

The document provides examples of conditional statements and switch case statements in C++. It includes 4 examples that demonstrate: 1) Using if/else to compare two integers and print which is larger 2) Using if/else to check if an integer is even or odd 3) Using if/else to check if an integer is positive or negative 4) Using if/else or switch case to build a basic calculator that performs addition, subtraction, multiplication, or division on two numbers

Uploaded by

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

If Sec1

The document provides examples of conditional statements and switch case statements in C++. It includes 4 examples that demonstrate: 1) Using if/else to compare two integers and print which is larger 2) Using if/else to check if an integer is even or odd 3) Using if/else to check if an integer is positive or negative 4) Using if/else or switch case to build a basic calculator that performs addition, subtraction, multiplication, or division on two numbers

Uploaded by

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

Conditions:

1- if and else condition


b. if (condition)
a. if (condition)
{statement}
{statement}
else

{statement}

c. if (condition)

{statement}

else if (condition)

{statement}

else

{statement}

2- Switch

Switch (expression)

case constant : statement; break;

...

default : statement; break;

1
EX1: write a c++ program that reads two integers and prints their maximum

include <iostream>
using namespace std;
int main()
{ int A,B;
cout<<"enter first number";
cin>>A;
cout<<"enter second number";
cin>>B;
if (A>B)
cout<<"A is bigger ";
else
cout<<"B is bigger ";

system("pause"); }

EX2: Write c++ program to read an integer number and then determine its type
odd or even (Q14 sheet 3)

#include <iostream>
using namespace std;

int main()
{
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

system("pause"); }

2
EX3: Write c++ program to read an integer number and then determine its
type positive or negative. (Q13 sheet 3)

#include <iostream>
using namespace std;

int main()
{
int x;

cout << "Enter a number: ";


cin >> x;

if ( x>0)
cout << x << " is positive.";
else if(x<0)
cout << x << " is negative.";
else
cout << x << " equals zero.";
system("pause"); }

3
EX4: Write a program to make a simple calculator, the program will ask the user
to enter 2 numbers and the operation symbol (+, -, * or /)then the program will
perform the required operation on the two numbers and print the result
(Q11 sheet 3)

#include <iostream>
using namespace std;
int main(){
Float first,second,result;
Char symbol;
Cout<<“enter first number:’;
Cin>>first;
Cout<<“enter operation symbol(*, /, + or -):”;
Cin>>symbol;
Cout<<“enter second number:”;
Cin>>second;
If(symbol == ‘+’) result = first + second;
else if (symbol == ‘-’) result = first - second;
else if (symbol == ‘*’) result = first * second;
else if (symbol == ‘/’)
{ if(second == 0)
{cout<<“you cannot divide by zero\n”; }
else result = first / second; }

Else { cout<<“you entered wrong operation symbol\n”; return2;}


Cout<<“result of”<<first<<symbol<<second<<“=“<<result<<“\n”;

system("PAUSE");}

4
Solution using switch-case

#include <iostream>
using namespace std;
int main(){
float first, second, result;
char symbol;
cout<<"enter first number:";
cin>>first;
cout<<"enter operation symbol(*, /, + or -):";
cin>>symbol;
cout<<"enter second number:";
cin>>second;
switch(symbol)
{
case'+': result = first + second; break;
case'-': result = first - second; break;
case'*': result = first * second; break;
case'/': if(second == 0)
{cout<<"you can not divide by zero\n";}
else result = first / second; break;
default: cout<<"you entered wrong operation symbol\n";}
cout<<"result of"<<first<<symbol<<second<<"="<<result<<"\n";

system("PAUSE");}

You might also like