by Dinesh Thakur Category: Control Structures

Let us use variables a and b of type float to represent two operands, variable c of type float to represent the result and variable op of type char to represent the operator. In the program given below, an expression of the form a op b is first accepted from the keyboard and a switch statement is used to evaluate the result.

 
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

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

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

The continue statement is another loop interruption statement provided in the C language. It interrupts only the current iteration of the loop as opposed to the break statement which interrupts the execution of the entire loop. The format of the continue statement is as follows:

 
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

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

    printf("Valid marks");

    else printf("Invalid marks");

 
by Dinesh Thakur Category: Control Structures

The program segment given below accepts marks in a single subject and uses a nested if statement to determine the validity of marks and the result if the value of marks is valid. This code can be written in a more readable form using an if-else-if statement as

 
by Dinesh Thakur Category: Control Structures

In this example, the initial value of loop variable j (of type int) is 100 and the update expression (j -= 10) reduces it by 10 after each iteration of the for loop.

 

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.



Search Content







Popular Article