do-while Statement

Saturday, June 16, 2012
We have seen that there may be certain situations when the body of while loop does not execute even a single time. This occurs when the condition in while is false. In while loop, the condition is tested first and the statements in the body are executed only when this condition is true. If the condition...

while exercises

Exercise 1) Calculate the sum of odd integers for a given upper limit. Also draw flow chart of the program. 2) Calculate the sum of even and odd integers separately for a given upper limit using only one loop structure. Also draw flow chart of the program. tips: Always use the self explanatory...

While Sample Program

Problem statement: Calculate the factorial of a given number. Solution: The factorial of a number N is defined as: By looking at the problem, we can see that there is a repetition of multiplication of numbers. A loop is needed to write a program to solve a factorial of a number. Let's think in terms...

While Flow Chart

The basic structure of while loop in structured flow chart is: At first, we will draw a rectangle and write while in it. Then draw a line to its right and use the decision symbol i.e. diamond diagram. Write the loop condition in the diamond and draw a line down to diamond which represents the flow...

Properties of while loop

In the above example, if the user enters 0, as the value for upper limit. In the while condition we test (number <= upperLimit) i.e. number is less than or equal to upperLimit ( 0 ), this test return false. The control of the program will go to the next statement after the while block. The statements...

sum of even numbers with while

Problem statement: Calculate the sum of even numbers for a given upper limit of integers. Solution: We analyze the problem and know that while statement will be used. We need to sum even numbers only. How can we decide that a number is even or not? We know that the number that is divisible by 2 is...

While Sample Program

To calculate the sum of 2000 integers, we will change the program (i.e. the while condition) in the editor and compile it and run it again. If we need to calculate the sum of first 5000 integers, we will change the program again in the editor and compile and run it again. We are doing this work again...

Repetition Structure (Loop)

In our day to day life, most of the things are repeated. Days and nights repeat themselves 30 times a month. Four seasons replace each other every year. We can see similar phenomenon in the practical life. For example, in the payroll system, some procedures are same for all the employees. These are...

if/else Sample Program

Problem Statement A shopkeeper announces a package for customers that he will give 10 % discount on all bills and if a bill amount is greater than 5000 then a discount of 15 %. Write a C program which takes amount of the bill from user and calculates the payable amount by applying the above discount...

Logical Operators

There are many occasions when we face complex conditions to make a decision. This means that a decision depends upon more than one condition in different ways. Here we combine the conditions with AND or OR. For example, a boy can be selected in basket ball team only if he is more than 18 years old and...

If/else Structure

We have seen that the if structure executes its block of statement(s) only when the condition is true, otherwise the statements are skipped. The if/else structure allows the programmer to specify that a different block of statement(s) is to be executed when the condition is false. The structure of if/else...

IF Sample Program 1

Now let’s see the usage of relational operators by an example. There are two students Amer and Amara. We take their ages from the user, compare them and tell who is older? As there are two students to be compared in terms of age, we need to declare two variables to store their ages. We declare two variables...

Flow Charting

There are different techniques that are used to analyze and design a program. We will use the flow chart technique. A flow chart is a pictorial representation of a program. There are labeled geometrical symbols, together with the arrows connecting one symbol with other. A flow chart helps in correctly...

Conditional Statements

Tuesday, June 12, 2012
In every day life, we are often making decisions. We perform different tasks while taking decisions. For example, the statement ‘Bring one litre of milk while returning home from college, if the milk shop is open’ involves this phenomenon. In this statement, there is an element of decision making. We...
 

introduction to programming Copyright © 2011-2012 | Powered by Blogger