tailieunhanh - Program Control Statements

This module discusses the statements that control a program’s flow of execution. There are three categories of : selection statements, which include the if and the switch; iteration statements, which include the for, while, and do-while loops; and jump statements, which include break, continue, return, and goto. Except for return, which is discussed later in this book, the remaining control statements, including the if and for statements to which you have already had a brief introduction, are examined here. . | Module3 Program Control Statements Table of Contents CRITICAL SKILL The if CRITICAL SKILL The switch CRITICAL SKILL The for CRITICAL SKILL The while CRITICAL SKILL The do-while CRITICAL SKILL Using break to Exit a CRITICAL SKILL Using CRITICAL SKILL Nested CRITICAL SKILL Using the goto This module discusses the statements that control a program s flow of execution. There are three categories of selection statements which include the if and the switch iteration statements which include the for while and do-while loops and jump statements which include break continue return and goto. Except for return which is discussed later in this book the remaining control statements including the if and for statements to which you have already had a brief introduction are examined here. 1 C A Beginner s Guide by Herbert Schildt CRITICAL SKILL The if Statement Module 1 introduced the if statement. Now it is time to examine it in detail. The complete form of the if statement is expression statement else statement where the targets of the if and else are single statements. The else clause is optional. The targets of both the if and else can also be blocks of statements. The general form of the if using blocks of statements is if expression statement sequence else statement sequence If the conditional expression is true the target of the if will be executed otherwise the target of the else if it exists will be executed. At no time will both be executed. The conditional expression controlling the if may be any type of valid C expression that produces a true or false result. The following program demonstrates the if by playing a simple version of the guess the magic number game. The program generates a random number prompts for your guess and prints the message Right if you guess the magic number. This program also introduces another C library function .