16 Nov 2011

Testing Code Block 2

 #include<iostream>  
 using namespace std;  
 int main()  
 {  
      cout << "Hello World!";  
      return 0;  
 }  

Testing Code Block

#include<iostream>
using namespace std;

int main()
{
    cout<<"something to test";
    return 0;
}

C++ "Hello World" Tutorial 1


C++ is very simple and basic language in computer programming. Simplest program in C++ is

#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
cout is the most powerful statement in c++ Programming

Output:
           Hello World

14 Nov 2011

C++ For Loop

using C++ For Loop printing of counting 1 to 20
 loop pattern is
for(data Type variableDeclaration; condition; increment )
for(      int                a=1;                   a<5;          a++)

#include<iostream>

using namespace std;

main()
{
for(int a=1; a<=20; a++)
{
cout<<a;
}
return 0;
}


C++ If-Else and Do-While Loop


using C++ If-Else statement and Do-While Loop to find the smaller number
#include<iostream.h>
#include<conio.h>
main()
{
char m;
do
{
clrscr();
int i,j;
cout<<"\n\t\tEnter First Number=";
cin>>i;
cout<<"\n\t\tEnter second number=";
cin>>j;
if(i<j)
{
cout<<"\n\t\tFirst number is Smaller"<<endl;
}
else
{
cout<<"\n\t\tSecond number is Smaller"<<endl;
}
cout<<"\t\t\tTo stop the process press \"s\" and"<<endl;
<<"\t\t\t"<<"To continue press any key"<<endl;
m=getche();
}
while(m!='s' && m!='S');
return 0;
}

C++ If-Else Statement


using c++ if-else statement
#include<iostream>

using namespace std;

main()
{
int a=1;
if(a==1)
{
cout<<"Yes";
}
else
{
cout"no";
}

return 0;

}

C++ variable processing


// Processing of  variables using addition and subrraction

#include <iostream>
using namespace std;

int main ()
{
  // declaring variables:
  int a, b;
  int result;

  // processing:
  a = 10;
  b = 10;
  a = a + 1;
  result = a - b;

  // printing :
  cout << result;

  return 0;
}

C++ Data Types

C++ have man data types that is used top stored variables in computer memory. using different data types computer compiler can easily understand the or identify the character, numbers and etc... some of the important and commonly used data types are

  • int
  • char
  • string
  • long int
  • float
  • bool
  • double 
  • long dounle

C++ General Information

How to write program in c++



My first program in C++




This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is. 



#include <iostream>




Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. 



using namespace std;




All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the namestd. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.



int main ()




This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them. Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.



cout << "HGello World";




This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program. cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).



return 0;




The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.


Comments:

            // Line comments
            /* Multiple Line comment */

13 Nov 2011

C# for loop


The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The for loop is handy for iterating over arrays and for sequential processing. In the following example, the value of int i is written to the console and i is incremented each time through the loop by 1.
Example:
// statements_for.cs
// for loop
using System;
class ForLoopTest
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
}
}

Output: 

5









Remarks:
The for statement executes the enclosed statement or statements repeatedly as follows:
  • First, the initial value of the variable i is evaluated.
  • Then, while the value of i is less than or equal to 5, the condition evaluates to true, the Console.WriteLine statement is executed and i is reevaluated.
  • When i is greater than 5, the condition becomes false and control is transferred outside the loop.
Because the test of conditional expression takes place before the execution of the loop, therefore, a for statement executes zero or more times.
All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:
for (;;)
{
// ...
}

C# switch statement


The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body as the following example:
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}

C# foreach loop example with string array


