if Statement

if statement is a basic control flow structure of C programming languageif statement is used when a unit of code need to be executed by a condition true or false. If the condition is true, the code in if block will execute otherwise it does nothing. The ifstatement syntax is simple as follows:
1.if(condition){
2./* unit of code to be executed */
3.}
C programming language forces condition must be a boolean expression or value. ifstatement has it own scope which defines the range over which condition affects, for example:
01./* all code in bracket is affects by if condition*/
02.if(x == y){
03.x++;
04.y--;
05.}
06./* only expression x++ is affected by if condition*/
07.if(x == y)
08.x++;
09.y--;
In case you want to use both condition of if statement, you can use if-else statement. If the condition of if statement is false the code block in else will be executed. Here is the syntax of if-else statement:
1.if(condition){
2./* code block of if statement */
3.}else{
4./* code block of else statement */
5.}
If we want to use several conditions we can use if-else-if statement. Here are common syntax of if-else-if statement:
01.if(condition-1){
02./* code block if condition-1 is true */
03.}else if (condition-2){
04./* code block if condition-2 is true */
05.}else if (condition-3){
06./* code block if condition-3 is true */
07.}else{
08./* code block all conditions above are false */
09.}
1.if(x == y){
2.printf("x is equal y");
3.}
4.else if (x > y){
5.printf("x is greater than y");
6.}else if (x < y){
7.printf("x is less than y");
8.}

source:cprogramlanguage.net

No comments:

Post a Comment