Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Introduction to Programming Lecture 5. In the Previous Lecture Basic structure of C program Basic structure of C program Variables and Data types Variables.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Computer Science Department Relational Operators And Decisions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Basic Of Computer Science
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Unary Not operator !  !true = false  !false = true.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Branching statements.
Chapter 4: Control Structures I
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Intro to Programming Week # 4 Switch Statement Lecture # 7
Intro to Programming Week # 3 If-else Statement Lecture # 6
Introduction to Programming
Summary Two basic concepts: variables and assignments Basic types:
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Branching statements Kingdom of Saudi Arabia
Department of Computer Science
Programming Fundamental
Programming Fundamental-1
Presentation transcript:

Programming Fundamentals Lecture 4

In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output and input Braces

Relational Operators

Decision

If Statement If condition is true statements If Ali’s height is greater then 6 feet Then Ali can become a member of the Basket Ball team

If Statement in C If (condition) statement ;

If Statement in C If ( condition ) { statement1 ; statement2 ; : }

If statement in C

Int n; Cin>>n; If(n>10) { Cout<<“uol”; Cout<<“ok”; }

Add two num..r they equal to five??

Divisible by 3 Int n; Cout<<“enter any number”; Cin>>; If(n%3==0) { Cout<<“the number”<<n<<“is divisible by 3”; }

If-else statement Used for making two way decsns(need?) One condition and two blocks of statements are given After evaluating a condition, one of the block will be executed If condition is true than the first block will be executed If condition is false than the second block will be executed

if-else if (condition) { statement ; - } else { statement ; - }

100 Int n; Cout<<“enter any integer value”; Cin>>n; If(n>100) Cout 100”; Else Cout<<“<100”;

Even or odd Int n; Cout<<“enter an integer”; Cin>>n; If(n%2==1) Cout<<“it is an odd number”; Else Cout<<“even”;

Take avg of 5 students marks and tell if that avg is > or < 100

Nested if statement When an if statement is used within another if statement, it is called nested if statement Used for multiway (not two way) decision making

Syntax If(condition-1) { If (condition-2) { Statement } Statement }

example Int a,b,c; Cout Cin>>a Cout Cin>>b; Cout Cin>>c; If(a==b) { if(a==c) cout<<“equal”; } Else Cout<<“different”; }

Nested if-else structure Used for multiple selection Syntax If(condition1) Statement1; Else if(condition2) Statement2; Else if(condition3) Statement3…....

Logical Operators AND&& OR||

Logical Operators If a is greater than b AND c is greater than d In C if(a > b && c> d) if(age > 18 || height > 5)

Multi-way decision

if ( grade ==‘A’ ) cout << “ Excellent ” ; if ( grade ==‘B’ ) cout << “ Very Good ” ; if ( grade ==‘C’ ) cout << “ Good ” ; if ( grade ==‘D’ ) cout << “ Poor ” ; if ( grade ==‘F’ ) cout << “ Fail ” ; if Statements

if ( grade ==‘A’ ) cout << “ Excellent ” ; else if ( grade ==‘B’ ) cout << “ Very Good ” ; else if ( grade ==‘C’ ) cout << “ Good ” ; else if ( grade ==‘D’ ) cout << “ Poor ” ; if else

if ( grade == ‘A’ ) cout << “ Excellent ” ; else if ( grade == ‘B’ ) … else if … … else … if else

switch statement

switch statements switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … }

switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } switch statements

case ‘A’ : cout << “ Excellent ” ; … … switch statements

Example switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : cout << “Good ” ; case ‘D’ : cout << “ Poor ” ; case ‘F’ : cout << “ Fail ” ; }

break;

Example switch ( grade ) { case ‘A’ : cout << “ Excellent ” ; break ; case ‘B’ : cout << “ Very Good ” ; break ; case ‘C’ : cout << “Good ” ; break ; case ‘D’ : cout << “ Poor ” ; break ; case ‘F’ : cout << “ Fail ” ; break ; }

default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; default :

goto Unconditional Branch of Execution

GOTO { Int c=1; abc: Cout<<c<<endl; C++; If(c<=10) Goto abc; }

Limitations of switch if ( amount > ) statements ;

Whole Number short int long

case ‘A’ : case ‘ 300 ‘ : case ‘ f ‘ :

Minimize the use of break Minimize the use of continue Never use goto

Flow Charting There are different techniques that are used to analyse and design a program. We will use the flow chart technique. A flow chart is a pictorial representation of a program. There are labelled geometrical symbols, together with the arrows connecting one symbol with other.

Flow Chart Symbols Start or stop Process Flow line Continuation mark Decision

Unary Not operator !  !true = false  !false = true