C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
t
                                                               y
         Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.


                                                               C
                                       Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS LOOPS


                                   2
Need
• Consider a problem:
  – Write a program that print “Hello” 5 times.
  – One possible statement to solve this problem is :
     • printf(“Hello n Hello n Hello n Hello n Hello);
  – But what if we want to print “Hello” 1000 times?
  – We need a control structure that repeatedly execute
    the following statement.
     • printf(“n Hello);
Decision statements
• To change the control flow of a program we
  use:
  – Branching statements : cause one section of code
    to be executed or not executed, depending on a
    conditional clause.
  – Looping statements : used to repeat a section of
    code a number of times or until some condition
    occurs.
Looping Statements
• Allow the program to repeat a section of code
  any number of times untill some condition
  occures.
Looping statements : 1
• while statement
                     initial_statement;
  – General form:    while(condition){
                           statement;
 while(condition)          iteration_statement;
       statement;    }
• The program will repeatedly execute the
  statement inside the while until the condition
  becomes false (0)
Problems
• Write a program that print “Hello” 5 times.
while(counter <= 5)
  printf(“Hello”);

• This statement reads
   – “while counter is less than or equal to 5 print Hello ”
Multiple Statements
• Put all statements inside the curly { } braces.
               while(condition)
               {
                    statement 1;
                    statement 2;
                    statement 3;
               }
(I ++) or (++ I)
• while (i++ < 10)
  – compare i with 10,
  – increment i.
• while (++i < 10)
  – increment i,
  – compare i with 10.
Problems
• Write a program that print numbers from 1 to 100.
• Write a program that print sum of 10 consecutive
  numbers, enter first number using keyboard.
• Write a program that print all even numbers from
  1 to 100.
• Write a program to find the factorial value of any
  number entered through the keyboard.
break statement
• break statement:
  – Loops can be exited at any point through the use of
    a break statement.
• Write a program that checks whether a given
  number is prime of not.
continue statement
• continue statement:
  – continue starts re-executing the body of the loop
    from the top.
• Write a program that print numbers from 1 to 100
  except the numbers that are divisible by 5 using
  continue statement.
Nested while
while(condition)       while(condition)
      while(condition) {
            statement;       while(condition)
                             {
                                   statement;
while(condition)             }
{                      }
      if(condition)
            statement;
      else
            statement;
}
Mind Bend
     Write a program that print all the numbers
  from 2 to 100 with the indication whether a
  number is prime of not.
Output will be like:
2 : prime
3 : prime
4 : not prime
Looping Statement : 2
• for statement
• General form:
for (initial_statement; condition; iteration_statement)
       body_statement;

           initial_statement;
           while(condition){
                 statement;
                 iteration_statement;
           }
Problems
• Two numbers (x and y) are entered through
  the keyboard. Write a program to find the
  value of xy
• Write a program that print numbers from 1
  to 100 except the numbers that are divisible
  by 5 and 7.
• Write a program to print all characters with
  their ASCII value.

                      16
Looping Statement : 3
• do-while loop
 do{
       statement1;
       statement2
 }while(condition)

• do-while run the statements at least once.
Problems
• Write a program that first take two numbers
  from user and then one operator (+, -, *, /, %)
  and print the result accordingly.
• Now modify the above program that give an
  option to the user to repeat the operation
  again and again with different operator
  choosen by him.
Mind Bend
• Write a program that takes 10 floating-point
  numbers as a input from user and print the
  greatest number.
• Write a program that take floating point
  number from user and print
  – ICE if it is less than 0.
  – WATER if it lies between 0 and 100.
  – STEAM if it is greater then 100.

                           19
To get complete benefit of this tutorial solve all the quiz on
                       www.learnbywatch.com

              For any problem in this tutorial mail me at
                    yogendra@learnbywatch.com
                        with the subject “C”

                     For Other information mail at
                       info@learnbywatch.com


Keep Watching Keep Learning

NEXT IS FUNCTIONS

                                    20

Loops

  • 1.
    C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  • 2.
    t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS LOOPS 2
  • 3.
    Need • Consider aproblem: – Write a program that print “Hello” 5 times. – One possible statement to solve this problem is : • printf(“Hello n Hello n Hello n Hello n Hello); – But what if we want to print “Hello” 1000 times? – We need a control structure that repeatedly execute the following statement. • printf(“n Hello);
  • 4.
    Decision statements • Tochange the control flow of a program we use: – Branching statements : cause one section of code to be executed or not executed, depending on a conditional clause. – Looping statements : used to repeat a section of code a number of times or until some condition occurs.
  • 5.
    Looping Statements • Allowthe program to repeat a section of code any number of times untill some condition occures.
  • 6.
    Looping statements :1 • while statement initial_statement; – General form: while(condition){ statement; while(condition) iteration_statement; statement; } • The program will repeatedly execute the statement inside the while until the condition becomes false (0)
  • 7.
    Problems • Write aprogram that print “Hello” 5 times. while(counter <= 5) printf(“Hello”); • This statement reads – “while counter is less than or equal to 5 print Hello ”
  • 8.
    Multiple Statements • Putall statements inside the curly { } braces. while(condition) { statement 1; statement 2; statement 3; }
  • 9.
    (I ++) or(++ I) • while (i++ < 10) – compare i with 10, – increment i. • while (++i < 10) – increment i, – compare i with 10.
  • 10.
    Problems • Write aprogram that print numbers from 1 to 100. • Write a program that print sum of 10 consecutive numbers, enter first number using keyboard. • Write a program that print all even numbers from 1 to 100. • Write a program to find the factorial value of any number entered through the keyboard.
  • 11.
    break statement • breakstatement: – Loops can be exited at any point through the use of a break statement. • Write a program that checks whether a given number is prime of not.
  • 12.
    continue statement • continuestatement: – continue starts re-executing the body of the loop from the top. • Write a program that print numbers from 1 to 100 except the numbers that are divisible by 5 using continue statement.
  • 13.
    Nested while while(condition) while(condition) while(condition) { statement; while(condition) { statement; while(condition) } { } if(condition) statement; else statement; }
  • 14.
    Mind Bend Write a program that print all the numbers from 2 to 100 with the indication whether a number is prime of not. Output will be like: 2 : prime 3 : prime 4 : not prime
  • 15.
    Looping Statement :2 • for statement • General form: for (initial_statement; condition; iteration_statement) body_statement; initial_statement; while(condition){ statement; iteration_statement; }
  • 16.
    Problems • Two numbers(x and y) are entered through the keyboard. Write a program to find the value of xy • Write a program that print numbers from 1 to 100 except the numbers that are divisible by 5 and 7. • Write a program to print all characters with their ASCII value. 16
  • 17.
    Looping Statement :3 • do-while loop do{ statement1; statement2 }while(condition) • do-while run the statements at least once.
  • 18.
    Problems • Write aprogram that first take two numbers from user and then one operator (+, -, *, /, %) and print the result accordingly. • Now modify the above program that give an option to the user to repeat the operation again and again with different operator choosen by him.
  • 19.
    Mind Bend • Writea program that takes 10 floating-point numbers as a input from user and print the greatest number. • Write a program that take floating point number from user and print – ICE if it is less than 0. – WATER if it lies between 0 and 100. – STEAM if it is greater then 100. 19
  • 20.
    To get completebenefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS FUNCTIONS 20