SlideShare a Scribd company logo
1 of 9
C++ Programming Club
      Lecture 2
• If Else Statement
• The if-else structure directs the computer to
  select between two statements based on the
  result of a comparison.

For example:
If (n<0)
 cout”The number is negative”;
Else
Cout<<“The number is positive”;
• Logical Operators
• Use Of Else if:
• If there are more than 2 conditions then we can
  use else if with the condition inside ().
• For Example:

 if (grade>80 && grade<100)
Cout<<“Excellent”;
Else if ( grade>70 && grade<80)
Cout<<“Good”
Else
cout<<“Average”
• Another Problem
Write a Program by using if-else statement for
the following data:
Specification Status           Code
Space exploration                S
Military grade                  M
Commercial grade                 C
Toy grade                        T
•      Solution:
#include <iostream>
using namespace std;
void main()
{
char code; // the variable is named code and it is in the form of a character or alphabet
cout << "Enter a specification code: "; //output on the screen
cin >> code;                             //input

if (code == 'S')                       // if we enter S the following will be the o/p and likewise for
other
cout << "The item is space exploration grade."; //alphabet(code)
else if (code == 'M')
cout << "The item is military grade.";
else if (code == 'C')
cout << "The item is commercial grade.";
else if (code == 'T')
cout << "The item is toy grade.";
else
cout << "An invalid code was entered."; // if we type an alphabet other than S,M,C,T this will be
the o/p
cout << endl;                               // on the screen
system ("pause");
}
• Homework
1-If an angle is equal to 90 degrees, print the message “The angle is a
right angle”;
else, print the message “The angle is not a right angle.”

2-(Data Processing) A student’s letter grade is calculated according to
the following schedule:
Numerical Grade                                       Letter Grade
Greater than or equal to 90                                A
Less than 90 but greater than or equal to 80                B
Less than 80 but greater than or equal to 70                C
Less than 70 but greater than or equal to 60                D
Less than 60                                               F

Using this information, write a C++ program that accepts a student’s
numerical grade,converts the numerical grade to an equivalent letter
grade, and displays the letter grade.
Lets Practice This Problem….
• A certain waveform is 0 volts for time less
  than 2 seconds and 3 volts for time equal to or
  greater than 2 seconds. Write a C++ program
  that accepts time in the variable named time
  and displays the appropriate voltage,
  depending on the input value.
• Solution
#include<iostream>
using namespace std;
void main ()
{
        int time;     //variable is called time and is in the form of integer
        cout<<"Enter time :"; //display on the screen
        cin>>time;               //input
        if (time<2)             //selection statement
                  cout<<"0 volts";
        else if (time>=2)
                  cout<<"3 volts";
        cout<<endl;            //output
          system ("pause");
}

More Related Content

What's hot (20)

Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
C++ quik notes
C++ quik notesC++ quik notes
C++ quik notes
 
algorithm
algorithmalgorithm
algorithm
 
Control structures
Control structuresControl structures
Control structures
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
 
c++
c++c++
c++
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Programming C Part 03
Programming C Part 03Programming C Part 03
Programming C Part 03
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 

Similar to C++ Programming Club-Lecture 2

Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_ifTAlha MAlik
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Lecture # 1 introduction revision - 1
Lecture # 1   introduction  revision - 1Lecture # 1   introduction  revision - 1
Lecture # 1 introduction revision - 1SajeelSahil
 
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
Lecture # 1 - Introduction  Revision - 1 OOPS.pptxLecture # 1 - Introduction  Revision - 1 OOPS.pptx
Lecture # 1 - Introduction Revision - 1 OOPS.pptxSanaullahAttariQadri
 
Switch.pptx
Switch.pptxSwitch.pptx
Switch.pptxAliAbro7
 
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.pptEPORI
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++Hadziq Fabroyir
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.pptTekle12
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdfT17Rockstar
 

Similar to C++ Programming Club-Lecture 2 (20)

Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Lecture # 1 introduction revision - 1
Lecture # 1   introduction  revision - 1Lecture # 1   introduction  revision - 1
Lecture # 1 introduction revision - 1
 
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
Lecture # 1 - Introduction  Revision - 1 OOPS.pptxLecture # 1 - Introduction  Revision - 1 OOPS.pptx
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
 
