Department of Information Technology Control Structures...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Department of Information Technology Control Structures Prepared by: Ms Seetha BIT205 – Programming in C+ +
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Department of Information Technology Control Structures Logical Expressions Logical expressions/boolean expressions evaluate to true or false . Syntax for defining logical expression: bool var1,var2…,varn ; Boolean/Logical operators : && and || or ! not Example of declaring variables : bool P=true; bool Q=false; Evaluating P&&Q will give a false value Evaluating !(P||Q) will give a false value
Image of page 2
Department of Information Technology Control Structures Relational Operators C++ has the following relational operators: = = equal ! = not equal < less than > more than < = less than or equal to > = more than or equal to Example : int i=1,j=2,k=2; char c=‘2’,d=‘3’,e=‘2’; What the results for the following expressions? a) c==e b) i!=k c) j<k; d) d>e;
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Department of Information Technology Control Structures Common programming error (x = y) ... is perfectly legal in C++ but it does not do what you think it does! It assigns x to y but x is not checked against y for equality. Precendence : Unary operators e.g. -3, +4, !x multiplication(*), division(/), modulus(%) addition(+), subtraction(-) relational (>,<,>=,<=,!=) relational(==) logical and (&&) logical or (||) assignment (=)
Image of page 4
Department of Information Technology Control Structures The if Statement looks like this: if (logical expression) { // code to be executed if logical // expression is true } else // this part is optional { // code to be executed if logical // expression is false }
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Department of Information Technology Control Structures #include <iostream.h> int main() { cout<< “Please enter a number: “ <<flush; int Value; cin>>Value; if (Value < 0) Value =-Value; cout<<Value<<“ is positive”<<endl; return 0; } #include <iostream.h> int main() {
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern