VC++ tutorial

Visual C++ Examples

How To Add Menu In VC++

Statements and Expression In VC++
Previous Home Next

An Expression is a statement it has a value of the variable. a c program control the flow of program execution. in other programming languages different kind of statements are available to perform loops, to select other statement to be executed and to the transfer control.

statements are used to make one time decisions in c++ programming, that is execute the code and ignore some codes its depend upon the test condition. decision making is important part of every programming language.

Types of statement:

Selection Statment:

If statement: It checks whether the test condition is true or not. If the test condition is true it executes the code inside the body of if statement. but if condition is false left the code inside the body of if statement.

Example:

#include<iostream>
	using namespace std;

	int main()
	{
		int X;
		cin >> X;
		if ( X == 20 )
		{
			cout << "is equal" << '\n';
			cout << "closing program" << '\n';
		}
		else
		{
			cout << "not equal" << '\n';
			cout << "closing program" << '\n';
		}
		return 0;
	}

If-else satement: The if...else executes body of if when the test expression is true and executes the body of else if test expression is false. in if statement is true it executes the code inside the body of if statement. and if test condition is false it execute the inside the body of else.

Example:

#include<iostream>
using namespace std;
int main()
{

     int a;
	cout<<"Enter the Number :";
     cin>>a;
	 if(a > 10)
     {

        cout<<a<<" Is Greater than 10";
     }
     else
     {
      
       cout<<a<<" Is Less than/Equal 10";
     }
 return 0;
 }

Nested If-Else Statement: Nested if-else statements are test for multiple cases by If-else selection statements inside other if-else selection statements. Sometimes a choice must be made between more than two possibilities.

Syntax:

     if ( condition1 )
    statement1 ;
    else if ( condition2 )
    statement2 ;
    . . .
    else if ( condition-n )
    statement-n ;
    else
    statement-e ; 

Example:

#include <iostream>
using namespace std;
int main() 
{
    char grade;

    cout << "Please enter a letter grade (A, B, C, D, or F): " << endl;
    cin >> grade;

    if (grade == 'A')
       cout << "Execlent. " << endl;
       else if (grade == 'B')
            cout << "Very Good. " << endl;
            else if (grade == 'C')
                 cout << "Good. " << endl;
                 else if (grade == 'D' || grade == 'F')
                 {
                    cout << "You need to a lot of improvement. " << endl;
		    cout << "Work on your Skills." << endl;
		 }
		 else
                    cout << grade << " is not a legal grade." << endl;

    cout << endl;
    return 0;
}
Iteration Statements (loops)

C++ provides flow control structures to implement repetition. These control structures, generally known as loops, allow us to write blocks of code that are executed a given number of times, or simply executed until certain condition is met. Loop can be categorized in to two basic type: for loop and while loop.

For loop:for loop is used to execute a set of statement repeatedly until a particular condition is satisfied. The “for loop” loops from one number to another number and increases by a specified value each time.

Syntax:

for(<initial value>;<condition>;<increment>)

Example:

#include<iostream>
	using namespace std;
    int main()
	{
		int i;
		for (i = 0; i < 5; i++)
		{
			cout << "Hello world" << "\n";
			}
		return 0;
	}

while loop: The while-loop simply repeats statements while expression is true. if after execution of statement, expression is no longer true the loop ends, and the program continues right after the loop.

while loop are related to the idea of a loop that is executed until certain condition happens.

Syntax:

while (condition)
{
    statement(s);
} 

Example

#include<iostream>
	using namespace std;

	int main()
	{
		int feet, meter;

		cin >> meter;

		feet = 0;
		while ( feet < meter)
		{
			feet++;
			cout << feet << '\n';
		}
		return 0;
	}

Do-while loop: do while loop similar to the while loop, except that the test condition occurs at the end of the loop. a do while loop executes one or more times depending on the value of the termination expression.

Example:

#include <iostream>
using namespace std;

int main() {
    int a,b, sum = 0;
    
    do {
        cout<<"Enter a number: ";
        cin>>a,b;
        sum = a+b;
    }
    while(a+b != 0);

    cout<<"Total sum = "<<sum;
    
    return 0;
}
Control Statement

Break statement: you want to exit a loop so you can use a break statement at any time. Break statement is very important if you want to stop a running loop because a condition has been met other than the loop end condition. A break statement is used in the conditional switch statement and with Do, for and while loop statements.

Example:

#include <iostream>
	using namespace std;

	int main()
	{
		int i;

		i = 0;
		while ( i < 20 )
		{
			i++;
			cout << "World\n";
			if ( i == 10)
				break;
		}
		return 0;
	}

Continue statement: In this statement control directly go to the test condition and then continue to the loop process cursor leave the current cycle of loop, and starts with the next cycle.

Example:

#include <iostream>
	using namespace std;

	int main()
	{
		int i;

		i = 0;
		while ( i < 30 )
		{
			i++;
			continue;
			cout << "world\n";
			if ( i == 30)
				break;
		}
		return 0;
	}
Expressions:

An expression is "a sequence of operators and operands that specifies a computation. Expression evaluation may produce a result and may generate side-effects

Primary Expression: The operands of any operator may be other expressions or primary expressions Postfix Expression

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: