Control Structure and Loop Statements

Similar documents
VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS

Looping statement While loop

C++ PROGRAMMING SKILLS Part 2 Programming Structures

DELHI PUBLIC SCHOOL TAPI

Sample Paper - II Subject Computer Science

REPETITION CONTROL STRUCTURE LOGO

Prepared by: Shraddha Modi

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

c++ Solutions eedsohag.epizy.com Ahmed Ali

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

CHAPTER 9 FLOW OF CONTROL

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Unit 3 Decision making, Looping and Arrays

Al-Albayt University Computer Science Department. C++ Programming 1 (901131) Coordinator: Dr. Ashraf Al-Ou n

Accelerating Information Technology Innovation

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Decision Making -Branching. Class Incharge: S. Sasirekha

SELECTION STATEMENTS:

D.A.V PUBLIC SCHOOLS, RANCHI ZONE FIRST SUMMATIVE ASSESSMENT CLASS - XI COMPUTER SCIENCE

CHAPTER : 9 FLOW OF CONTROL

CHAPTER 4 CONTROL STRUCTURES

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

BRAIN INTERNATIONAL SCHOOL. Term-I Class XI Sub: Computer Science Revision Worksheet

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

'C' Programming Language

C Programming Class I

1. Answer the following : a) What do you mean by Open Source Software. Give an example. (2)

Lecture 7: General Loops (Chapter 7)

Data Structures II Lesson 1 (Circular Queue)

Question Bank (SPA SEM II)

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Downloaded from

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Score score < score < score < 65 Score < 50

MODULE 2: Branching and Looping


Number: Name-Surname :... Group:...

Problem Solving and 'C' Programming

Subject: PIC Chapter 2.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Fundamentals of Computer Programming Using C

C C++ C++! "# C++ ++

3 The L oop Control Structure

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Downloaded from

خ ث ح 13:10 14:00 خ ث ح 51:51 16:11 ر ن 09:41 11:11 ر ن

Selection / making decision If statement if-else, if-else-if or nested if Switch Case

Introduction to Algorithms and Programming (COMP151)

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

Decision making with if Statement : - Control Statements. Introduction: -

Chapter 5: Control Structures

A Freshman C++ Programming Course

Loops / Repetition Statements

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Homework 2 Solutions Group B 1- Write a C++ program to read a students score in Statistics and print if he is successful or failing.

Computers Programming Course 7. Iulian Năstac

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Chapter 3 Problem Solving and the Computer

LAB 4.1 Relational Operators and the if Statement

C++ Programming: From Problem Analysis to Program Design, Third Edition

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Introduction. C provides two styles of flow control:

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

Control Statements. If Statement if statement tests a particular condition

UIC. C Programming Primer. Bharathidasan University

Computer Programming

home Al-Albayt University Computer Science Department C++ Programming 1 (901133) Instructor: Eng. Rami Jaradat

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Name Section: M/W T/TH Number Definition Matching (8 Points)

2. ARRAYS What is an Array? index number subscrip 2. Write array declarations for the following: 3. What is array initialization?

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Darshan Institute of Engineering & Technology for Diploma Studies

n Group of statements that are executed repeatedly while some condition remains true

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Sample Paper Class XI Subject Computer Sience UNIT TEST II

6 COMPUTER PROGRAMMING

Decision Making and Branching

Chapter 13 Control Structures

CHAPTER-6 GETTING STARTED WITH C++

---

Questions Bank. 14) State any four advantages of using flow-chart

ECE 122. Engineering Problem Solving with Java

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

COMPUTER SCIENCE PRACTICAL GUIDE [ ENGLISH MEDIUM ]

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Decision Making and Loops

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

Transcription:

Control Structure and Loop Statements A C/C++ program executes in sequential order that is the way the instructions are written. There are situations when we have to skip certain code in the program and do something else. The control structure in C++ are used to transfer control of the execution sequence to another point in the program. There are various types of control structures: if-else switch break unconditional and conditional goto continue LOOP statement When a statement or group of statements is required to be executed repeatedly, then loop statements are of great help to us. The loop statement in C++ are given below: For-loop While loop Do while loop Control Statements 1. If statement If statement is used when conditional execution is required. The statement test for a condition, if the condition matches (i.e evaluates to true) then the statements are executes else (if condition evaluates to false) the statements following the if are skipped. There are various form of writing the if statement, and these are: i. if statement ii. if-else statement iii. if-else-if statement iv. nested if statements i. if statement int a,b,c, largest; cout << enter three numbers\n ;

cin>>a>>b>>c; largest = a; if(b>largest) largest = b; If (c > largest) largest = c; cout<< largest of <<a<<, <<b<, and <<c << is = <<largest; ii. The if-else statement The if-else statement WAP to display the result 1 st, 2 nd, 3 rd division or fail if a student scores >60%, >50%, >40%, <40%

