by Dinesh Thakur Category: Control Structures

The program segment given below reads a number from the keyboard (in variable num of type float) and prints its square root. However, as the sqrt function requires a non-negative argument, the program uses a do ..while loop to read the data until valid data is entered.

 
by Dinesh Thakur Category: Control Structures

Often we wish to perform more than one operation when the condition in an if statement evaluates as true and/or false as shown in Fig. In such situations, we use block statements (also called compound statements) within the if statement.

 
by Dinesh Thakur Category: Control Structures

The trapezoidal rule is the simplest method to approximate the definite integral of a function f(x) over the interval [a, b]. Given N equally space points (with a spacing of h) X0, X1, ..., XN such that X0 = a and XN = b, the integral of f(x) can be approximated as the sum

 
by Dinesh Thakur Category: Control Structures

large= a> b? a : b;

This example illustrates how a conditional expression can be used to assign one of two alternative values to a variable depending on the outcome of some condition. As the conditional expression operator has higher precedence than the assignment operator, the above assignment statement is equivalent to

 
by Dinesh Thakur Category: Control Structures

Consider that we have to add the given numbers until the desired sum is obtained (i. e., as long as the sum is less than a specified value).

 
by Dinesh Thakur Category: Control Structures

The program segment given below using a do ...while loop and then determines the average of the given numbers.

 
by Dinesh Thakur Category: Control Structures

As a good programming practice, we display a message to prompt the user before accepting data from the keyboard. This enables the user to enter the required data correctly. However, the user may still enter incorrect data. Such data may cause the programs to print incorrect results.

 
by Dinesh Thakur Category: Control Structures

if (marks >= 0 && marks <= 100)

    printf("Valid marks");

    else printf("Invalid marks");

 
by Dinesh Thakur Category: Control Structures

The program segment given below accepts an integer number in variable digit of type int from the keyboard and if it is a single digit number (0-9), it displays the word corresponding to it;

 
by Dinesh Thakur Category: Control Structures

The while expression may consist of a single expression (as it is generally done in most of the programs); however, we may also use compound conditions or expressions. Multiple expressions may be connected by a comma operator or by Boolean operators. If the expressions are simply connected by comma, it is the last expression that is evaluated. The expressions preceding the last are ignored. In the following while expression the first expression, i.e., j <4 is neglected.

 

Page 6 of 7



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles