这里是 C/C++ else 语句。当条件为假时,我们可以使用 else 语句和 if 语句来执行一段代码。语法:if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }if-else 语句的工作...
2) if else condition This type of if condition is used, when you have one condition and two body of code (two set of statements) to execute based on a single condition. Syntax if( test-condition) { True-block; } else { False-block; } Here, two blocks,True-blockandFalse-block. ...
Theif...elsestatement is used to execute a block of code among two alternatives. However, if we need to make a choice between more than two alternatives, we use theif...else if...elsestatement. Syntax if(condition1) {// code block 1}elseif(condition2){// code block 2}else{// co...
Example for C If-Else Statement In the following example, we have an if-else with condition to check whether the number is even. If the number is even, then we shall print a message to the console that the number is even. This is if block, and have just a single statement. If the...
The if-else in C++ is the most commonly used decision-making statement. It is used to decide which block of statements will be executed based on the result of the conditional statement. Here also, the condition has only two boolean values, i.e., either true or false....
Example: #include <stdio.h> int main() { int num = 10; if (num > 5) { printf("The number is greater than 5.\n"); } else { printf("The number is not greater than 5.\n"); } return 0; } In this example, the condition num > 5 is evaluated. Since the value of num is...
If the condition is true, print some text:Example if (20 > 18) { printf("20 is greater than 18");} Try it Yourself » We can also test variables:Example int x = 20;int y = 18;if (x > y) { printf("x is greater than y");} Try it Yourself » Example explained...
In the example we have a simple condition; if the num variable is positive, the message "The number is positive" is printed to the console. Otherwise; nothing is printed. $ ./if_stm The number is positive if_else.c #include <stdio.h> int main() { int num = -4; if (num > 4...
} else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ } 例子(Example) #include <stdio.h> int main () { /* local variable definition */ ...
Example of if else statement In this program user is asked to enter the age and based on the input, the if..else statement checks whether the entered age is greater than or equal to 18. If this condition meet then display message “You are eligible for voting”, however if the condition...