Docsity

C++ Computer Language, Schemes and Mind Maps of Computer science

Partial preview of the text

Download C++ Computer Language and more Schemes and Mind Maps Computer science in PDF only on Docsity!

CHAPTER # 4

CONTROL STRUCTURES

Control Structure

  • A statement used to control the flow of execution of instructions in

a program is called control structure.

  • Control structure are used to implement the logic of a program.
  • There are three kinds of control structures: i.e. 1. Sequential Structure: In sequential structure, the statements of a program are executed in the same order in which they are written in the program. 2. Selection Structure: The selection structure is used for selecting a statement or group of statements for execution on the basis of a given a condition. It is also known as decision structure. 3. Repetition structure: Repetition structure is used to execute a statement or group of statements repeatedly as long as the given condition is true. It is also known as iteration structure or Loop structure.

1. The if statement

• “if” is a decision making statement which is

used to execute or skip a statement by

checking a condition.

• General Syntax:

if (condition)

statements;

Flow chart

Condition (^) Statements T F

Example

  • Write a program that inputs marks and displays “Congratulations, You have passed” if the marks are 40 or more. #include<iostream.h> #include<conio.h> void main() { clrscr(); int marks; cout<<“Please enter your Marks: “; cin>>marks; if (marks >= 40) { cout<<“Congratulations, You have passed”; } getch(); }

Limitation of if statement

• if statement is the simplest selection structure

which execute the statement or set of

statements if the condition is true, but if the

condition is false then nothing will happen.

Flow chart

Condition (^) Statements T F Statements

Explanation

• The condition is evaluated.

• If the condition is true, the block of

statements under “if” will be executed.

• If the condition is false, the block of

statement under “else” will be executed.

• If there is a single statement to be executed

whether the condition is true or false then

braces are not required.

Examples:

• Write a program that read a number and prints

whether it is even or odd number.

• Write a program that reads a number and prints its

square if the number is greater than 10 otherwise

prints its cube.

• Write a program that reads three numbers and prints

the largest one.

• Write a program reads a letter and prints whether it is a

lowercase or uppercase letter.

else-if Statement

• else-if statement is used to choose one block

of statements out of many blocks.

• It is used when there are many options and

only one should be selected on the basis of a

condition.

else-if

• In this structure, if any of the condition is true,

the program executes the statements under

that if or else-if.

• If none of the condition is true, the program

executes the statements under the final else.

Flow Chart

Condition Condition Condition n Statements Statements Statements Statements T T T F F F

Example 1

• Write a program that inputs marks of a

student and prints his Grade according to the

following scheme:

80 – 100 A

70 – 79 B

60 – 69 C

50 – 59 D

Below 50 F

#include <iostream.h> #include <conio.h> void main() { clrscr(); int marks; cout<<“Enter your Marks: “; cin>>marks; if (marks >= 80) cout<<“Your Grade is A\n”; else if (marks >= 70) cout<<“Your Grade is B\n”; else if (marks >= 60) cout<<“Your Grade is C\n”; else if (marks >= 50) cout<<“Your Grade is D\n”; else cout<<“Your Grade is F\n”; getch(); }