This example shows how you can use the keyword foreach on a string array in a C# program to loop through the elements in the array. In the foreach-statement, you do not need to specify the loop bounds minimum or maximum, and do not need an 'i' variable as in for-loops. This results in fewer characters to type and code that it is easier to review and verify, with no functionality loss.
Program that uses foreach over array [C#]

using System;

class Program
{
static void Main()
{
// Use a string array to loop over.
string[] ferns =
{
"Psilotopsida",
"Equisetopsida",
"Marattiopsida",
"Polypodiopsida"
};
// Loop with the foreach keyword.
foreach (string value in ferns)
{
Console.WriteLine(value);
}
}
}

Output

Psilotopsida
Equisetopsida
Marattiopsida
Polypodiopsida

C# if else


The if statement selects a statement for execution based on the value of a Boolean expression. In the following example a Boolean flag flagCheck is set to true and then checked in the if statement. The output is: The flag is set to true.
bool flagCheck = true;
if (flagCheck == true)
{
Console.WriteLine("The flag is set to true.");
}
else
{
Console.WriteLine("The flag is set to false.");
}

C# Working With string part 2

Consider this example, which uses Replace, Insert, and ToUpper:

public class TestStringsApp
    public static void Main(string[] args) 
    { 
         string a = "strong"; // Replace all 'o' with 'i' 
         string b = a.Replace('o', 'i'); 
         Console.WriteLine(b);
         string c = b.Insert(3, "engthen"); string d = c.ToUpper(); 
         Console.WriteLine(d); 
    }
}

The output from this application will be:

string STRENGTHENING

C# Working With int Array 1


Int array example

As an introduction to the C# array type, let's look at a simple example program that allocates and initializes an integer array of three elements. Please notice how the elements can be assigned to or read from using the same syntax (values[int]). The array is zero-based; we also demonstrate the foreach loop upon it.
Program that uses an array [C#]

using System;

class Program
{
static void Main()
{
// Use an array.
int[] values = new int[3];
values[0] = 5;
values[1] = values[0] * 2;
values[2] = values[1] * 2;

foreach (int value in values)
{
Console.WriteLine(value);
}
}
}

Output

5
10
20

C# Working With Int Array


This first example shows how you can declare int arrays in four different ways. The first three arrays declared below are declared on single lines, while the fourth array is declared by individual assignment of the elements.
Code:
using System;

class Program
{
static void Main()
{
int[] arr1 = new int[] { 3, 4, 5 }; // Declare int array
int[] arr2 = { 3, 4, 5 }; // Another
var arr3 = new int[] { 3, 4, 5 }; // Another

int[] arr4 = new int[3]; // Declare int array of zeros
arr4[0] = 3;
arr4[1] = 4;
arr4[2] = 5;

if (arr1[0] == arr2[0] &&
arr1[0] == arr3[0] &&
arr1[0] == arr4[0])
{
Console.WriteLine("First elements are the same");
}
}
}

Output

First elements are the same

C# integer


You can declare and initialize a variable of the type int like this example:
        int i = 123;
When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In this example, it is of the type int.

C# Your First Program

C# use of string


The string type represents a string of Unicode characters. string is an alias for System.String in the .NET Framework.
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects. This makes testing for string equality more intuitive.
string a = "hello";
string b = "h";
b += "ello"; // append to b
Console.WriteLine( a == b ); // output: True -- same value
Console.WriteLine( (object)a == b ); // False -- different objects
The + operator concatenates strings:
string a = "good " + "morning";
The [] operator accesses individual characters of a string:
char x = "test"[2];  // x = 's';
String literals are of type string and can be written in two forms, quoted and @-quoted. Quoted string literals are enclosed in double quotation marks ("):
"good morning"  // a string literal
and can contain any character literal, including escape sequences:
string a = "\\\u0066\n";  // backslash, letter f, new line
Note   The escape code \udddd (where dddd is a four-digit number) represents the Unicode character U+dddd. Eight-digit Unicode escape codes are also recognized: \udddd\udddd.
@-quoted string literals start with @ and are enclosed in double quotation marks. For example:
@"good morning"  // a string literal
The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"
To include a double quotation mark in an @-quoted string, double it:
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.
Another use of the @ symbol is to use referenced (/reference) identifiers that happen to be C# keywords.

Example

// keyword_string.cs
using System;
class test
{
public static void Main( String[] args )
{
string a = "\u0068ello ";
string b = "world";
Console.WriteLine( a + b );
Console.WriteLine( a + b == "hello world" );
}
}

Output

hello world
True

C# Hello World! Tutorial 1

1. Open Visual Studio and Click on New Project.

2. Click Visual C# > Console Application and name it Hello World

3. This Window will appear and all the code we'll write in main function.

4. Write the code shown below. as you write cout << "Hello World!"; in C++.
here you'll write Console.WriteLine("Hello World!"); and compile the program by pressing F5.

5. Here's the final output.