C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Saturday, 15 April 2017

C++ if...else

                                         C++ if...else 



The  if  else executes the codes inside the body of if the test expression is true and skip the expression is true and skips the codes inside the body of else.

if the test expression is false, it executes the codes inside the body of else statement and skip the codes inside the body of it.

How if...else statement works ? 



Flowchart of if...else
















Example :  if...else Statements













Output











This line is always printed.

C++ if Statement

                                            C++ if  Statement


The if statement evaluates the test expression inside parenthesis.

If test expression is evaluated to true, statements inside the body of if is executed.

If test expression is evaluated to false, statements inside the body of if is skipped.













How if statement works ?



Flowchart of if Statement






















Above figure describes the working of an if statement.

Example : C++ if Stament







C++ for Loop

                                            C++ for Loop


Loops are used in programming to repeat a specific block of code and specific block until some end condition is met. There are three type of loops in C++ programming:

1. for loop
2. while loop
3. do...while loop


for Loop Syntax :









where only testExpression is mandatory.

How for loop works ?  :

1. The initialization statement is executed only unes at the beginning.

2. Then, the test expression is evaluated.

3. If the test expression is false, for loop is executed and update expression is updated.

4. Again, the test expression is evaluated and this process repeats until the test expression is false.


Flowchart of for Loop in C++























Example: for Loop :







































Output:










In the program, user is asked to enter a positive integer which is stored in variable n (suppose user enter 5). Here is the working of for loop :

1.  Initialy i is equal to 1, test expression is true, factorial becomes 1.
2.  i is Updated to 2, test expression is true, factorial becomes 2.
3.  i is Updated to 3, test expression is true, factorial becomes 6.
4.  i is Updated to 4, test expression is true, factorial becomes 24.
5.  i is Updated to 5, test expression is true, factorial becomes 120.
6.  i is Updated to 6, test expression is false, for loop is terminated.

In the above program, variable i is not used outside of the for loop.in such a cases, it is better to declare the variable in for loop (at initialization statement).


C++ While Loop

                                       C++ While Loop


Loops are used in programming to repeat a specific block of code. 

There are 3 type of Loops  in C++ Programming :

  • for Loop
  • while Loop
  • do....while Loop

C++ while Loop


The syntex of a while loop is :











Where, testExpression is checked on each enty of the while loop.

How while loop works ? 

  • The while loop evaluates the test expression.
  •  If the test expression is true, codes inside the body of while loop is evaluated.
  • Then, the test expression is evaluated again. this process goes on the test expression is false.
  • When the test expression is false, while loop is terminated.


Flowchart of while Loop






















Example  : C++ while Loop :




































Output :









In this program , user is asked to enter a  positive integer which is stored in variable number.
Let's suppose, User entered 4.

Then , the while loop states executing the code. Here's ow while loop works:

1. Initially, i = 1 , test expression i <= number is true and factorial becomes 1.
2. Variable i is updated to 2, test expression is true, factorial becomes 2.
3. Variable i is updated to 3, test expression is true, factorial becomes 6.
4. Variable i is updated to 4, test expression is true, factorial becomes 24.
5. Variable i is updated to 5, test expression is false and while loop is terminated.






C++ do....while Loop

                                      do....while Loop


The do..while loop is a variant of  the while loop with one inportant difference. The body of do ...while loop is executed once before the test expression is checked.

The syntax of do... while loop is:

 









How do...while loop works?

  • The codes inside the body of loop is executed at least once. Then, only the test expression is checked.
  • If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false.
  • When the test expression isfalse, do...whileloop is terminated.


Flowchart of do...while Loop:
























Example : C++do....while Loop  




































Output:






Popular Posts

Recent Posts

Categories

Unordered List

Text Widget