Switch.pptx
Switch.pptxSwitch.pptx
Switch.pptx
 
C++ basic.ppt
C++ basic.pptC++ basic.ppt
C++ basic.ppt
 
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
 
C++basics
C++basicsC++basics
C++basics
 
C++basics
C++basicsC++basics
C++basics
 
c++basiccs.ppt
c++basiccs.pptc++basiccs.ppt
c++basiccs.ppt
 
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
 
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++
 
Ch3.1
Ch3.1Ch3.1
Ch3.1
 
Statement
StatementStatement
Statement
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdf
 

More from Ammara Javed

More from Ammara Javed (15)

Counters
CountersCounters
Counters
 
Registers
RegistersRegisters
Registers
 
Flip flops
Flip flopsFlip flops
Flip flops
 
Logic Gates
Logic GatesLogic Gates
Logic Gates
 
Programmable logic devices
Programmable logic devicesProgrammable logic devices
Programmable logic devices
 
Maxterms
MaxtermsMaxterms
Maxterms
 
Karnaugh Maps
Karnaugh MapsKarnaugh Maps
Karnaugh Maps
 
Demultiplexers
DemultiplexersDemultiplexers
Demultiplexers
 
Minterms
MintermsMinterms
Minterms
 
Multiplexer
MultiplexerMultiplexer
Multiplexer
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Encoders
EncodersEncoders
Encoders
 
Decoders
DecodersDecoders
Decoders
 
Circuit Analysis-
Circuit Analysis-Circuit Analysis-
Circuit Analysis-
 
The Impact Of Holy Quran In Our Everyday Life
The Impact Of Holy Quran In Our Everyday LifeThe Impact Of Holy Quran In Our Everyday Life
The Impact Of Holy Quran In Our Everyday Life
 

C++ Programming Club-Lecture 2

  • 2. • If Else Statement • The if-else structure directs the computer to select between two statements based on the result of a comparison. For example: If (n<0) cout”The number is negative”; Else Cout<<“The number is positive”;
  • 4. • Use Of Else if: • If there are more than 2 conditions then we can use else if with the condition inside (). • For Example: if (grade>80 && grade<100) Cout<<“Excellent”; Else if ( grade>70 && grade<80) Cout<<“Good” Else cout<<“Average”
  • 5. • Another Problem Write a Program by using if-else statement for the following data: Specification Status Code Space exploration S Military grade M Commercial grade C Toy grade T
  • 6. Solution: #include <iostream> using namespace std; void main() { char code; // the variable is named code and it is in the form of a character or alphabet cout << "Enter a specification code: "; //output on the screen cin >> code; //input if (code == 'S') // if we enter S the following will be the o/p and likewise for other cout << "The item is space exploration grade."; //alphabet(code) else if (code == 'M') cout << "The item is military grade."; else if (code == 'C') cout << "The item is commercial grade."; else if (code == 'T') cout << "The item is toy grade."; else cout << "An invalid code was entered."; // if we type an alphabet other than S,M,C,T this will be the o/p cout << endl; // on the screen system ("pause"); }
  • 7. • Homework 1-If an angle is equal to 90 degrees, print the message “The angle is a right angle”; else, print the message “The angle is not a right angle.” 2-(Data Processing) A student’s letter grade is calculated according to the following schedule: Numerical Grade Letter Grade Greater than or equal to 90 A Less than 90 but greater than or equal to 80 B Less than 80 but greater than or equal to 70 C Less than 70 but greater than or equal to 60 D Less than 60 F Using this information, write a C++ program that accepts a student’s numerical grade,converts the numerical grade to an equivalent letter grade, and displays the letter grade.
  • 8. Lets Practice This Problem…. • A certain waveform is 0 volts for time less than 2 seconds and 3 volts for time equal to or greater than 2 seconds. Write a C++ program that accepts time in the variable named time and displays the appropriate voltage, depending on the input value.
  • 9. • Solution #include<iostream> using namespace std; void main () { int time; //variable is called time and is in the form of integer cout<<"Enter time :"; //display on the screen cin>>time; //input if (time<2) //selection statement cout<<"0 volts"; else if (time>=2) cout<<"3 volts"; cout<<endl; //output system ("pause"); }