learncpponline

do-while statement in C++

The do-while loop is another repetitive loop used in C++ programs. When a loop is constructed using the while statement, the test for continuation of the loop is carried out at the beginning of each pass. Sometimes, however, it is desirable to have a loop with the test for continuation at the end of each pass. This can be accomplished by means of do-while statement. The general form of the do-while statement is do{ statement 1; statement 2; …. …. } while(expression); The statement will be executed repeatedly, as long as the value of expression is true (i.e., is non-zero). Notice that statement will always be executed at least once, since the test for repetition does not occur until the end of the first pass through the loop. The statement can be either simple or compound. It must include some feature that eventually alters the value of expression so that...

while statement in C++

The second type of loop, the while loop, is used when we are not certain that the loop will be executed. After checking whether the initial condition is true or false and finding it to be true, then only while loop will enter into the loop operations. The general form of the while loop for a single statement is: while(expression) statement; The general form of the while loop for a block of statements is: while(expression) { statement 1; statement 2; …. …. } The expression can be any valid C++ language expression including the value of a variable, an unary or a binary expression, or the value returned by a function. The statement can be single or compount statement. The statement will be executed repeatedly, as long as the expression is true (i.e., as long as expression has a non zero value). statement must include some features that eventually alters...

for statement in C++

The for statement or for loop is useful while executing a statement multiple number of times. The for loop is the most commonly used statement in C++. This loop consists of three expression: The first expression is used to initialize the index value The second expression is used to check whether or not the loop is to be continued again The third expression is used to change the index value for further iteration The general form of for statement is: for(expression 1; expression 2; expression 3) statement; In other words, for(initial condition; test condition; incrementer or decrementer) { statement1; statement2; ….. ….. } Where, expression 1 is the initialization of the expression or the condition called an index expression 2 is the condition checked. As long as the given expression is true the loop statement will be repeated expression 3 is the incrementer or decrementer to change the index value...

Switch Statement in C++

The switch statement is a special multiway decision maker that tests whether an expression matches one of the number of constant values accordingly. switch statement allows the user to choose a statement or a group of statements among several alternatives. The switch statement is useful when a variable is compared with different constants, and in case it is equal to a constant, a set of statements would be executed. The general form of the switch statement is as follows: switch (expression) { case constant 1: statement; case constant 2: statement; ………. ………. case constant n: statement; default: statement; } In this general form: 1) The expression, whose value is being compared, may be any valid expression but not a floating point expression 2) The The value that follows the keyword case may only be constants. They cannot be expressions. They may be an integer or characters, but not the floating...

If-else Statement in C++

if-else statement if statement is most commonly used with the following format: if(expression) { statement 1 } else { statement 2 } where else part is optional. In this case, either of the two statements are executed depending upon the value of the expression. If the expression has a non-zero value (i.e., if the expression is true), then statement1 will be executed. Otherwise (i.e., if expression is false), statement2 will be executed. Example: Given here is a program which reads a number and then prints whether the given number is even or odd. Note: If we divide a number by 2 and if the remainder is 0 then the number is EVEN else the number is ODD #include<iostream.h> void main() { int n; cout << “Enter a number :”; cin >> n; if(n%2 == 0) cout << “The number is EVEN”; else cout << “The number is ODD”; }

If Statement in C++

if statement: The if statement is used to express the conditional expressions. If the given condition is true then it will execute the statements otherwise it will execute the optional statements. The basic simple structure of the if statement is shown below: if (expression) { //set of statements } The expression must be placed in round brackets as shown above. In this form, the set of statements would be executed only if the expression has a non-zero value (i.e., if expression is true). If the expression has a value zero (i.e., if expression is false), then the set of statements would be ignored by C++ compiler. The set of statements are skipped and the execution continues with the next statements. Example: Given below is a program which reads two numbers and then prints greater one using if statement. #include <iostream.h> void main() { int a, b; cout << “Enter Two...