Free Essay

Sdaw

In:

Submitted By thebutcher
Words 365
Pages 2
Mark Joren Crisologo
1BSCE-1

Basic Structures of C++

1.Cin

// i/o example

#include <iostream> using namespace std;

int main ()
{
int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0;
}

cin >> a >> b; | |

2.Cout

cout << "Output sentence"; // prints Output sentence on screen cout << 120; // prints number 120 on screen cout << x; // prints the value of x on screen | |

cout << "Hello"; // prints Hello cout << Hello; // prints the content of variable Hello | |

cout << "First sentence.\n"; cout << "Second sentence.\nThird sentence."; | |

3. If statement

if (x > 0.0) sum += x;

if ( account_balance < 0 ) { cout << "Your account is overdrawn. Balance " << account_balance << endl; if ( overdraft_limit == 0 ) cout << "You have exceeded your limit. << endl; }

if ( account_balance < 0 ) cout << "Your account is overdrawn. Balance " << account_balance << endl; if ( account_balance < 0 && overdraft_limit == 0 ) cout << "You have exceeded your limit. << endl;

4.Nested if

------------------------------------------------- if( boolean_expression 1)
-------------------------------------------------
{
-------------------------------------------------
// Executes when the boolean expression 1 is true
-------------------------------------------------
if(boolean_expression 2)
-------------------------------------------------
{
-------------------------------------------------
// Executes when the boolean expression 2 is true
-------------------------------------------------
}
-------------------------------------------------
}

-------------------------------------------------
#include <iostream>
-------------------------------------------------
using namespace std;
-------------------------------------------------

------------------------------------------------- int main ()
-------------------------------------------------
{
-------------------------------------------------
// local variable declaration:
-------------------------------------------------
int a = 100;
-------------------------------------------------
int b = 200;
-------------------------------------------------

------------------------------------------------- // check the boolean condition
-------------------------------------------------
if( a == 100 )
-------------------------------------------------
{
-------------------------------------------------
// if condition is true then check the following
-------------------------------------------------
if( b == 200 )
-------------------------------------------------
{
-------------------------------------------------
// if condition is true then print the following
-------------------------------------------------
cout << "Value of a is 100 and b is 200" << endl;
-------------------------------------------------
}
-------------------------------------------------
}
-------------------------------------------------
cout << "Exact value of a is : " << a << endl;
-------------------------------------------------
cout << "Exact value of b is : " << b << endl;
-------------------------------------------------

------------------------------------------------- return 0;
-------------------------------------------------
}
When the above code is compiled and executed, it produces the following result:
-------------------------------------------------
Value of a is 100 and b is 200

5. Switch/Case

------------------------------------------------- switch(expression) {
-------------------------------------------------
case constant expression1:
-------------------------------------------------
statements1; break;
-------------------------------------------------
case constant expression2:
-------------------------------------------------
statements1; break;
-------------------------------------------------
..
-------------------------------------------------
..
-------------------------------------------------
default : statementsN;
-------------------------------------------------
}

Similar Documents