If else Control Statements in C and C++

<<Previous

Next >>





The if statement can execute the block of statements only if the conditional expression is evaluated to true. In the case of an if-else statement, it can take action when the conditional expression is evaluated as true or false.

if else Syntax in C and C++ Programming

	if(conditional-expression)
                  /* statements can be executed only the conditional_expression is true */
          statement1;
	else
                  /* statements can be executed only the conditional_expression is false*/
         statement2;

You can also execute

	if (conditional_expression){
                /* statements can be executed only the conditional_expression is true */
          compound statements;
	}else {
           /* statements can be executed only the conditional_expression is false */
        compound statements;
	}

The if..else statement checks whether the condition is true or false. If the condition is true, then the if block will be executed. If the condition is false, then the else block will be executed.

if else in C and C++ Programming - Flow Chart


Example C Program for if else statement

#include <stdio.h>
#include <conio.h>

int main()
{
	int A=25;
	int B=40;
	if (A > B){
		printf("The A value is greater than B \n");
		A = A - 5;
	}
	else {
		printf("The A value is not greater than B \n");
		A = A + 5;
	}
	printf("The A value is: %d \n The B value is :%d",A,B);
	return(0)
}

Output

The A value is not greater than B
The A value is:30
The B value is:40

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














If else Control Statements in C and C++

<<Previous

Next >>





The if statement can execute the block of statements only if the conditional expression is evaluated to true. In the case of an if-else statement, it can take action when the conditional expression is evaluated as true or false.

if else Syntax in C and C++ Programming

	if(conditional-expression)
                  /* statements can be executed only the conditional_expression is true */
          statement1;
	else
                  /* statements can be executed only the conditional_expression is false*/
         statement2;

You can also execute

	if (conditional_expression){
                /* statements can be executed only the conditional_expression is true */
          compound statements;
	}else {
           /* statements can be executed only the conditional_expression is false */
        compound statements;
	}

The if..else statement checks whether the condition is true or false. If the condition is true, then the if block will be executed. If the condition is false, then the else block will be executed.

if else in C and C++ Programming - Flow Chart


Example C Program for if else statement

#include <stdio.h>
#include <conio.h>

int main()
{
	int A=25;
	int B=40;
	if (A > B){
		printf("The A value is greater than B \n");
		A = A - 5;
	}
	else {
		printf("The A value is not greater than B \n");
		A = A + 5;
	}
	printf("The A value is: %d \n The B value is :%d",A,B);
	return(0)
}

Output

The A value is not greater than B
The A value is:30
The B value is:40

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy