Skip to main content

Posts

Showing posts with the label Cpp Flow Control

C++ Decision Making

Flow Control C++ Decision Making C++ conditional statements allows you to make decision, based upon the result of a condition. These statements are called as  Decision Making Statements  or  Conditional Statements . So far, we have seen that all set of statements in a C++ program gets executed sequentially in the order in which they are written and appear. This occurs when there is no jump based statements or repetitions of certain calculations. But a number of situations may arise where we may have to change the order of execution of statements depending on some specific conditions. This involves a kind of decision making from a set of calculations. It is to be noted that C++ assumes any non-zero or non-null value as true and if zero or null, treated as false. This type of structure requires that the programmers indicate several conditions for evaluation within a program. The statement(s) will get executed only if the condition becomes true and optionally, alternati...

C++ if-else Statements

C++ if-else Statements If else statements in C++ is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if expression is evaluated to true, otherwise executes else statement code block. The basic format of if else statement is: Syntax: if ( test_expression ) { //execute your code } else { //execute your code } Figure – Flowchart of if else Statement: Example of a C++ Program to Demonstrate if else Statement Example: #include <iostream> using namespace std ; int main () { int a = 15 , b = 20 ; if ( b > a ) {    cout << "b is greater" << endl ; } else {    cout << "a is greater" << endl ; }    system ( "PAUSE" ); } Program Output: Example: #include <iostream> using namespace std ; int main () { char name ; int password ; cout << "Enter the name: " ; cin >>...

C++ if Statements

C++ if Statements If statements in C++ is used to control the program flow based on some condition, it’s used to execute some statement code block if expression is evaluated to true, otherwise it will get skipped. This is an simplest way to modify the control flow of the program. The if statement in C++ can be used in various forms depending on the situation and complexity. There are four different types of if statement in C++. These are: Simple if Statement if-else Statement Nested if-else Statement else-if Ladder The basic format of if statement is: Syntax: if ( test_expression ) { statement 1 ; statement 2 ; ... } ‘Statement n’ can be a statement or a set of statements and if the test expression is evaluated to  true , the statement block will get executed or it will get skipped. Figure – Flowchart of if Statement: Example of a C++ Program to Demonstrate if Statement Example: #include <iostream> using namespace std ; int main () { int a = 15 , b = 20 ;...

C++ else-if Statements

C++ else-if Statements else if statements in C++ is like another if condition, it’s used in program when if statement having multiple decisions. The basic format of else if statement is:  Syntax: if ( test_expression ) { //execute your code } else if ( test_expression n ) { //execute your code } else { //execute your code } Example of a C++ Program to Demonstrate else if Statement Example: #include <iostream> using namespace std ; int main () { int a = 15 , b = 20 ; if ( b > a ) {         cout << "b is greater" << endl ; } else if ( a > b ){         cout << "a is greater" << endl ; } else {         cout << "\n Both are equal" << endl ; }     system ( "PAUSE" ); } Program Output:

C++ switch Statements

C++ switch Statements C++ switch statement is used when you have multiple possibilities for the if statement. The basic format of switch statement is:  Syntax: switch ( variable ) { case 1 : //execute your code break ; case n : //execute your code break ; default : //execute your code break ; } After the end of each block it is necessary to insert a  break statement  because if the programmers do not use the break statement, all consecutive blocks of codes will get executed from each and every case onwards after matching the case block. Example of a C++ Program to Demonstrate Switch Statement Example: #include <iostream> using namespace std ; main () { int a ; cout << "Please enter a no between 1 and 5: " << endl ; cin >> a ; switch ( a ) { case 1 : cout << "You chose One" << endl ; break ; case 2 : cout << "You chose Two" << endl ; ...

C++ Loops

C++ Loops Sometimes it is necessary in the program to execute the statement several times, and C++ loops execute a block of commands a specified number of times, until a condition is met. In this chapter you will learn about all the looping statements of C++ along with their use. What is Loops in C++? There may arise some situations during programming where programmers need to execute block of code several times (with slight variations sometimes). In general, statements get executed sequentially with a C++ program, one statement followed by another. C++ provides statements for several control structures along with iteration / repetition capability that allow programmers to execute a statement or group of statements multiple times. C++ supports following types of loops: while loops do while loops for loops All are slightly different and provides loops for different situations. Figure – Flowchart of Looping: C++ Loop Control Statements Loop control statements is used to change normal...

C++ while loops

C++ while loops . C++ while loops statement allows to repeatedly run the same block of code, until a condition is met. while loop  is most basic loop in C++. while loop has one control condition, and executes as long the condition is true.  The condition of the loop is tested before the body of the loop is executed, hence it is called an  entry-controlled  loop. The basic format of while loop statement is: Syntax: While ( condition ) { statement ( s ); Incrementation ; } Figure – Flowchart of while loop: Example of a C++ Program to Demonstrate while loop Example: #include <iostream> using namespace std ; int main () { /* local variable Initialization */ int n = 1 , times = 5 ; /* while loops execution */ while ( n <= times ) { cout << "C++ while loops: " << n << endl ; n ++; } return 0 ; } Program Output:

For Programs Click Here