Mithat Konar
if student’s grade is greater than 60 print "Passed"
If the condition is true
the print statement is executed and program goes on to next statement. If the condition is false
the print statement is ignored and the program goes onto the next statement.
if
structure is a single-entry/single-exit structure.if (<expression>) <statement>;
if (grade > 60) cout << "Passed" << endl;
true
or false
value.true
or false
is a Boolean value.bool
type in C++>
greater than<
less than>=
greater than or equal to<=
less than or equal to==
equal to (equality)!=
not equal to (inequality)==
equality operator with the =
assignment operator. This is a common mistake and can cause hard-to-find errors!operator | associativity | type |
---|---|---|
() | left to right | parenthesis |
* / % | left to right | multiplicative |
+ - | left to right | additive |
<< >> | left to right | stream insertion/extraction |
< <= > >= | left to right | relational |
== != | right to left | equality |
= | right to left | assignment |
if student’s grade is greater than or equal to 60 print “Passed” else print "Failed"
If the condition is true the “Passed” print statement is executed and the program goes on to next statement. If the condition is false the “Failed” print statement is executed and the program goes onto the next statement.
if/else
structure is a single-entry/single-exit structure.if (<expression>) <statement>; else <statement>;
if (grade >= 60) cout << "Passed" << endl; else cout << "Failed" << endl;
if student’s grade is greater than or equal to 90 print "A" else if student’s grade is greater than or equal to 80 print "B" else if student’s grade is greater than or equal to 70 print "C" else if student’s grade is greater than or equal to 60 print "D" else print "F"
if student’s grade is greater than or equal to 90 print "A" else if student’s grade is greater than or equal to 80 print "B" else if student’s grade is greater than or equal to 70 print "C" else if student’s grade is greater than or equal to 60 print "D" else print "F"
if (grade >= 90) cout << "A" << endl; else if (grade >= 80) cout << "B" << endl; else if (grade >= 70) cout << "C" << endl; else if (grade >= 60) cout << "D" << endl; else cout << "F" << endl;
cout << "This is a simple statement." << endl;
{ x = 3 * y; cout << "The magic number is: " << x << endl; }
if (grade >= 60) cout << "Passed." << endl; else { cout << "Failed." << endl; cout << "You must take this course again." << endl; }
cout << "You must take this course again." << endl;
would be executed no matter what.
if (grade >= 60) cout << "Passed." << endl; else { char yourGrade; yourGrade = 'F'; cout << "You received a grade of " << yourGrade << endl; cout << "You must take this course again." << endl; }
&&
(logical AND)true
if both operands are true
, returns false
otherwise.||
(logical OR)true
if either operand is true
, returns false
otherwise.!
(logical NOT, logical negation)true
when its operand is false
, false
otherwise.!
→ &&
→ ||
expression | result |
---|---|
false && false | false |
false && true | false |
true && false | false |
true && true | true |
expression | result |
---|---|
false || false | false |
false || true | true |
true || false | true |
true || true | true |
expression | result |
---|---|
!false | true |
!true | false |
assume: int x = 12, y = 5, z = -4;
expression | result |
---|---|
(x > y) && (y > z) | true |
(x > y) && (z > y) | false |
(x <= z) || (y == z) | false |
(x <= z) || (y != z) | true |
!(x >= z) | false |
int x = 5, y = 6; if ( (x < 0) && (-6 > y-1) ) // (-6 > y-1) is not evaluated ... if ( (x > 0) || (-6 > y-1) ) // (-6 > y-1) is not evaluated ...
false
,true
.if (6) cout << "foo" << endl; else cout << "bar" << endl;
if (0) cout << "foo" << endl; else cout << "bar" << endl;
expr1 ? expr2 : expr3;
expr1
is true, return the value of expr2
, otherwise return the value of expr3
.”y = x<0 ? -1.0*x : x; w = x<0 ? y=10 : z=20;