C and C++ Programming Resources

Custom Search

Loops and Decision – if else, for and while

C language programes are executed in a sequence, but we can control the execution of C program by using any control mechanism by which we can compare things and come to a decision. This involves using some operations called Relational Operators, conditional statements called if-else and loops. We have fundamental operators to compare two values.

These relational operators compare two values and return true or false after comparison. We can use the relational operators to compare values of any basic data type, so all we need is to modify the behavior of the program.

“The relational operators compare two values of any basic data type and return true or false after comparison.”

Here are the examples of Conditional Operators

  1. < Less than
  2. > Greater than
  3. == Equal to
  4. <= Less than or equal to
  5. >= Greater than or equal to
  6. != Not equal to

The decision control structure in C can be implemented using

  1. The if statement
  2. The if – else statement
  3. The conditional operators

The if statement by itself will execute a single statement or a group of statements when the condition following if is true

If the condition is false then a group of statements can be executed using else statement

The simple example of an if statement is:

if(varName == 20)
  printf ("Value of the variable is 20");

We can use the block to specify the statements to pre executed if the given condition is true

if(varName == 20){
   printf ("Value of the variable is 20");
   printf ("Print what ever you want!!!");

These are three methods by way of which we can repeat a part of a program in C programming

  1. Using a for statement
  2. Using a while statement
  3. Using a do-while statement

The for statement is started with the keyword for . There are three expressions which appear with in a for statement.

  1. Initializing Expression
  2. Test Expression
  3. Increment Expression

It has the following standard form.

for (int i=0;i<= min; i++)
   ans += i;

The while loop continues untill the specified condition is true. The general form of while loop is

   loop statement

The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the do-while statement. So do-while loop is always executed at least once. Its general form is

   loop statement

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4] [Page - 5]

Tags: ,

There are 183 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.

  • tokyo12345 says:

    please help me also in this question
    write a program to produce a conversion for weight from pounds to kilogram. start from 50lbs to 200lbs…the results of this program is going to be used in the next comelec registration.try to obtain the following results on the screen.

    Pounds Kilos
    100.00 45.45
    101.00 45.91
    102.00 46.36
    103.00 46.82
    104.00 47.27
    105.00 47.73
    106.00 48.18
    107.00 48.64
    108.00 49.09
    109.00 49.55
    110.0 …..

    please help me i need this tomorrow 1 pm email me (lawrence_dejesus21@yahoo.com)

    thank you

  • rere says:

    ——– Original Message ——–

    ——– Original Message ——–
    please give me the right code for this using while loop statement..

    write a program that will calculate and print bills for the city power company. The rates vary depending on whether the use is residential, commercial, or industrial. A code of R means residential, C means commercial, I for industrial use. Any other code should be treated as an error.
    The rates are computed as follows:
    R: $6.00 plus $0.52 per kwh used
    C: $60.00 for the first 10000kwph and $0.045 for each additional kwh
    I: rate varies depending on time of usage:
    Peak hours: $76.00 for the first 1000kwh and $0.065 for each additional kwh
    off-peak hours: $40.00 for the first 1000kwh and $0.028 for each additional kwh.

    Your program should prompt the user to enter an integer account number,the use code (type char). And the neccessary consumption figures in whole number of kilowatt-hours. your program should display the amount due from the user..

Leave a Reply

You must be logged in to post a comment.