Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Control Statements - Information Technology - Lecture Slides, Slides of Information Technology

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

2012/2013

Uploaded on 12/31/2013

mandhata
mandhata 🇮🇳

4.5

(13)

68 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CONTROL STATEMENTS
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Control Statements - Information Technology - Lecture Slides and more Slides Information Technology in PDF only on Docsity!

CONTROL STATEMENTS

Flow

of

Control

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

Logic

of

an

if

statement

conditionevaluated statement true false

The

if

Statement

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.

The

if

else

Statement

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

Boolean

Expressions

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 ( = )

Block

Statements

Several statements can be grouped together into a block statement delimited by braces

A

block statement can be used wherever a statement is called for in the

C

syntax rules **if (total

MAX) { printf ("Error!!\n"); errorCount++; }**

Block

Statements

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

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

The

switch

Statement

Often a break statement is used as the last statement in each case's statement list

A

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

The

switch

Statement

A

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

switch

Statement

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

Repetition

in

Programs

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 12310000. - Loop is a control structure that repeats a group of steps in a program. - Loop body stands for the repeated statements. - There are three

C

loop control statements:

while , for , and do

while .

Flow

Diagram

of

Loop

Choice

Process

e.g., calculate the value of n ! e.g., read the content in a file