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

Similar documents
DELHI PUBLIC SCHOOL TAPI

CHAPTER : 9 FLOW OF CONTROL

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

C++ PROGRAMMING SKILLS Part 2 Programming Structures

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

Score score < score < score < 65 Score < 50

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

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

REPETITION CONTROL STRUCTURE LOGO

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

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

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

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

Accelerating Information Technology Innovation

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming Fundamentals

INTRODUCTION TO COMPUTER SCIENCE - LAB

C++ Final Exam 2017/2018

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

LECTURE 5 Control Structures Part 2

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

COMPUTER SCIENCE (083)

CHAPTER 9 FLOW OF CONTROL

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

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

Chapter 7. Additional Control Structures

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

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

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Why Is Repetition Needed?

Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

In this chapter you will learn:

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

KENDRIYA VIDYALAYA SANGATHAN, GUWAHATI REGION HALF-YEARLY EXAMINATION-2015 CLASS XI : COMPUTER SCIENCE Time Allotted : 3 hrs. Max.

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

Add Subtract Multiply Divide

Introduction to Programming

Lecture 7 Tao Wang 1

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

Computer Programming. Decision Making (2) Loops

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

LAB 4.1 Relational Operators and the if Statement

IT 1003 Introduction to Programming (New)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

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

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

COMMON QUARTERLY EXAMINATION SEPTEMBER 2018

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

Creating a C++ Program

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

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

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CSI33 Data Structures

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

Exceptions, Case Study-Exception handling in C++.

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Computer Programming

BLUE PRINT SUBJECT: - COMPUTER SCIENCE(083) CLASS-XI. Unit Wise Marks

Control Structures of C++ Programming (2)

Repetition Structures

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Prepared by: Shraddha Modi

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Control Structure and Loop Statements

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Basic Source Character Set for C++ Language:

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

Increment and the While. Class 15

do { statements } while (condition);

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

A Look Back at Arithmetic Operators: the Increment and Decrement

A control structure refers to the way in which the Programmer specifies the order of executing the statements

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

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


SFU CMPT Topic: Control Statements

Name SECTION: 12:45 2:20. True or False (12 Points)

Review Summer CSC1322 Adv Programming in C++ Professor: Zhang

Problem Solving and 'C' Programming

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

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

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Transcription:

SPM 2102 PROGRAMMING LANGUAGE 1 C++ Programming Structure By NORAH MD NOOR 1

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

Introduction: Flow of control Normal flow of control is sequential statements are executed one after another in the order they are written Special structures to change of control: Selection Conditionally execute next statement (if, if-else, if-else-if, switch-case) Iteration (repetition, loops) Repeatedly execute next statement (with condition) for, while, do-while 3

If Statement If markah > 80 YES? TRUE? then process NO? FALSE? end False Value Condition True Process 4

if ( condition ) statement; or if Statement : Syntax if ( condition ) statement 1; statement 2; statement N; } 5

if Statement : examples #include<conio.h> #include<iostream.h> void main() int value; cout<<"jika markah lebih 80, TAHNIAH akan dipaparkan"; cout<<"\njika markah kurang 80, maka TAMAT "; cout<<"\nmasukkan nilai : "; cin>>value; if (value>=80) //condition **syarat cout<<"\n TAHNIAH \n"; //statement**pernyataan getch(); } 6

If Statement : examples #include<conio.h> #include<iostream.h> void main() int markah; cout<<"masukkan markah peperiksaan : "; cin>>markah; if (markah>=40) cout<<"layak menduduki peperiksaan akhir"<<endl; cout<< Berusahalah bersungguh-sungguh"; } getch(); } 7

if Statement : examples if( marks >= 80 ) grade = A ; if ( x > 0 && x < 10 ) sum++; if( key == A ) avg = TotMarks/students; //process cout << Average marks is << avg; } 8

If else Statement if value <40 TRUE? GAGAL else FALSE? TAHNIAH end Value Value < 40 False TAHNIAH True GAGAL 9

if-else Statement : syntax if ( condition ) statement1; else statement2; if ( condition ) statement1; statement2; } else statement3; statement4; } 10

if-else Statement : Example float marks; cout << Enter your marks << endl; cin >> marks; if( marks >= 50 ) cout << You ve passed! ; else cout << You ve failed! << endl 11

if-else else-if Statement Syntax: if ( condition1 ) statement1; else if ( condition2 ) statement2 else if ( condition3 ) statement3 else if ( conditionn-1 ) statementn-1; else statementn 12

if-else-if Statement : Example #include <iostream.h> #include <conio.h> void main ( ) char choice; cout<<"insert your color : R/B/Y?"; cin >> choice; if ( choice == 'R' choice == 'r' ) cout << "Red" << endl; else if ( choice == 'B' choice == 'b') cout << "Blue" << endl; else if ( choice == 'Y' choice == 'y' ) cout << "Yellow" << endl; else cout << "Error" << endl; getch(); } 13

Switch Statement Syntax: switch( expression ) case expression1: statement1; break; case expression2: statement2; break; case expressionn: statementn1; break; default: default_statement; 14

#include <iostream.h> // Multi if example #include <conio.h> void main () Example int no1, no2, choice; cout<< Pilih operasi: 1: Tambah, 2: Tolak, 3: Darab"<<endl; cin >> choice; cout<< Masukkan nombor pertama"<<endl; cin>>no1; cout<< Masukkan nombor kedua"<<endl; cin>>no2; if ( choice == 1) cout <<no1+no2<< endl; else if ( choice == 2) cout <<no1-no2<< endl; else if ( choice == 3) cout <<no1*no2<<endl; else cout << "Error" << endl; getch(); } 15

#include <iostream.h> #include <conio.h> void main () int no1, no2, choice; Example // Switch case example cout<< Pilih operasi: 1: Tambah, 2 : Tolak, 3 : Darab"<<endl; cin >> choice; cout<< Masukkan nombor pertama"<<endl; cin>>no1; cout<< Masukkan nombor kedua"<<endl; cin>>no2; switch (choice) case 1 : cout <<no1+no2<< endl; break; case 2 : cout <<no1-no2<< endl; break; case 3 : cout <<no1*no2<< endl; break; default : cout << "Error" << endl; break; } getch(); } 16

Looping / Iteration While Do-While For

Looping Structure -While Syntax for Looping structure - While while (condition) Statement 1 ; Statement 2; Statement n; } Page 18

Looping structure -while #include <iostream.h> int x = 1; int Y; main() while ( Y <= 21 ) } } Page 19 Cycle x Y 1 1 1 2 2 4 3 3 9 4 4 16 5 5 25 6 6 exit Y = x * x; cout<< \n << x << power of 2 = <<Y; x++;

Looping Structure : DO-WHILE Syntax for Looping structure Do - while do statement; while (condition); Example : number = 0; do cout<<number; number=number+1; } while (number < 10) Page 20

Looping Structure : Difference While number = 0; while (number < 10) //condition cout<<number; //statement number++; } Do-while number = 0; do cout<<number; //statement number=number+1; } while (number < 10); //condition Page 21

Looping Structure : FOR Specifically used for specific loop define (user OR default) for (default ; condition ; increment/decrement_condition) statement Example : for (x=1 ; x <= 10 ; x++) cout<<x; Note : x by default = 0 & x incrementally till 10 Page 22 Cycle x 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10

Page 23 That s all