About control statement and its classification.

example 3 of while loop
example 3 of while loop
control statement
fig:classification of control statement

if statements

  • If statement is a conditional branching statement. In conditional branching statement a condition is evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:

if (expression)
statement;

 

 

if statement

if else statement:

  • This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. The general form of the if-else statement is

if(expression)
statement1
else
statement2

if else statement
fig: if else statement

Here is a set of basic programming problems based on if else to practice for beginners.

  • Write a C program to input two numbers and find maximum between two.
  • Write a C program to input any number and check whether it is even or odd.
  • Write a C program to input any mark and check whether it pass or fail.

if (x%2==0)
{
printf(“x is an even number”);
}
else
{
if (x>10)
{
printf(“x is an odd number and greater than 10”);
}
else
{
printf(“x is an odd number and less than 10”);
}
}

the other if-else construct, the  if, else if, else. This construct is useful where two or more alternatives are available for selection.

The syntax is

if(condition)
statement 1;
else if (condition)
statement 2;
…………………
…………………
else if(condition)
statement n-1;
else
statemens n ;

if ladder statement flow chart
Fig:    if ladder statement flow chart
if ladder statement program
Fig:if ladder statement program
  • Write a C program to input any year and check whether it is leap year or not.

c program for leap year

Nested if…else statement (if…elseif….else Statement)

  • It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

How nested if…else works?

  • The nested if…else statement has more than one test expression. If the first test expression is true, it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true, it executes the statement/s inside the braces{ } just below it. This process continues. If all the test expression are false, code/s inside else is executed and the control of program jumps below the nested if…else

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*