Visit Our UNIX and Linux User Community


Multiple conditionals in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple conditionals in a while loop
# 8  
Old 10-19-2007
Hi.
Quote:
Originally Posted by Ygor
Using "break" violates good programming standards because it breaks the "single-entry single-exit" rule for control structures.
I'm not so sure about this claim. There is still a single exit at the bottom of the while, and how you get there seems to be the issue here. I would take the meaning of multiple-exit to be that there would be a place to which control is transferred without explicitly traveling through the end of the while, just as multiple-entry would mean entering the body of the while without going through the top of the while. (For precision I suppose we could say the statement immediately after the end of the while instead of the end of the while itself.)

In any case, I tend to lean toward making understanding and maintenance easy, so readability counts for a lot:
Quote:
For pragmatists, I think it's important to notice that even Dijkstra(as I recall) and Wirth (definitely) admit that there are certain circumstances when GOTOs are best practice: to exit deeply nested structures (of IFs/FORs/WHILEs) in case of unrecoverable error, because doing so with a goto results in far more readable code than does the same code rewritten to test for FATAL_ERROR everywhere.

-- Goto Considered Harmful
I enjoy these Dharma-duels (as long as they don't get out of hand, and I think I will be a grasshopper for long time) ... cheers, drl

Last edited by drl; 10-19-2007 at 12:58 PM.. Reason: Clarify.
# 9  
Old 05-01-2008
Thanks for the logic tip

Quote:
Originally Posted by Ygor
Your logic is at fault. You want the loop to continue WHILE $life is not equal to zero AND $mask is not equal to $word.

De Morgan's laws - Wikipedia, the free encyclopedia
I figure your right on... fix what you were doing wrong in the loop and learn something rather than jump ship... I was stuck in the same loop problem and your post helped me. its been years since I wrote anything and forgot how the || was evaluated.. as soon as I read your post it all came back .. Thanks... Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #260
Difficulty: Easy
Steve Jobs and Steve Wozniak co-founded Apple in 1976 to sell Wozniak's Apple I personal computer.
True or False?

8 More Discussions You Might Find Interesting

1. Programming

Gnu make: default values and conditionals

I need some help. I use variables and conditionals to build with or without specic libraries. I set USEAL ?= 1 # get value from cmd, defaults to 1 (see comment) the conditional is: ifeq ($(USEAL),1) LIBS = <any libs> else LIBS = endif Its amazing that this works when invoked... (4 Replies)
Discussion started by: dodona
4 Replies

2. Shell Programming and Scripting

Script no conditionals

Hello, pro scripters, noob here, I am complete noob in this and I have to write a program which: Calculates the modulus of two numbers which the user enters with keyboard. Number interval 1-9. These two numbers that were entered and modulus which was calculated are stored in a separate file.... (1 Reply)
Discussion started by: IrmantasID
1 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

mysql help : query with 2 conditionals

Hi there, I have a table that stores multiple records for many different servers, each of which is timestamped ... I wanted to write a query that would enable me to only output the "latest" record (based on timestamp) for each "unique" server. So for example my main table looks like this ... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

5. Shell Programming and Scripting

Log successful unix conditionals

Dears, Can anyone advise how to log successful conditionals (unix commands) from any ksh script? For example, the code is as follows: if <command> then Block1 of statements else Block2 of statements fi What needed exactly is to log "Block1 of statements" if its conditon is met.... (4 Replies)
Discussion started by: kadi
4 Replies

6. Shell Programming and Scripting

Using arrays with conditionals

Pardon the pseudo code below, but I'm wondering how I would do something along the lines of this... #!/bin/sh excludeList="A B" theItem="C" if then echo $theItem fi I know I could loop through the array and check against each value, but was wondering if there was an alternate way... (1 Reply)
Discussion started by: myndcraft
1 Replies

7. Shell Programming and Scripting

Multiple Conditionals

Yet another question. I want to make something like this: if ||&& then ... fi but that apparently is not right. I want either the first condition to be true OR the second AND third conditions to be true for the "then" to be processed. How can I do this? (1 Reply)
Discussion started by: jeriryan87
1 Replies

8. Shell Programming and Scripting

ideas for perl script - strings,conditionals..etc

I have a matrix , how do I compare all the elements of a column , lets say I want to check if the columns contain the alphabets "S","H","A","R","A","T". and not "X"s. Lets say matrix looks something like this .. SSSXSH HHXXHA AAXXAT RRRXRS AAXTAR TTTTTA I can hard code it where... (4 Replies)
Discussion started by: sharatz83
4 Replies

Featured Tech Videos