Friday, 29 May 2020

C++ Loops - while, for and do while loop


C++ Loops - while, for and do while loop


In any programming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.


How it works
A sequence of statement is executed until a specified condition is true. This sequence of statement to be executed is kept inside the curly braces { } known as loop body. After every execution of loop body, condition is checked, and if it is found to be true the loop body is executed again. When condition check comes out to be false, the loop body will not be executed.


There are 3 type of loops in C++ language
while loop
for loop
do-while loop


while loop
while loop can be address as an entry control loop. It is completed in 3 steps.
·         Variable initialization.(e.g int x=0;)
·         condition(e.g while( x<=10))
·         Variable increment or decrement (x++ or x-- or x=x+2)
Syntax:
 
variable initialization;
while (condition)
{
    statements;
    variable increment or decrement; 
}
 


for loop
for loop is used to execute a set of statement repeatedly until a particular condition is satisfied. we can say it an open ended loop. General format is,
 
for(initialization; condition; increment/decrement)
{
    statement-block;
}
-block;
}
In for loop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.


do...while loop

In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. General format of do-while loop is,

 
do
{
    // a couple of statements
}
while(condition);
 


Jumping out of a loop

Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becocmes true, that is jump out of loop. C language allows jumping from one statement to another within a loop as well as jumping out of the loop.

1) break statement

When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

2) continue statement

It causes the control to go directly to the test-condition and then continue the loop process. On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.

Friday, 22 November 2019

Find Sum of Digits In A Given Number: C

Video Tutorial: Find Sum of Digits In A Given Number: C

Video Tutorial to illustrate, C Program to find sum of digits in the given/user entered integer number.
In this program we assign variable sum = 0 to avoid garbage values in sum before the calculation, which would result in wrong output.
We store the user entered value in num.
1
2
3
4
5
6
 while( num )
  {
    rem = num % 10;
    sum = sum + rem;
    num = num / 10;
  }
Here the loop executes until the value of num is zero.
If user enters 123, we apply the modulus to get the individual values.
Ex:
123 % 10 = 3
12 % 10 = 2
1 % 10 = 1
We get 123, 12 and 1 by dividing the original value by 10.
Ex:
123 user entered value.
123 / 10 = 12
12 / 10 = 1
Now the sum.
sum = sum + rem;
3 = 0 + 3
5 = 3 + 2
6 = 5 + 1
So the sum of digits in the given integer number is 6.
Video Tutorial: Find Sum of Digits In A Given Number: C
Full Free Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    #include < stdio.h >
    #include < conio.h >
 
    void main()
    {
 int num, rem, sum = 0;
 clrscr();
 
 printf("Enter an integer number\n");
 scanf("%d", &num);
 
 while(num)
 {
   rem = num % 10;
   sum = sum + rem;
   num = num / 10;
 }
 
 printf("\nThe sum of digits is %d", sum);
 getch();
    }




Class XI Computer Application Wbchse

 Class XI Computer Application Wbchse