int percent, division; cout << enter your percentage of marks <<endl; cin>>percent; if(percent > 60) cout<< You scored 1 st division\n ; else if(percent >50) cout<< You scored 2 nd division\n ; if(percent>40) cout<< You scored 3rd division\n ; else cout<< You Failed\n ; SWITCH CASE //WAP to develop a calculator to perform Add, Sub, Mul, and Division Operation using switch case and do-while statement clrscr(); int a,b,c; char choice,ans; do cout<<"enter A for Addition\n"; cout<<"enter s for Sub"<<endl; cout<< "enter M for Mul"<<endl; cout<<"enter D for Div"<<endl; cout <<"What u want to do?"; cin>>choice; switch(choice) case 'A':cout<<"enter two numbers"; cin>>a>>b; c=a+b; cout<<"the sum is"<<c; break; case 'S': cout<<"enter two numbers"; cin>>a>>b;

c=a-b; cout<<"the diff is"<<c; break; case 'M' : cout<<"enter two numbers"; cin>>a>>b; c=a*b; cout<<"the mul is"<<c; break; case 'D': cout<<"enter two numbers"; cin>>a>>b; c=a/b; cout<<"the div is"<<c; break; default: cout<<"invalid choice"; break; cout <<"Want to do another operation (Y or y for yes)"<<endl; cin>>ans; while(ans=='y' ans=='y'); LOOP Statements FOR-LOOP // program to generate factorial of a number int n, fact=1; cout <<"enter a number whose factorial is required:"<<endl; cin>>n; for (int i=1; i<=n;i++) fact=fact*i; cout<<"factorial of : "<<n <<" is : "<<fact<<endl;

Nested For LOOP #include<conio.> int i,j; clrscr(); for (i=0 ; i<=5 ;i++) for (j=0;j<=i ; j++) cout <<j; cout<<"\n";

#include<iomanip.h> int i,j,k,m=10; clrscr(); for (i=0 ; i<=5 ;i++) cout<<setw(m); for (j=i; j>=0 ; j--) cout <<j; for (k=1;k<=i;k++) cout <<k; m--; cout<<"\n"; BREAK STATEMENT

// Program to enter +ve number; Break if ve number is entered int x,i; for (i=0; i<=10;i++) cout <<"enter a number:\n"; cin>>x; if(x< 0) break; cout<<"\n You entered a -ve number:"; //Program to find if the number is prime or not

int num, d=2; cout <<"\nenter a number :\t"; cin>>num; while(d <num) if(num%d==0) cout<<num<<" is not a prime number\n"; break; d++; if (d==num) cout<< num <<" is prime number"; CONTINUE STATEMENT The continue statement causes control of the loop body to go at the beginning of the loop bypassing the remaining statements in the loop body. The continue statement is usually associated with the if statement. On meeting certain condition the program will enter the next iteration.

In the case of the for loop as soon as after the execution of continue statement, increment/decrement statement of the loop gets executed. After the execution of increment statement, condition will be checked. In case of the while loop, continue statement will take control to the condition statement. In case of the do-while loop, continue statement will take control to the condition statement specified in the while loop. // C++ Program to demonstrate working of continue statement int x,i; cout<<"program to enter +ve number:\n"; for (i=0; i<=10;i++) if(i ==5 i==7 i==9) continue; cout<<"\n i=:\t"<<i; getch()

//The program skips the next statement in the for loop when i==j and prints //the remaining int j,i; clrscr(); for(i=0; i<5;i++) for(j=0;j<5;j++) if (i==j) continue; cout <<"\n"<<i<<" "<<j; // C++ Program to demonstrate working of continue statement int x,i; cout<<"program to enter +ve number:\n"; cout<<"how many times to display:\t"; cin>>x; do for (i=0; i<=10;i++)

if (i >6) continue; cout<<"\n i=:\t"<<i; x--; while(x>=0); Exercise: From Chapter-3 Kanitkar (Let us C) 1. WAP to Print all prime numbers between 1 and 100 (use break and continue statement) 2. WAP to fill the entire screen with smily face (ASCII value=1) 3. WAP to add first seven terms of the following series using for loop 1/1!+2/2!+3/3!... 4. WAP to produce the following outputs: 5 4 3 2 1 0 4 3 2 1 0 3 2 1 0 2 1 0 1 0 0 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 3 2 1 0 1 2 3 2 1 0 1 2 1 0 1 0 * * * * * * * * * * * * * * * 5. WAP to generate the table of 2 to 9 6. WAP to display the printable character when a user input any ASCII code from 0 to 255 7. WAP to find the roots of a quadratic equation