This example shows how you can use if..else to "open a door" if the user enters the correct code:Example int doorCode = 1337;if (doorCode == 1337) { printf("Correct code.\nThe door is now open.");} else { printf("Wrong code.\nThe door remains closed.");} Try it Yourself...
C/C++ if else statement with ExamplesC/C++ 中的决策制定 有助于编写决策驱动语句和根据特定条件执行一组特定的代码。if 语句单独告诉我们,如果条件为真,...
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...
} else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } 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...
Working of if...else Statement Example 2: if...else statement // Check whether an integer is odd or even#include<stdio.h>intmain(){intnumber;printf("Enter an integer: ");scanf("%d", &number);// True if the remainder is 0if(number%2==0) {printf("%d is an even integer.",numb...
Example: if-else Statement Without Curly BracesConsider the following code too −Open Compiler #include <stdio.h> int main() { int amount = 50; float discount, nett; printf("Amount: %d\n", amount); if (amount<100) printf("Discount not applicable\n"); else printf("Discount applicable...
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 example of if // else statement #include <stdio.h> int main() { int age = 25; if (age < 20) printf("Age less than 20"); else printf("Age greater than 20"); return 0; } 输出 Age greater than 20注:本文由VeryToolz翻译自 C - if...else Statement ,非经特殊声明,文中...
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...
if (number > 0) { printf("The number is positive.\n"); } return 0; } In this example, since number is greater than 0, the condition evaluates to true, and the message “The number is positive.” is printed to the screen. Understanding If-Else Statement The if-else statement extends...