CONDITIONAL STATEMENTS AND LOOPS PART - I



CONDITIONAL STATEMENTS AND LOOPS:
INTRODUCTION:
         Power of programing languages lies in their ability to instruct a computer to perform the same basic operation again and again or repeatedly till a specific condition is satisfied.
         In programming language C, like other, this power is accomplished with the help of different kinds of control statements.





Control statements are of the following two types:
  • Conditional branching 
  • Looping
         Conditional branching, a program is to decide wether or not to execute the next statement(s) based on the resultant value of the current expression. For example, go to IInd year, if you pass Ist year exams, else repeat Ist  year.
         Looping, the program perform a set of steps or operations repeatedly till a given condition is obtained. Looping for example, add 1 to number x till current value of x becomes 100 is also called iteration.





The if Statement:
         The if statement is a control statement that tests a particular condition. Whenever, the evaluated condition comes out to be true, then that action or the set of action are carried out. Otherwise, the given set of action(s) are ignored. The syntax for this statement is:
If(expression)
Statement(s);
       The exprssion must be enclosed within parentheses( ). If expression evaluates to a non zero value, the condition is considered to be true and the next statement(s) following the closing parentheses is/are executed.
For Example:
#include<stdio.h>
main()    {
int a, b, c, max;
printf("Enter the value of a,b & c:");
scanf("%d %d %d",&a, &b, &c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("The biggest no. is: %d",max);   }
Output:






Multiple if Statement:
         It may so happen that we may want more than one statement to be executed when the condition following the statement if is satisfied. In this case, all such statements must be placed within a pair of braces ({ }). The individual statements enclosed by braces are executed in the order in which they are placed.
For Example:
#include<stdio.h>
main()  {
int a, b, c;
printf("Enter the value of a, b & c:");
scanf("%d %d %d",&a, &b, &c);
if (a > b)          {
if (b>c)            {
printf("The biggest no. is: %d",a);    }}
if (b>a)            {
if (b>c)            {
printf("The biggest no. is: %d",b);       }}
if (c>a) {
if (c>b)            {
printf("The biggest no. is: %d",c);    }}}
Output:






Nested if-else Statement:
       A single if statement allows the program to select one out of the two given statements. However, you may need to specify several conditions and take one out of them. This type of program flow requires a construct called a nested if statement.
The if-else-if ladder
The conditions and their associated statements can be arranged in a nested if construct that takes the following form:
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
---------------------
--------------------
else
statement;
     The conditions are evaluated from top to bottom, and whenever an evaluated condition is true, the corresponding statement(s) are executed and the rest of the statement in this manner is called if-else-is ladder.
For Example:
#include<stdio.h>
main()  {
int  a, b, c;
printf("Enter the value of a,b & c:");
scanf("%d %d %d",&a, &b, &c);
if(a>b)  {
if(a>c)   {
printf("The biggest no. is: %d",a);       }
else       {
printf("The biggest no. is: %d",c);       }}
else       {
if(b>c)  {
printf("The boggest no. is: %d",b);      }
else       {
printf("The biggest no. is: %d",c);        }}}
Output:





CONDITIONAL STATEMENTS AND LOOPS PART - I CONDITIONAL STATEMENTS AND LOOPS PART - I Reviewed by vijay pratap singh on September 04, 2017 Rating: 5

No comments:

Powered by Blogger.