


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This lecture is part of lecture series on Information Technology course. This lecture includes: Control Statements, Flow of Control, Conditional Statements, Logic of an If Statement, the If Statement, Logic of an If-Else Statement, Boolean Expressions, Nested If Statements, Flow Diagram of Loop Choice Process, the Switch Statement
Typology: Slides
1 / 34
This page cannot be seen from the preview
Don't miss anything!
Unless specified otherwise, the order of statement execution through a function is linear: one statement after another in sequence
Some programming statements allow us to: - decide whether or not to execute a particular statement - execute a statement over and over, repetitively - These decisions are based on boolean expressions (or conditions
that evaluate to
conditionevaluated statement true false
The if statement has the following syntax: if ( condition ) statement ; if is a C reserved word The condition must be a boolean expression. It mustevaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.
An else clause can be added to an if statement to make an if
else statement if ( condition ) statement ; else statement ;
If the condition is true, statement is executed; if the condition is false, statement is executed - One or the other will be executed, but not both
A condition often uses one of C's equality operators or relational operators ,^ which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to - Note the difference between the equality operator ( == ) and the assignment operator ( = )
Several statements can be grouped together into a block statement delimited by braces
block statement can be used wherever a statement is called for in the
syntax rules **if (total
MAX) { printf ("Error!!\n"); errorCount++; }**
In an if-else statement, the if portion, or the else portion, or both, could be block statements **if (total
MAX) { printf("Error!!");errorCount++; } else{ printf ("Total: %d“, total); current = total2; }*
The switch statement provides another way to decide which statement to execute next
The switch statement evaluates an expression, then attempts to match the result to one of several possible cases - Each case contains a value and a list of statements - The flow of control transfers to statement associated with the first case value that © 2004 Pearson Addison-Wesley. All rights reserved
Often a break statement is used as the last statement in each case's statement list
break statement causes control to transfer to the end of the switch statement
If a break statement is not used, the flow of control will continue into the next case - Sometimes this may be appropriate, but often we want to execute only the statements associated with one case
switch statement can have an optional default case
The default case has no associated value and simply uses the reserved word default - If the default case is present, control will transfer to it if no other case value matches - If there is no default case, and no other value matches, control falls through to the statement after the switch
The expression of a switch statement must result in an integral type
meaning an integer ( byte
short
int
or a char
It cannot be a floating point value
float or double
The implicit test condition in a switch statement is equality
You cannot perform relational checks with a switch statement
In most software, the statements in the program may need to repeat for many times.
e.g., calculate the value of n !. - If n = 10000, it’s not elegant to write the code as 123…10000. - Loop is a control structure that repeats a group of steps in a program. - Loop body stands for the repeated statements. - There are three
loop control statements:
while , for , and do
while .
e.g., calculate the value of n ! e.g., read the content in a file