| A | B | A && B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
| A | B | A || B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
| A | ! A |
|---|---|
| True | False |
| False | True |
| A | B | C | A || B && C |
|---|---|---|---|
| True | True | True | |
| True | True | False | |
| True | False | True | |
| True | False | False | |
| False | True | True | |
| False | True | False | |
| False | False | True | |
| False | False | False |
| A | B | ! (A && B) |
|---|---|---|
| True | True | |
| True | False | |
| False | True | |
| False | False |
#include <iostream>
using namespace std;
int main()
{
int a=1,b=2;
a < b && cout << "Test One " << endl;
a > b && cout << "Test Two " << endl;
a < b || cout << "Test Three " << endl;
a > b || cout << "Test Four " << endl;
system("pause");
return 0;
}
Yield
Test One Test Four Press any key to continue . . .
int a=5,b;
b= a>5 ? 4 : 3;
if (expression) statement;
If the expression evaluates to nonzero the statement is executed. If you wish
multiple statements executed, then they must be enclosed in { }.
if (x==3) cout << y << endl;
if(x>0) {
x+=y;
y<<=3;
z=0;
}
#include <iostream.h>
main()
{
int year=1753;
while( year<2400) {
if(year%4==0 && (year%100 || !(year%400)))
cout << year << " is a leap year" << endl;
year++;
}
}
if(x==3) cout << y << endl; else x+=y;
if (mon==1) days=31; else if (mon==2) days=28; else if (mon==3) days=31; else if (mon==4) days=30; else if (mon==5) days=31; else if (mon==6) days=30; else if (mon==7) days=31; else if (mon==8) days=31; else if (mon==9) days=30; else if (mon==10) days=31; else if (mon==11) days=30; else if (mon==12) days=31;
if (mon==1) days=31;
else if (mon==2) days=28;
else if (mon==3) days=31;
else if (mon==4) days=30;
else if (mon==5) days=31;
else if (mon==6) days=30;
else if (mon==7) days=31;
else if (mon==8) days=31;
else if (mon==9) days=30;
else if (mon==10) days=31;
else if (mon==11) days=30;
else if (mon==12) days=31;
int x=1,y=1,z,a;
if(x==0)
if (y==0) z=1;
else {
z=2;
a=3;
}
z keeps a value of zero since it was not initialized. Else statements
line up with the closest unattached if statement. This problem is know as the
dangling else. What the code sees is:
int x=1,y=1,z,a;
if(x==0)
if (y==0) z=1;
else {
z=2;
a=3;
}
To get the prior meaning the code would need to be written as:
int x=1,y=1,z,a;
if(x==0) {
if (y==0) z=1;
}
else {
z=2;
a=3;
}
or as
int x=1,y=1,z,a;
if(x==0)
if (y==0) z=1;
else ;
else {
z=2;
a=3;
}
switch (expression)
{
case constant1 : statement1;
case constant2 : statement2;
.
.
case constant : statement;
}
Where constant1, constant2.. constantn are possible values of expression.
They must be integer type (char, short, int, long). If the expression
matches the constant control is transferred to that statement that follows
the constant.
switch (mon) {
case 1 : days=31; break;
case 2 : days=28; break;
case 3 : days=31; break;
case 4 : days=30; break;
case 5 : days=31; break;
case 6 : days=30; break;
case 7 : days=31; break;
case 8 : days=31; break;
case 9 : days=30; break;
case 10 : days=31; break;
case 11 : days=30; break;
case 12 : days=31;
}
Since the switch is implemented as a computed goto instead of the Pascal
case statement, the break statement is necessary to bypass code.
switch (mon) {
case 2 : days=28; break;
case 4 :
case 6 :
case 9 :
case 11 : days=30; break;
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : days=31;
}
switch (mon) {
case 2 : if(year%4) days=28;
else days=29;
break;
case 4 :
case 6 :
case 9 :
case 11 : days=30; break;
default : days=31;
}
#include <iostream>
using namespace std;
void main()
{
int a,b,c,h;
cout << "enter three numbers. ";
cin >> a >> b >>c;
cout << "the numbers are" << a << " " << b << " " << c << endl;
if (a>0 && b>0 && c>0) { // Test for valid input
if (a+b>c && a+c>b && b+c>a) { // Test for triangle
if (a==b && b==c) { // Equilateral?
cout << " Equilateral triangle << endl;
}
else if (a==b || b==c || a==c) { // Isosceles?
cout << " Isosceles triangle" << endl;
}
else { // Scalene
cout << " Scalene triangle" << endl;
h = (b>a?(b>c?b:c):(a>c?a:c)); // Find largest side
if (h*h == a*a+b*b+c*c-h*h) // Right ?
cout << "Also a right triangle" << endl;
}
}
else cout << " Not a triangle" << endl;
}
else cout << " Invalid numbers" << endl;
}
#include <iostream>
using namespace std;
void main()
{
int m,y,d,c,f;
cout << "please enter the month day and year" << endl;
cout << "like 6 10 1998 :";
cin >> m >> d >> y;
m-=2; // Subtract two from month?
if (m<=0) { // Prior year?
m+=12; // adjust month
y--; // adjust year
}
c=y/100; // Extract century
y%=100; // extract year in century
f = ((int)(2.6*m-0.2) + d + y + (y/4) + (c/4) - 2*c)%7; // Zellers
f+= f<0 ? 7 : 0; // fix for negative number.
switch(f) {
case 0 : cout << "Sunday" << endl; break;
case 1 : cout << "Monday" << endl; break;
case 2 : cout << "Tuesday" << endl; break;
case 3 : cout << "Wednesday" << endl; break;
case 4 : cout << "Thursday" << endl; break;
case 5 : cout << "Friday" << endl; break;
case 6 : cout << "Saturday" << endl;
}
}