0% found this document useful (0 votes)
47 views

Control Structures Pgms

C language includes a wide variety of powerful and flexible control statements. Nature of the loop or control structure used determines the number of times the statements (or code block) are executed, if executed at all. Loops are used to repeat one statement or set statements more than one time.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Control Structures Pgms

C language includes a wide variety of powerful and flexible control statements. Nature of the loop or control structure used determines the number of times the statements (or code block) are executed, if executed at all. Loops are used to repeat one statement or set statements more than one time.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex. No.

: 4 Date: Objective:

Study of Control statements

C language includes a wide variety of powerful and flexible control statements. Nature of the loop or control structure used determines the number of times the statements (or code block) are executed, if executed at all.

Back ground / Pre requisite: C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements. 1. 2. 3. 4. if statement switch statement Conditional operator statement goto statement

ifelse Statement The if statement is a powerful decision making statement and is used to controlthe flow of execution of statements. It is basically a two-way decision statement and is used in conjunction with an expression. Syntax if (conditional) { block of statements executed if conditional is true; } else { block of statements if condition false; } Switch Statement: The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.

Syntax: Syntax switch (expression) { case item: statements; break; case item: statements; break; case item: statements; break; default: statement; break; }

Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C. For Loop For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. Syntax for (initialization; condition; increment/decrement) { statements block; } While Loop The while loop repeats a statement until the test at the top proves false. For Loop While Loop Do-While Loop

Syntax while(condition) { statement block; }

Do-while loop This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable. Syntax do { statement block; } while(condition);

Choosing the proper control statements: Picking the appropriate control structure will ensure the proper operation of the software and aid in the readability of the software. The use of a properly developed flowchart can aid in the selection of the control structure. Each control structure has its own unique program flow which is evident in a flowchart. Control structures have the common element of a decision located at some point in the flowchart. The location of the processes associated with the decision aid in the identification of the structure. In other words, the relationship of the processes and decision determine the type of control statement. Another key element in the identification of the control structure is the flow through the processes.

Ex. No. : 4 a Date: Objective:

Find Biggest Among Three Numbers

To write a program to find Biggest among three numbers using if-else statement. Problem Statement:

Pseudo Code/Algorithm:

Test Case:

Review Questions:
1) Which among the following is a unconditional control structure

a) do-while

b) if-else

c) goto

d) for

2) Which operator in C is called a ternary operator

a) if..then

b) ++

c) ?

d) ()

Ex. No. : 4 b Date: Objective:

Arithmetic Operation using Switch Case

To write a program to do Arithmetic Operation using Switch Case statement. Problem Statement:

Pseudo Code/Algorithm:

Test Case:

Review Questions: 1. What is the use of break statement in switch-case statement?

2. Which of the following cannot be checked in a switch-case statement? A.Character C.Float B.Integer D.enum

Ex. No. : 4 c Date: Objective:

Reversing the Given Number

To write a program to reverse the given integer using while loop. Problem Statement:

Pseudo Code/Algorithm Step 1: Initialize SUM as 0

Step 2: Enter the number, N to be reversed Step 3: While the number, N is not less than zero do 3.1 R = Find the remainder of N divided by 10 3.2 SUM = Multiply SUM by 10 and add to R 3.3 N = Find the Quotient of N divided by 10 Step 4: Print the value of SUM, which is the reversal of a given number. Test Case :

Review Questions:
1. C supports how many basic looping constructs

a) 2

b) 3

c) 4

d) 4

2. A statement differs from expression by terminating with a

a) ;

b) :

c) ,

d)

Ex. No. : 4 d Date: Objective:

Armstrong Number Generation

To write a program to generate Armstrong numbers using do while loop. Problem Statement:

Pseudo Code/Algorithm

Step 1: Get limit of the generation N Step 2: Do the following for N times Step 2.1: SUM = sum of cubes of each digit of original number Step 2.2: If original number and SUM is same, then print the original Number.

Test Case

Review Questions: 1. Why we use do-while loop in c? Also tell any properties which you know ?

2. What should be the expression return value for a do-while to terminate

a)1

b)0

c)-1

d)NULL

Ex. No. : 4 e Date: Objective:

Fibonacci series

To write a program to generate Fibonacci series using for loop. Problem Statement:

Pseudo Code/Algorithm

Step 1: Get limit of the generation N Step 2: If limit of the generation N is equal to Zero Then print zero Step 3: print zero and one Do the following for 2 to N times Step 3.1: C=sum of first and second First=second Second =c Step 3.2: Print the Fibonacci Series Test Case :

Review Questions:
1. Why n++ executes faster than n+1?

2. Differentiate between a for loop and a while loop? What are it uses?

You might also like