際際滷

際際滷Share a Scribd company logo
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)
 coutThe 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 students 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 students
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)
Sena Nama
C++ programming
C++ programmingC++ programming
C++ programming
viancagerone
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
Debre Tabor University
C++ quik notes
C++ quik notesC++ quik notes
C++ quik notes
argusacademy
algorithm
algorithmalgorithm
algorithm
Divya Ravindran
Control structures
Control structuresControl structures
Control structures
Isha Aggarwal
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
Widad Jamaluddin
c++
c++c++
c++
Danish Noor
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
indra Kishor
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
Md. Imran Hossain Showrov
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
Programming C Part 03
Programming C Part 03Programming C Part 03
Programming C Part 03
Raselmondalmehedi
C++loop statements
C++loop statementsC++loop statements
C++loop statements
Muhammad Uzair Rasheed
Lecture03
Lecture03Lecture03
Lecture03
elearning_portal
L畉p tr狸nh C
L畉p tr狸nh CL畉p tr狸nh C
L畉p tr狸nh C
Viet NguyenHoang
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
Adam Mukharil Bachtiar
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
kinan keshkeh
L畉p tr狸nh C
L畉p tr狸nh CL畉p tr狸nh C
L畉p tr狸nh C
Viet NguyenHoang

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

Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman 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 Joby
GrejoJoby1
Computational PhysicsssComputational Physics.pptx
Computational PhysicsssComputational Physics.pptxComputational PhysicsssComputational Physics.pptx
Computational PhysicsssComputational Physics.pptx
khanzasad009
Lecture # 1 introduction revision - 1
Lecture # 1   introduction  revision - 1Lecture # 1   introduction  revision - 1
Lecture # 1 introduction revision - 1
SajeelSahil
Example Programs of CPP programs ch 1.pptx
Example Programs of CPP programs ch 1.pptxExample Programs of CPP programs ch 1.pptx
Example Programs of CPP programs ch 1.pptx
DrVikasMahor
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
SanaullahAttariQadri
Switch.pptx
Switch.pptxSwitch.pptx
Switch.pptx
AliAbro7
C++ basic.ppt
C++ basic.pptC++ basic.ppt
C++ basic.ppt
SityogInstituteOfTec1
C++ Condition C++ Conditionsd.pptxs.pptx
C++ Condition C++ Conditionsd.pptxs.pptxC++ Condition C++ Conditionsd.pptxs.pptx
C++ Condition C++ Conditionsd.pptxs.pptx
MAHERMOHAMED27
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
TatyaTope4
C++basics
C++basicsC++basics
C++basics
aamirsahito
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
EPORI
C++basics
C++basicsC++basics
C++basics
JosephAlex21
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
Infotech27
c++basiccs.ppt
c++basiccs.pptc++basiccs.ppt
c++basiccs.ppt
RaghavendraMR5
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
Farhan Ab Rahman
#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
Ch3.1
Ch3.1Ch3.1
Ch3.1
aamirsahito
Statement
StatementStatement
Statement
Ahmad Kamal
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman 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 Joby
GrejoJoby1
Computational PhysicsssComputational Physics.pptx
Computational PhysicsssComputational Physics.pptxComputational PhysicsssComputational Physics.pptx
Computational PhysicsssComputational Physics.pptx
khanzasad009
Lecture # 1 introduction revision - 1
Lecture # 1   introduction  revision - 1Lecture # 1   introduction  revision - 1
Lecture # 1 introduction revision - 1
SajeelSahil
Example Programs of CPP programs ch 1.pptx
Example Programs of CPP programs ch 1.pptxExample Programs of CPP programs ch 1.pptx
Example Programs of CPP programs ch 1.pptx
DrVikasMahor
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
SanaullahAttariQadri
Switch.pptx
Switch.pptxSwitch.pptx
Switch.pptx
AliAbro7
C++ Condition C++ Conditionsd.pptxs.pptx
C++ Condition C++ Conditionsd.pptxs.pptxC++ Condition C++ Conditionsd.pptxs.pptx
C++ Condition C++ Conditionsd.pptxs.pptx
MAHERMOHAMED27
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
TatyaTope4
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
EPORI
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
Infotech27
#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

More from Ammara Javed (15)

Counters
CountersCounters
Counters
Ammara Javed
Registers
RegistersRegisters
Registers
Ammara Javed
Logic Gates
Logic GatesLogic Gates
Logic Gates
Ammara Javed
Programmable logic devices
Programmable logic devicesProgrammable logic devices
Programmable logic devices
Ammara Javed
Maxterms
MaxtermsMaxterms
Maxterms
Ammara Javed
Karnaugh Maps
Karnaugh MapsKarnaugh Maps
Karnaugh Maps
Ammara Javed
Demultiplexers
DemultiplexersDemultiplexers
Demultiplexers
Ammara Javed
Minterms
MintermsMinterms
Minterms
Ammara Javed
Multiplexer
MultiplexerMultiplexer
Multiplexer
Ammara Javed
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
Ammara Javed
Encoders
EncodersEncoders
Encoders
Ammara Javed
Decoders
DecodersDecoders
Decoders
Ammara Javed
Circuit Analysis-
Circuit Analysis-Circuit Analysis-
Circuit Analysis-
Ammara Javed
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
Ammara Javed

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) coutThe 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 students 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 students 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"); }