Programming Fundamentals

Similar documents
DELHI PUBLIC SCHOOL TAPI

REPETITION CONTROL STRUCTURE LOGO

Increment and the While. Class 15

Introduction. C provides two styles of flow control:

For Loop. Variations on Format & Specific Examples

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

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

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

CHAPTER : 9 FLOW OF CONTROL

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

INTRODUCTION TO PROGRAMMING

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

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

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

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

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

Prepared by: Shraddha Modi

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

Why Is Repetition Needed?

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

Flow Control. CSC215 Lecture

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

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

Chapter 4: Control structures. Repetition

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

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Unit 3 Decision making, Looping and Arrays

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Object-Oriented Programming in Java

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

CHAPTER 9 FLOW OF CONTROL

Chapter 4: Control structures

Module 4: Decision-making and forming loops

Unit 7. 'while' Loops

SELECTION STATEMENTS:

Advanced Computer Programming

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

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

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

C Programming Lecture V

DECISION CONTROL AND LOOPING STATEMENTS

Introduction to the Java Basics: Control Flow Statements

Loops. CSE 114, Computer Science 1 Stony Brook University

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

LECTURE 5 Control Structures Part 2

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

8. Control statements

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Looping. Arizona State University 1

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Computers Programming Course 7. Iulian Năstac

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

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

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

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Loops (while and for)

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

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

C++ Final Exam 2017/2018

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Chapter 7. Additional Control Structures

Chapter 5: Control Structures II (Repetition)

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

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Control Structures of C++ Programming (2)

UIC. C Programming Primer. Bharathidasan University

C++ PROGRAMMING SKILLS Part 2 Programming Structures

5. Control Statements

In this chapter you will learn:

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

Programming, numerics and optimization

ECE 122. Engineering Problem Solving with Java

EP241 Computing Programming

CS2255 HOMEWORK #1 Fall 2012

Lecture 10. Daily Puzzle

Quiz Determine the output of the following program:

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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.

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

CS 106 Introduction to Computer Science I

204111: Computer and Programming

Introduction to Programming

Lab 10: Alternate Controls

Programming Language. Functions. Eng. Anis Nazer First Semester

Lecture # 6. Repetition. Review. If Else Statements Comparison Operators Boolean Expressions Nested Ifs. Switch Statements.

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

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

Transcription:

Programming Fundamentals Programming Fundamentals Instructor : Zuhair Qadir Lecture # 11 30th-November-2013 1

Programming Fundamentals Programming Fundamentals Lecture # 11 2

Switch Control substitute for long if statements that compare a variable to several "integral" values. expression is any legal C++ expression. statements are any legal C++ statements or block of statements. switch evaluates expression and compares the result to each of the case values. evaluation is only for equality; relational operators may not be used here, nor can Boolean operations. switch (expression) case valueone: case valuetwo:... case valuen: default:

Switch If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block, unless a break statement is encountered. If nothing matches, execution branches to the optional default statement. If there is no default and there is no matching value, execution falls through the switch statement and the statement ends. switch (expression) case valueone: case valuetwo:... case valuen: default:

int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<< Enter Your Selection: ; cin>>input; switch ( input ) case 1: // Note the colon, not a semicolon cout<< press enter to play game ; case 2: cout<< enter you name to load game ; case 3: cout<< enter all players names to load game ; case 4: cout<< press esc to exit game ; Default: cout<<" bad input, quitting\n ;

Loops & Its Logic What is a Loop? An iterative statement is called loop. A type of control structure that repeats a statement or set of statements is known as looping structures. It is also known as iterative or repetitive structure. There are two elements of the loop: the body of loop which is to be executed number of times and a loop condition which terminates the loop when a particular condition is met. Repetition A single statement A set of statements loop consists of three main statements:- (1) Initialization (2) Condition (3) Incrementation 611/12/2014

Types of Loop Types of Loop Counter loop (for loop) Conditional loops (while, do while) For Loop Types of Loop While Loop Do While Loop 711/12/2014

For Loop Operation Initialization Expression Test Expression Body of Loop True Increment Expression False Exit 811/12/2014

For Loop Structure Test Expression Initialization Expression keyword for ( j=0 ; j < 5 ; j++ ) Updation Expression Single Statement Loop Body 911/12/2014

11/12/2014 For Loop Structure Initialization Expression Test Expression Updation Expression keyword for ( j=0 ; j < 5 ; j++ ) Multiple Statement Loop Body 10

11/12/2014 For Loop // demonstrate simple for loop # include <iostream> #include<conio.h> void main() int j; for (j=0 ; j < 5 ; j++) cout<<j * j; getch(); Output: 0 1 4 9 16 11

For Loop Example //print number from 1 to 10 using for loop. #include<stdio.h> #include<conio.h> Void main() int i; for ( i=1; i<=10; i++) Cout<< \n I ; getch(); 12

For Loop Example //program to calculate the sum of number from 1 to 5 using for loop. #include<stdio.h> #include<conio.h> Void main() Int i,sum; for ( i=1; i<=5; i++) sum=sum+i; Cout<< sum is = << sum; getch(); 13

11/12/2014 For Loop Example //program to print odd number between 1 to 10. #include<stdio.h> #include<conio.h> Void main() Int i; for( i=1;i<10;i+=2) cout<< even numbers are<< i << \n\n ; getch(); 14

For Loop Example of reverse loop. //print number from 10 to 1 using for loop. #include<stdio.h> #include<conio.h> Void main() Int i; for ( i=10; i>=1; i--) Cout<< i << endl; getch(); 15

11/12/2014 For Loop Variations Multiple initialization and increment Expressions for (x=0, j=0 ; j < 5 ; j++, x++) controlling Increment or Decrement for ( j=5 ; j > 0 ; j-- ) //decrement by 1 for ( j=0 ; j <10 ; j+=2 ) //increment by 2 16

11/12/2014 The Infinite loop A loop that does not terminate is called Infinite loop. A loop who's condition always remain true is called infinite loop. e.g. for(int i=0;i>0;i++) cout<<i<<endl; 17

Resources to read from http://www.cplusplus.com/doc/tutorial/