C++ Programming Code Examples
C++ > If Else and Switch Case Code Examples
The if-else Statement
/* The if-else Statement
The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements ifthe expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated as */
if (expression)
{
block of statement;
}
else
statement;
/* The group of statements after the if upto and not including the else is called an if block. Similarly,the statements after the else form the else block.
Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right.
Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces.
As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above "Multiple Statements within if" must be used. */
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.
The Time complexity to generate this code is 'O(v*e)'. This algorithm takes the input of the number of vertexes for the tree. Then it takes the input if "Vertex Pairs" which have an edge