Example 1: if statement // Program to display a number if it is negative#include<stdio.h>intmain(){intnumber;printf("Enter an integer: ");scanf("%d", &number);// true if number is less than 0if(number <0) {printf("You entered %d.\n", number); }printf("The if statement is ...
if(condition){//Block of C statements here//These statements will only execute if the condition is true} Flow Diagram of if statement Example of if statement #include<stdio.h>intmain(){intx=20;inty=22;if(x<y){printf("Variable x is less than y");}return0;} Output: Variablexisless ...
+---+ | Condition | +---|---+ | +---v---+ | If true | | statement | +---|---+ | +---v---+ | Else | | statement | +---+ Example: #include <stdio.h> int main() { int num = 10; if (num > 5) { printf("The number is greater than 5.\n"); } else {...
The following are examples of theifstatement: C if( i >0) y = x / i;else{ x = i; y = f( x ); } In this example, the statementy = x/i;is executed ifiis greater than 0. Ifiis less than or equal to 0,iis assigned tox, andf( x )is assigned toy. The statement forming...
Example of else..if statement Lets take the same example that we have seen above while discussing nested if..else. We will rewrite the same program using else..if statements. #include<stdio.h>intmain(){intvar1,var2;printf("Input the value of var1:");scanf("%d",&var1);printf("Inpu...
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...
if语句后面可以跟一个可选的else语句,该语句在布尔表达式为false时执行。 语法(Syntax) C编程语言中if...else语句的语法是 - if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false *...
If the condition is false (zero), the block of code is skipped. Simple Example Illustrating If Statement Consider a program that checks if a number is positive: #include <stdio.h> int main() { int number = 10; if (number > 0) { printf("The number is positive.\n"); } return 0...
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 number is not even, in the el...
else if(condition3 ) { /* statement3 */ } else /* default statement */ Flow diagram For example: How if-else Statement Works in C? Basically, if the condition returns to be true then the statements mentioned inside the body of the logical ‘if’ are met or executed and the statements...